File tree Expand file tree Collapse file tree 1 file changed +29
-2
lines changed
src/main/java/org/truffleruby/core/rope Expand file tree Collapse file tree 1 file changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -116,13 +116,13 @@ protected static String getStructure(Rope rope) {
116
116
}
117
117
118
118
private static String getStructure (LeafRope rope ) {
119
- return " \" " + rope . toString () + " \" " ;
119
+ return escape ( rope ) ;
120
120
}
121
121
122
122
private static String getStructure (ConcatRope rope ) {
123
123
final ConcatState state = rope .getState ();
124
124
return state .isFlattened ()
125
- ? "(\" flat concat rope\" ; " + rope . toString ( ) + ")"
125
+ ? "(\" flat concat rope\" ; " + escape ( rope ) + ")"
126
126
: "(" + getStructure (state .left ) + " + " + getStructure (state .right ) + ")" ;
127
127
}
128
128
@@ -137,6 +137,33 @@ private static String getStructure(RepeatingRope rope) {
137
137
return "(" + getStructure (rope .getChild ()) + "*" + rope .getTimes () + ")" ;
138
138
}
139
139
140
+ private static String escape (Rope rope ) {
141
+ final StringBuilder builder = new StringBuilder ();
142
+ builder .append ('"' );
143
+
144
+ for (int i = 0 ; i < rope .byteLength (); i ++) {
145
+ final byte character = rope .get (i );
146
+ switch (character ) {
147
+ case '\\' :
148
+ builder .append ("\\ " );
149
+ break ;
150
+ case '"' :
151
+ builder .append ("\\ \" " );
152
+ break ;
153
+ default :
154
+ if (character >= 32 && character <= 126 ) {
155
+ builder .append ((char ) character );
156
+ } else {
157
+ builder .append (StringUtils .format ("\\ x%02x" , character ));
158
+ }
159
+ break ;
160
+ }
161
+ }
162
+
163
+ builder .append ('"' );
164
+ return builder .toString ();
165
+ }
166
+
140
167
}
141
168
142
169
@ CoreMethod (names = "bytes?" , onSingleton = true , required = 1 )
You can’t perform that action at this time.
0 commit comments