}
private IRubyObject dumpCommon(boolean is1_9) {
Ruby runtime = getRuntime();
ByteList buf = null;
Encoding enc = value.getEncoding();
int p = value.getBegin();
int end = p + value.getRealSize();
byte[]bytes = value.getUnsafeBytes();
int len = 2;
while (p < end) {
int c = bytes[p++] & 0xff;
switch (c) {
case '"':case '\\':case '\n':case '\r':case '\t':case '\f':
case '\013': case '\010': case '\007': case '\033':
len += 2;
break;
case '#':
len += isEVStr(bytes, p, end) ? 2 : 1;
break;
default:
if (ASCII.isPrint(c)) {
len++;
} else {
if (is1_9 && enc instanceof UTF8Encoding) {
int n = StringSupport.preciseLength(enc, bytes, p - 1, end) - 1;
if (n > 0) {
if (buf == null) buf = new ByteList();
int cc = codePoint(runtime, enc, bytes, p - 1, end);
Sprintf.sprintf(runtime, buf, "%x", cc);
len += buf.getRealSize() + 4;
buf.setRealSize(0);
p += n;
break;
}
}
len += 4;
}
break;
}
}
if (is1_9 && !enc.isAsciiCompatible()) {
len += ".force_encoding(\"".length() + enc.getName().length + "\")".length();
}
ByteList outBytes = new ByteList(len);
byte out[] = outBytes.getUnsafeBytes();
int q = 0;
p = value.getBegin();
end = p + value.getRealSize();
out[q++] = '"';
while (p < end) {
int c = bytes[p++] & 0xff;
if (c == '"' || c == '\\') {
out[q++] = '\\';
out[q++] = (byte)c;
} else if (c == '#') {
if (isEVStr(bytes, p, end)) out[q++] = '\\';
out[q++] = '#';
} else if (!is1_9 && ASCII.isPrint(c)) {
out[q++] = (byte)c;
} else if (c == '\n') {
out[q++] = '\\';
out[q++] = 'n';
} else if (c == '\r') {
out[q++] = '\\';
out[q++] = 'r';
} else if (c == '\t') {
out[q++] = '\\';
out[q++] = 't';
} else if (c == '\f') {
out[q++] = '\\';
out[q++] = 'f';
} else if (c == '\013') {
out[q++] = '\\';
out[q++] = 'v';
} else if (c == '\010') {
out[q++] = '\\';
out[q++] = 'b';
} else if (c == '\007') {
out[q++] = '\\';
out[q++] = 'a';
} else if (c == '\033') {
out[q++] = '\\';
out[q++] = 'e';
} else if (is1_9 && ASCII.isPrint(c)) {
out[q++] = (byte)c;
} else {
out[q++] = '\\';
if (is1_9) {
if (enc instanceof UTF8Encoding) {
int n = StringSupport.preciseLength(enc, bytes, p - 1, end) - 1;
if (n > 0) {
int cc = codePoint(runtime, enc, bytes, p - 1, end);
p += n;
outBytes.setRealSize(q);
Sprintf.sprintf(runtime, outBytes, "u{%x}", cc);
q = outBytes.getRealSize();
continue;
}
}
outBytes.setRealSize(q);
Sprintf.sprintf(runtime, outBytes, "x%02X", c);
q = outBytes.getRealSize();
} else {
outBytes.setRealSize(q);
Sprintf.sprintf(runtime, outBytes, "%03o", c);
q = outBytes.getRealSize();
}
}
}
out[q++] = '"';
outBytes.setRealSize(q);
assert out == outBytes.getUnsafeBytes(); // must not reallocate
final RubyString result = new RubyString(runtime, getMetaClass(), outBytes);
if (is1_9) {
if (!enc.isAsciiCompatible()) {
result.cat(".force_encoding(\"".getBytes());
result.cat(enc.getName());
result.cat((byte)'"').cat((byte)')');
enc = ASCII;
}
result.associateEncoding(enc);
result.setCodeRange(CR_7BIT);