Skip to content

Commit 3d3b2eb

Browse files
committed
Simplify Primitive.string_byte_index too like Primitive.string_character_index
1 parent 02902f8 commit 3d3b2eb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,7 +4504,7 @@ protected Object stringCharacterIndex(Rope stringRope, Rope patternRope, int off
45044504
}
45054505

45064506
protected boolean patternFits(Rope stringRope, Rope patternRope, int offset) {
4507-
return patternRope.byteLength() + offset <= stringRope.byteLength();
4507+
return offset + patternRope.byteLength() <= stringRope.byteLength();
45084508
}
45094509
}
45104510

@@ -4526,16 +4526,11 @@ protected RubyNode coercePatternToRope(RubyNode pattern) {
45264526
return ToRopeNodeGen.create(pattern);
45274527
}
45284528

4529-
@Specialization(guards = "offset < 0")
4530-
protected Object stringByteIndexNegativeOffset(Rope stringRope, Rope patternRope, int offset) {
4531-
return nil;
4532-
}
4533-
45344529
@Specialization(
45354530
guards = {
45364531
"offset >= 0",
45374532
"singleByteOptimizableNode.execute(stringRope)",
4538-
"patternRope.byteLength() > stringRope.byteLength()" })
4533+
"!patternFits(stringRope, patternRope, offset)" })
45394534
protected Object stringByteIndexPatternTooLarge(Rope stringRope, Rope patternRope, int offset) {
45404535
return nil;
45414536
}
@@ -4544,7 +4539,7 @@ protected Object stringByteIndexPatternTooLarge(Rope stringRope, Rope patternRop
45444539
guards = {
45454540
"offset >= 0",
45464541
"singleByteOptimizableNode.execute(stringRope)",
4547-
"patternRope.byteLength() <= stringRope.byteLength()" })
4542+
"patternFits(stringRope, patternRope, offset)" })
45484543
protected Object stringByteIndexSingleByteOptimizable(Rope stringRope, Rope patternRope, int offset,
45494544
@Cached RopeNodes.BytesNode stringBytesNode,
45504545
@Cached RopeNodes.BytesNode patternBytesNode,
@@ -4577,7 +4572,7 @@ protected Object stringByteIndexSingleByteOptimizable(Rope stringRope, Rope patt
45774572
guards = {
45784573
"offset >= 0",
45794574
"!singleByteOptimizableNode.execute(stringRope)",
4580-
"patternRope.byteLength() <= stringRope.byteLength()" })
4575+
"patternFits(stringRope, patternRope, offset)" })
45814576
protected Object stringByteIndex(Rope stringRope, Rope patternRope, int offset,
45824577
@Cached RopeNodes.CalculateCharacterLengthNode calculateCharacterLengthNode,
45834578
@Cached RopeNodes.BytesNode stringBytesNode,
@@ -4607,6 +4602,10 @@ protected Object stringByteIndex(Rope stringRope, Rope patternRope, int offset,
46074602

46084603
return nil;
46094604
}
4605+
4606+
protected boolean patternFits(Rope stringRope, Rope patternRope, int offset) {
4607+
return offset + patternRope.byteLength() <= stringRope.byteLength();
4608+
}
46104609
}
46114610

46124611
/** Calculates the byte offset of a character, indicated by a character index, starting from a provided byte offset

0 commit comments

Comments
 (0)