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()); }