Skip to content

Commit 8cce99e

Browse files
committed
Escape in Truffle::Ropes.debug_print_rope
1 parent 13f2d44 commit 8cce99e

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

src/main/java/org/truffleruby/core/rope/RopeNodes.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ protected Object debugPrintLeafRope(LeafRope rope, int currentLevel, boolean pri
747747

748748
System.err.println(StringUtils.format(
749749
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
750-
printString ? rope.toString() : "<skipped>",
750+
printString ? RopeOperations.escape(rope) : "<skipped>",
751751
rope.getClass().getSimpleName(),
752752
bytesAreNull,
753753
rope.byteLength(),
@@ -768,7 +768,7 @@ protected Object debugPrintSubstringRope(SubstringRope rope, int currentLevel, b
768768

769769
System.err.println(StringUtils.format(
770770
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; O: %d; E: %s)",
771-
printString ? rope.toString() : "<skipped>",
771+
printString ? RopeOperations.escape(rope) : "<skipped>",
772772
rope.getClass().getSimpleName(),
773773
bytesAreNull,
774774
rope.byteLength(),
@@ -795,7 +795,7 @@ protected Object debugPrintConcatRopeBytes(ConcatRope rope, int currentLevel, bo
795795
if (state.isFlattened()) {
796796
System.err.println(StringUtils.format(
797797
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
798-
printString ? rope.toString() : "<skipped>",
798+
printString ? RopeOperations.escape(rope) : "<skipped>",
799799
rope.getClass().getSimpleName(),
800800
bytesAreNull,
801801
rope.byteLength(),
@@ -805,7 +805,7 @@ protected Object debugPrintConcatRopeBytes(ConcatRope rope, int currentLevel, bo
805805
} else {
806806
System.err.println(StringUtils.format(
807807
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; E: %s)",
808-
printString ? rope.toString() : "<skipped>",
808+
printString ? RopeOperations.escape(rope) : "<skipped>",
809809
rope.getClass().getSimpleName(),
810810
bytesAreNull,
811811
rope.byteLength(),
@@ -830,7 +830,7 @@ protected Object debugPrintRepeatingRope(RepeatingRope rope, int currentLevel, b
830830

831831
System.err.println(StringUtils.format(
832832
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; T: %d; D: %d; E: %s)",
833-
printString ? rope.toString() : "<skipped>",
833+
printString ? RopeOperations.escape(rope) : "<skipped>",
834834
rope.getClass().getSimpleName(),
835835
bytesAreNull,
836836
rope.byteLength(),
@@ -854,7 +854,7 @@ protected Object debugPrintLazyInt(LazyIntRope rope, int currentLevel, boolean p
854854

855855
System.err.println(StringUtils.format(
856856
"%s (%s; BN: %b; BL: %d; CL: %d; CR: %s; V: %d, D: %d; E: %s)",
857-
printString ? rope.toString() : "<skipped>",
857+
printString ? RopeOperations.escape(rope) : "<skipped>",
858858
rope.getClass().getSimpleName(),
859859
bytesAreNull,
860860
rope.byteLength(),

src/main/java/org/truffleruby/core/rope/RopeOperations.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,32 @@ public static boolean anyChildContains(Rope rope, String value) {
687687
// to worry about the risk of retaining a substring rope whose child contains the value.
688688
return rope.byteLength() >= value.length() && RopeOperations.decodeRope(rope).contains(value);
689689
}
690+
691+
public static String escape(Rope rope) {
692+
final StringBuilder builder = new StringBuilder();
693+
builder.append('"');
694+
695+
for (int i = 0; i < rope.byteLength(); i++) {
696+
final byte character = rope.get(i);
697+
switch (character) {
698+
case '\\':
699+
builder.append("\\");
700+
break;
701+
case '"':
702+
builder.append("\\\"");
703+
break;
704+
default:
705+
if (character >= 32 && character <= 126) {
706+
builder.append((char) character);
707+
} else {
708+
builder.append(StringUtils.format("\\x%02x", character));
709+
}
710+
break;
711+
}
712+
}
713+
714+
builder.append('"');
715+
return builder.toString();
716+
}
717+
690718
}

src/main/java/org/truffleruby/core/rope/TruffleRopesNodes.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ protected static String getStructure(Rope rope) {
116116
}
117117

118118
private static String getStructure(LeafRope rope) {
119-
return escape(rope);
119+
return RopeOperations.escape(rope);
120120
}
121121

122122
private static String getStructure(ConcatRope rope) {
123123
final ConcatState state = rope.getState();
124124
return state.isFlattened()
125-
? "(\"flat concat rope\"; " + escape(rope) + ")"
125+
? "(\"flat concat rope\"; " + RopeOperations.escape(rope) + ")"
126126
: "(" + getStructure(state.left) + " + " + getStructure(state.right) + ")";
127127
}
128128

@@ -137,33 +137,6 @@ private static String getStructure(RepeatingRope rope) {
137137
return "(" + getStructure(rope.getChild()) + "*" + rope.getTimes() + ")";
138138
}
139139

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-
167140
}
168141

169142
@CoreMethod(names = "bytes?", onSingleton = true, required = 1)

0 commit comments

Comments
 (0)