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,17 +84,19 @@ static int indexOf(final CharSequence cs, final int searchChar, int start) {
}
//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,17 +150,18 @@ static int lastIndexOf(final CharSequence cs, final int searchChar, int start) {
}
}
//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) {
if (cs.charAt(i) == chars[0]) {
if (i + 1 == cs.length()) {
break;
}
if (cs.charAt(i + 1) == chars[1]) {
return i;
}
}
if (i + 1 == cs.length()) {
break;
}
if (cs.charAt(i + 1) == chars[1]) {
return i;
}
}
}
}
return NOT_FOUND;

View File

@ -300,6 +300,8 @@ public void testIndexOf_charInt() {
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();