@@ -205,24 +205,24 @@ public static String decodeOrEscapeBinaryRope(Rope rope, byte[] bytes) {
205
205
if (rope .isAsciiOnly () || rope .getEncoding () != ASCIIEncoding .INSTANCE ) {
206
206
return decodeRopeSegment (rope , bytes , 0 , bytes .length );
207
207
} else {
208
- // A Rope with BINARY encoding cannot be converted faithfully to a Java String.
209
- // (ISO_8859_1 would just show random characters for bytes above 128)
210
- // Therefore we convert non-US-ASCII characters to "\xNN".
211
- // MRI Symbol#inspect for binary symbols is similar: "\xff".b.to_sym => :"\xFF"
212
-
213
- final StringBuilder builder = new StringBuilder (rope .byteLength ());
208
+ return escapeBinaryRope (bytes );
209
+ }
210
+ }
214
211
215
- for (int i = 0 ; i < bytes .length ; i ++) {
216
- final byte c = bytes [i ];
217
- if (c >= 0 ) { // US-ASCII character
218
- builder .append ((char ) (c & 0xFF ));
219
- } else {
220
- builder .append ("\\ x" ).append (String .format ("%02X" , c & 0xFF ));
221
- }
212
+ private static String escapeBinaryRope (byte [] bytes ) {
213
+ // A Rope with BINARY encoding cannot be converted faithfully to a Java String.
214
+ // (ISO_8859_1 would just show random characters for bytes above 128)
215
+ // Therefore we convert non-US-ASCII characters to "\xNN".
216
+ // MRI Symbol#inspect for binary symbols is similar: "\xff".b.to_sym => :"\xFF"
217
+ final StringBuilder builder = new StringBuilder (bytes .length );
218
+ for (final byte c : bytes ) {
219
+ if (c >= 0 ) { // US-ASCII character
220
+ builder .append ((char ) (c & 0xFF ));
221
+ } else {
222
+ builder .append ("\\ x" ).append (String .format ("%02X" , c & 0xFF ));
222
223
}
223
-
224
- return builder .toString ();
225
224
}
225
+ return builder .toString ();
226
226
}
227
227
228
228
public static String decodeRope (Rope value ) {
@@ -242,9 +242,15 @@ private static String decodeRopeSegment(Rope value, byte[] bytes, int byteOffset
242
242
}
243
243
}
244
244
245
+ /** This method has no side effects, because it does not even have access to the Rope - for debugging only. */
245
246
@ TruffleBoundary
246
247
public static String decode (Encoding encoding , byte [] bytes ) {
247
- return decode (EncodingManager .charsetForEncoding (encoding ), bytes , 0 , bytes .length );
248
+ if (encoding == ASCIIEncoding .INSTANCE ) {
249
+ return escapeBinaryRope (bytes );
250
+ } else {
251
+ final Charset charset = EncodingManager .charsetForEncoding (encoding );
252
+ return decode (charset , bytes , 0 , bytes .length );
253
+ }
248
254
}
249
255
250
256
private static String decode (Charset charset , byte [] bytes , int byteOffset , int byteLength ) {
0 commit comments