LANG-1300: fixed CharSequenceUtils indexOf for Supplementary chars

This commit is contained in:
MarkDacek 2017-03-08 22:58:51 -05:00
parent 66f8569ecc
commit 12e597a78c
2 changed files with 22 additions and 17 deletions

View File

@ -84,16 +84,18 @@ public class CharSequenceUtils {
}
//supplementary characters (LANG1300)
if (searchChar <= Character.MAX_CODE_POINT) {
int ind = 0;
char[] chars = Character.toChars(searchChar);
for (int i = start; i < sz; i++) {
if (cs.charAt(i) == chars[0]) {
if (i + 1 == sz) {
break;
}
if (cs.charAt(i + 1) == chars[1]) {
return i;
}
for (int i = start; i < sz - 1; i++) {
char high = cs.charAt(i);
char low = cs.charAt(i + 1);
if (high == chars[0] && low == chars[1]) {
return ind;
} else if (Character.isSurrogatePair(high, low)) {
//skip over 1
i++;
}
ind++;
}
}
return NOT_FOUND;
@ -148,6 +150,7 @@ public class CharSequenceUtils {
}
}
//supplementary characters (LANG1300)
//NOTE - we must do a forward traversal for this to avoid duplicating code points
if (searchChar <= Character.MAX_CODE_POINT) {
char[] chars = Character.toChars(searchChar);
for (int i = start; i >= 0; --i) {

View File

@ -300,6 +300,8 @@ public class StringUtilsEqualsIndexOfTest {
StringBuilder builder = new StringBuilder();
builder.appendCodePoint(CODE_POINT);
assertEquals(0, StringUtils.indexOf(builder, CODE_POINT, 0));
builder.appendCodePoint(CODE_POINT);
assertEquals(1, StringUtils.indexOf(builder, CODE_POINT, 1));
//inner branch on the supplementary character block
char[] tmp = {(char) 55361};
builder = new StringBuilder();