private ByteList inspectIntoByteList(boolean ignoreKCode) {
Ruby runtime = getRuntime();
Encoding enc = runtime.getKCode().getEncoding();
final int length = value.length();
ByteList sb = new ByteList(length + 2 + length / 100);
sb.append('\"');
for (int i = 0; i < length; i++) {
int c = value.get(i) & 0xFF;
if (!ignoreKCode) {
int seqLength = enc.length((byte)c);
if (seqLength > 1 && (i + seqLength -1 < length)) {
// don't escape multi-byte characters, leave them as bytes
sb.append(value, i, seqLength);
i += seqLength - 1;
continue;
}
}
if (isAlnum(c)) {
sb.append((char)c);
} else if (c == '\"' || c == '\\') {
sb.append('\\').append((char)c);
} else if (c == '#' && isEVStr(i, length)) {
sb.append('\\').append((char)c);
} else if (isPrint(c)) {
sb.append((char)c);
} else if (c == '\n') {
sb.append('\\').append('n');
} else if (c == '\r') {
sb.append('\\').append('r');
} else if (c == '\t') {
sb.append('\\').append('t');
} else if (c == '\f') {
sb.append('\\').append('f');
} else if (c == '\u000B') {
sb.append('\\').append('v');
} else if (c == '\u0007') {
sb.append('\\').append('a');
} else if (c == '\u0008') {
sb.append('\\').append('b');
} else if (c == '\u001B') {
sb.append('\\').append('e');
} else {
sb.append(ByteList.plain(Sprintf.sprintf(runtime,"\\%03o",c)));
}
}
sb.append('\"');
return sb;
}