Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

31
LINES

< > BotCompany Repo | #1004878 // New luaQuote (without library)

JavaX fragment (include)

1  
static S luaQuote(S s) ctex {
2  
  byte[] in = toUtf8(s);
3  
  new ByteArrayOutputStream buf;
4  
  
5  
  int c;
6  
  buf.write( (byte) '"' );
7  
  for ( int i = 0, n = in.length; i < n; i++ ) {
8  
  	switch ( c = ubyteToInt(in[i]) ) {
9  
  	case '"': case '\\':  case '\n':
10  
  		buf.write( (byte)'\\' );
11  
  		buf.write( (byte)c );
12  
  		break;
13  
  	default:
14  
  		if (c <= 0x1F || c == 0x7F) {
15  
  			buf.write( (byte) '\\' );
16  
  			if (i+1 == n || ubyteToInt(in[i+1]) < '0' || ubyteToInt(in[i+1]) > '9') {
17  
  				buf.write(toUtf8(Integer.toString(c)));
18  
  			} else {
19  
  				buf.write( (byte) '0' );
20  
  				buf.write( (byte) (char) ('0' + c / 10) );
21  
  				buf.write( (byte) (char) ('0' + c % 10) );
22  
  			}
23  
  		} else {
24  
  			buf.write((byte) c);
25  
  		}
26  
  		break;
27  
  	}
28  
  }
29  
  buf.write( (byte) '"' );
30  
  ret fromUtf8(buf.toByteArray());
31  
}

Author comment

Began life as a copy of #1004741

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004878
Snippet name: New luaQuote (without library)
Eternal ID of this version: #1004878/1
Text MD5: fd109588e60bb9088eaf45c107572a4b
Author: stefan
Category: javax / lua
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-11 23:09:25
Source code size: 826 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 443 / 715
Referenced in: [show references]