Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,11 @@ public static int indexOf(final UTF8String target, final UTF8String pattern,
// Initialize the string search with respect to the specified ICU collation.
String targetStr = target.toValidString();
String patternStr = pattern.toValidString();
// Check if `start` is out of bounds. The provided offset `start` is given in number of
// codepoints, so a simple `targetStr.length` check is not sufficient here. This check is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation.

// needed because `String.offsetByCodePoints` throws an `IndexOutOfBoundsException`
// exception when the offset is out of bounds.
if (targetStr.codePointCount(0, targetStr.length()) <= start) return MATCH_NOT_FOUND;
StringSearch stringSearch =
CollationFactory.getStringSearch(targetStr, patternStr, collationId);
stringSearch.setOverlapping(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,27 @@ public void testStringLocate() throws SparkException {
assertStringLocate("b", "a🙃x🙃b", 4, "UTF8_LCASE", 5);
assertStringLocate("b", "a🙃x🙃b", 4, "UNICODE", 5);
assertStringLocate("b", "a🙃x🙃b", 4, "UNICODE_CI", 5);
// Out of bounds test cases.
assertStringLocate("a", "asd", 4, "UTF8_BINARY", 0);
assertStringLocate("a", "asd", 4, "UTF8_LCASE", 0);
assertStringLocate("a", "asd", 4, "UNICODE", 0);
assertStringLocate("a", "asd", 4, "UNICODE_CI", 0);
assertStringLocate("a", "asd", 100, "UTF8_BINARY", 0);
assertStringLocate("a", "asd", 100, "UTF8_LCASE", 0);
assertStringLocate("a", "asd", 100, "UNICODE", 0);
assertStringLocate("a", "asd", 100, "UNICODE_CI", 0);
assertStringLocate("a", "🙃🙃", 4, "UTF8_BINARY", 0);
assertStringLocate("a", "🙃🙃", 4, "UTF8_LCASE", 0);
assertStringLocate("a", "🙃🙃", 4, "UNICODE", 0);
assertStringLocate("a", "🙃🙃", 4, "UNICODE_CI", 0);
assertStringLocate("", "asd", 100, "UTF8_BINARY", 1);
assertStringLocate("", "asd", 100, "UTF8_LCASE", 1);
assertStringLocate("", "asd", 100, "UNICODE", 1);
assertStringLocate("", "asd", 100, "UNICODE_CI", 1);
assertStringLocate("asd", "", 100, "UTF8_BINARY", 0);
assertStringLocate("asd", "", 100, "UTF8_LCASE", 0);
assertStringLocate("asd", "", 100, "UNICODE", 0);
assertStringLocate("asd", "", 100, "UNICODE_CI", 0);
}

/**
Expand Down