Skip to content

Commit 99a9f50

Browse files
committed
Use regionEquals() in String#end_with?
1 parent e136c59 commit 99a9f50

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ protected boolean endWithBytes(Object string, Object suffix, RubyEncoding enc,
988988
@Cached BytesNode suffixBytesNode,
989989
@CachedLibrary(limit = "2") RubyStringLibrary strings,
990990
@CachedLibrary(limit = "2") RubyStringLibrary stringsSuffix,
991-
@Cached ConditionProfile leftAdjustProfile,
992-
@Cached LoopConditionProfile loopProfile) {
991+
@Cached ConditionProfile isCharacterHeadProfile) {
993992

994993
final Rope stringRope = strings.getRope(string);
995994
final Rope suffixRope = stringsSuffix.getRope(suffix);
@@ -1007,21 +1006,11 @@ protected boolean endWithBytes(Object string, Object suffix, RubyEncoding enc,
10071006

10081007
final int offset = stringByteLength - suffixByteLength;
10091008

1010-
if (leftAdjustProfile.profile(!isCharacterHead(enc, stringByteLength, stringBytes, offset))) {
1009+
if (isCharacterHeadProfile.profile(!isCharacterHead(enc, stringByteLength, stringBytes, offset))) {
10111010
return false;
10121011
}
10131012

1014-
int i = 0;
1015-
try {
1016-
for (; loopProfile.inject(i < suffixByteLength); i++) {
1017-
if (stringBytes[offset + i] != suffixBytes[i]) {
1018-
return false;
1019-
}
1020-
}
1021-
} finally {
1022-
profileAndReportLoopCount(loopProfile, i);
1023-
}
1024-
return true;
1013+
return ArrayUtils.regionEquals(stringBytes, offset, suffixBytes, 0, suffixByteLength);
10251014
}
10261015

10271016
private boolean isCharacterHead(RubyEncoding enc, int stringByteLength, byte[] stringBytes, int offset) {

0 commit comments

Comments
 (0)