Skip to content

Commit

Permalink
Fix strrpos function
Browse files Browse the repository at this point in the history
  • Loading branch information
feilong-liu committed Dec 10, 2024
1 parent 8ed1609 commit cd9f2dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,25 @@ private static long stringPositionFromEnd(Slice string, Slice substring, long in
return 1;
}

String stringUtf8 = string.toStringUtf8();
String substringUtf8 = substring.toStringUtf8();
int foundInstances = 0;
// set the initial index just after the end of the string
// this is to allow for the initial index decrement
int index = string.length();
int index = stringUtf8.length();
do {
// step backwards through string
index = string.toStringUtf8().lastIndexOf(substring.toStringUtf8(), index - 1);
index = stringUtf8.lastIndexOf(substringUtf8, index - 1);
if (index < 0) {
return 0;
}
foundInstances++;
}
while (foundInstances < instance);

return countCodePoints(string, 0, index) + 1;
// stringPositionFromStart function returns countCodePoints(string, 0, index) + 1 because it directly works on Slice and use indexOf function of Slice,
// Here we convert Slice to Utf8 string call lastIndexOf function of String to get index, hence directly return index+1
return index + 1;
}

@Description("suffix starting at given index")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ public void testStringReversePosition()
assertFunction("STRRPOS('x', '')", BIGINT, 1L);
assertFunction("STRRPOS('', '')", BIGINT, 1L);

assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u7231')", BIGINT, 2L);
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u5E0C\u671B')", BIGINT, 3L);
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u7231')", BIGINT, 4L);
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', '\u5E0C\u671B')", BIGINT, 6L);
assertFunction("STRRPOS('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', 'nice')", BIGINT, 0L);
assertFunction("STRRPOS('Screenshot 2024-12-01 at 12.00.51\u202fPM.png', '.')", BIGINT, 37L);

assertFunction("STRRPOS(NULL, '')", BIGINT, null);
assertFunction("STRRPOS('', NULL)", BIGINT, null);
Expand Down

0 comments on commit cd9f2dd

Please sign in to comment.