Skip to content

Commit

Permalink
Compat fixes for String#inspect wrt dummy UTF encodings.
Browse files Browse the repository at this point in the history
See #2581.
  • Loading branch information
headius committed Mar 16, 2015
1 parent 94bda8d commit af940b1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -2419,6 +2419,7 @@ public IRubyObject inspect19() {
result.associateEncoding(resultEnc);

boolean isUnicode = StringSupport.isUnicode(enc);
boolean asciiCompat = enc.isAsciiCompatible();

EncodingDB.Entry e = null;
CaseInsensitiveBytesHash<EncodingDB.Entry> encodings = runtime.getEncodingService().getEncodings();
Expand All @@ -2431,6 +2432,7 @@ public IRubyObject inspect19() {
} else if (c0 == 0xFF && c1 == 0xFE) {
e = encodings.get("UTF-16LE".getBytes());
} else {
e = encodings.get("ASCII-8BIT".getBytes());
isUnicode = false;
}
} else if (enc == encodings.get("UTF-32".getBytes()).getEncoding() && end - p > 3) {
Expand All @@ -2444,6 +2446,7 @@ public IRubyObject inspect19() {
} else if (c3 == 0 && c2 == 0 && c1 == 0xFE && c0 == 0xFF) {
e = encodings.get("UTF-32LE".getBytes());
} else {
e = encodings.get("ASCII-8BIT".getBytes());
isUnicode = false;
}
}
Expand All @@ -2468,13 +2471,13 @@ public IRubyObject inspect19() {
}
int c = enc.mbcToCode(bytes, p, end);
p += n;
if ((enc.isAsciiCompatible() || isUnicode) &&
if ((asciiCompat || isUnicode) &&
(c == '"' || c == '\\' ||
(c == '#' && p < end && (StringSupport.preciseLength(enc, bytes, p, end) > 0) &&
(cc = codePoint(runtime, enc, bytes, p, end)) == '$' || cc == '@' || cc == '{'))) {
if (p - n > prev) result.cat(bytes, prev, p - n - prev);
result.cat('\\');
if (enc.isAsciiCompatible() || enc == resultEnc) {
if (asciiCompat || enc == resultEnc) {
prev = p - n;
continue;
}
Expand All @@ -2500,7 +2503,7 @@ public IRubyObject inspect19() {
continue;
}

if ((enc == resultEnc && enc.isPrint(c)) || (enc.isAsciiCompatible() && Encoding.isAscii(c) && enc.isPrint(c))) {
if ((enc == resultEnc && enc.isPrint(c)) || (asciiCompat && Encoding.isAscii(c) && enc.isPrint(c))) {
continue;
} else {
if (p - n > prev) result.cat(bytes, prev, p - n - prev);
Expand Down

0 comments on commit af940b1

Please sign in to comment.