Skip to content

Commit 02902f8

Browse files
author
Lillian Zhang
committed
Edits from PR comments
1) Remove inaccesible offset < 0 case 2) Consider offset length in guards
1 parent 03a790f commit 02902f8

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,16 +4416,11 @@ protected RubyNode coercePatternToRope(RubyNode pattern) {
44164416
return ToRopeNodeGen.create(pattern);
44174417
}
44184418

4419-
@Specialization(guards = "offset < 0")
4420-
protected Object stringCharacterIndexNegativeOffset(Rope stringRope, Rope patternRope, int offset) {
4421-
return nil;
4422-
}
4423-
44244419
@Specialization(
44254420
guards = {
44264421
"offset >= 0",
44274422
"singleByteOptimizableNode.execute(stringRope)",
4428-
"patternRope.byteLength() > stringRope.byteLength()" })
4423+
"!patternFits(stringRope, patternRope, offset)" })
44294424
protected Object stringCharacterIndexPatternTooLarge(Rope stringRope, Rope patternRope, int offset) {
44304425
return nil;
44314426
}
@@ -4434,7 +4429,7 @@ protected Object stringCharacterIndexPatternTooLarge(Rope stringRope, Rope patte
44344429
guards = {
44354430
"offset >= 0",
44364431
"singleByteOptimizableNode.execute(stringRope)",
4437-
"patternRope.byteLength() <= stringRope.byteLength()" })
4432+
"patternFits(stringRope, patternRope, offset)" })
44384433
protected Object stringCharacterIndexSingleByteOptimizable(Rope stringRope, Rope patternRope, int offset,
44394434
@Cached RopeNodes.BytesNode stringBytesNode,
44404435
@Cached RopeNodes.BytesNode patternBytesNode,
@@ -4507,6 +4502,10 @@ protected Object stringCharacterIndex(Rope stringRope, Rope patternRope, int off
45074502

45084503
return nil;
45094504
}
4505+
4506+
protected boolean patternFits(Rope stringRope, Rope patternRope, int offset) {
4507+
return patternRope.byteLength() + offset <= stringRope.byteLength();
4508+
}
45104509
}
45114510

45124511
@Primitive(name = "string_byte_index", lowerFixnum = 2)

0 commit comments

Comments
 (0)