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)

static S luaQuote(S s) ctex {
  byte[] in = toUtf8(s);
  new ByteArrayOutputStream buf;
  
  int c;
  buf.write( (byte) '"' );
  for ( int i = 0, n = in.length; i < n; i++ ) {
  	switch ( c = ubyteToInt(in[i]) ) {
  	case '"': case '\\':  case '\n':
  		buf.write( (byte)'\\' );
  		buf.write( (byte)c );
  		break;
  	default:
  		if (c <= 0x1F || c == 0x7F) {
  			buf.write( (byte) '\\' );
  			if (i+1 == n || ubyteToInt(in[i+1]) < '0' || ubyteToInt(in[i+1]) > '9') {
  				buf.write(toUtf8(Integer.toString(c)));
  			} else {
  				buf.write( (byte) '0' );
  				buf.write( (byte) (char) ('0' + c / 10) );
  				buf.write( (byte) (char) ('0' + c % 10) );
  			}
  		} else {
  			buf.write((byte) c);
  		}
  		break;
  	}
  }
  buf.write( (byte) '"' );
  ret fromUtf8(buf.toByteArray());
}

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: 440 / 711
Referenced in: [show references]