Skip to content

Commit 13f2d44

Browse files
committed
Split Rope#toString between a safe and unsafe version
1 parent 069c1f3 commit 13f2d44

22 files changed

+53
-61
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,4 @@ public final byte[] getBytes() {
5454
return bytes;
5555
}
5656

57-
@Override
58-
public final String toString() {
59-
if (DEBUG_ROPE_BYTES) {
60-
final byte[] bytesBefore = bytes;
61-
final String string = RopeOperations.decodeOrEscapeBinaryRope(this, RopeOperations.flattenBytes(this));
62-
assert bytes == bytesBefore : "bytes should not be modified by Rope#toString() as otherwise inspecting a Rope would have a side effect";
63-
return string;
64-
} else {
65-
return RopeOperations.decodeRope(this);
66-
}
67-
}
68-
6957
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ public int hashCode() {
183183
return RopeOperations.hashForRange(this, 1, 0, byteLength());
184184
}
185185

186-
@Override
187-
public String toString() {
188-
return toLeafRope().toString();
189-
}
190-
191186
@Override
192187
Rope withEncoding7bit(Encoding newEncoding, ConditionProfile bytesNotNull) {
193188
return withEncoding(newEncoding);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public abstract class Rope implements Comparable<Rope> {
2121
// NativeRope, RepeatingRope, 3 LeafRope, ConcatRope, SubstringRope, 1 LazyRope
2222
public static final int NUMBER_OF_CONCRETE_CLASSES = 8;
2323

24-
// Useful for debugging. Setting to true avoids ManagedRope#toString to populate bytes as a side-effect of the debugger calling toString().
25-
protected static final boolean DEBUG_ROPE_BYTES = false;
26-
2724
public final Encoding encoding;
2825
private final int byteLength;
2926
private int hashCode = 0;
@@ -161,8 +158,15 @@ public byte get(int index) {
161158
return getByteSlow(index);
162159
}
163160

161+
@Override
162+
public String toString() {
163+
// This is designed to not have any side effects - compare to getString
164+
return RopeOperations.decode(encoding, RopeOperations.flattenBytes(this));
165+
}
166+
164167
/** Should only be used by the parser */
165-
public final String getString() {
168+
public final String normaliseAndGetJavaString() {
169+
// This will have side effects such as flattening a ConcatString
166170
return RopeOperations.decodeRope(this);
167171
}
168172

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void register(LeafRope rope) {
5454
final BytesKey key = new BytesKey(rope.getBytes(), rope.getEncoding());
5555
final Rope existing = bytesToRope.put(key, rope);
5656
if (existing != null && existing != rope) {
57-
throw new AssertionError("Duplicate Rope in RopeCache: " + existing.getString());
57+
throw new AssertionError("Duplicate Rope in RopeCache: " + existing);
5858
}
5959
}
6060

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static Rope ascii(String string) {
112112
final LeafRope rope = new AsciiOnlyLeafRope(bytes, USASCIIEncoding.INSTANCE).computeHashCode();
113113
final Rope existing = ROPE_CONSTANTS.putIfAbsent(string, rope);
114114
if (existing != null) {
115-
throw new AssertionError("Duplicate Rope in RopeConstants: " + existing.getString());
115+
throw new AssertionError("Duplicate Rope in RopeConstants: " + existing);
116116
}
117117
return rope;
118118
}

src/main/java/org/truffleruby/parser/ast/ClassVarAsgnParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ClassVarAsgnParseNode extends AssignableParseNode implements INameN
4747
public ClassVarAsgnParseNode(SourceIndexLength position, Rope name, ParseNode valueNode) {
4848
super(position, valueNode);
4949

50-
this.name = name.getString();
50+
this.name = name.normaliseAndGetJavaString();
5151
}
5252

5353
@Override

src/main/java/org/truffleruby/parser/ast/ClassVarParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ClassVarParseNode extends ParseNode implements INameNode, SideEffec
4343
private String name;
4444

4545
public ClassVarParseNode(SourceIndexLength position, Rope name) {
46-
this(position, name.getString());
46+
this(position, name.normaliseAndGetJavaString());
4747
}
4848

4949
public ClassVarParseNode(SourceIndexLength position, String name) {

src/main/java/org/truffleruby/parser/ast/Colon3ParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Colon3ParseNode extends ParseNode implements INameNode {
4545

4646
public Colon3ParseNode(SourceIndexLength position, Rope name) {
4747
super(position);
48-
this.name = name.getString();
48+
this.name = name.normaliseAndGetJavaString();
4949
}
5050

5151
@Override

src/main/java/org/truffleruby/parser/ast/ConstDeclParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ConstDeclParseNode(SourceIndexLength position, Rope name, INameNode const
4848
super(position, valueNode);
4949

5050
assert constNode != null || (name != null && !name.isEmpty());
51-
this.name = name == null ? null : name.getString();
51+
this.name = name == null ? null : name.normaliseAndGetJavaString();
5252
this.constNode = constNode;
5353
}
5454

src/main/java/org/truffleruby/parser/ast/ConstParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ConstParseNode extends ParseNode implements INameNode {
4343
private String name;
4444

4545
public ConstParseNode(SourceIndexLength position, Rope name) {
46-
this(position, name.getString());
46+
this(position, name.normaliseAndGetJavaString());
4747
}
4848

4949
public ConstParseNode(SourceIndexLength position, String name) {

0 commit comments

Comments
 (0)