Merge pull request #2397 from hapifhir/ng_codepoint_fix

UTF-16 characters might cause errors when calculating hash values
This commit is contained in:
Nick Goupinets 2021-02-19 17:22:20 -05:00 committed by GitHub
commit ddd7c0f267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 2 deletions

View File

@ -78,4 +78,31 @@ public class StringUtil {
return new String(bytes, StandardCharsets.UTF_8); return new String(bytes, StandardCharsets.UTF_8);
} }
/**
* Gets the string prefix of the specified length.
*
* @param theString
* String to get the prefix from
* @param theCodePointCount
* Length of the prefix in code points
* @return
* Returns the string prefix of the specified number of codepoints.
*/
public static String left(String theString, int theCodePointCount) {
if (theString == null) {
return null;
}
if (theCodePointCount < 0) {
return "";
}
// char count can only be bigger than the code point count
if (theString.length() <= theCodePointCount) {
return theString;
}
return theString.substring(0, theString.offsetByCodePoints(0, theCodePointCount));
}
} }

View File

@ -10,8 +10,21 @@ import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class StringUtilTest { public class StringUtilTest {
@Test
public void testLeft() {
assertNull(StringUtil.left(null, 1));
assertEquals("", StringUtil.left("", 10));
assertEquals("STR", StringUtil.left("STR", 10));
assertEquals(".", StringUtil.left("...", 1));
// check supplementary chars
assertEquals("\uD800\uDF01", StringUtil.left("\uD800\uDF01\uD800\uDF02", 1));
}
@Test @Test
public void testNormalizeString() { public void testNormalizeString() {
assertEquals("TEST TEST", StringUtil.normalizeStringForSearchIndexing("TEST teSt")); assertEquals("TEST TEST", StringUtil.normalizeStringForSearchIndexing("TEST teSt"));

View File

@ -48,7 +48,6 @@ import javax.persistence.SequenceGenerator;
import javax.persistence.Table; import javax.persistence.Table;
import static org.apache.commons.lang3.StringUtils.defaultString; import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.left;
//@formatter:off //@formatter:off
@Embeddable @Embeddable
@ -289,6 +288,7 @@ public class ResourceIndexedSearchParamString extends BaseResourceIndexedSearchP
hashPrefixLength = 0; hashPrefixLength = 0;
} }
return hash(thePartitionSettings, theRequestPartitionId, theResourceType, theParamName, left(theValueNormalized, hashPrefixLength)); return hash(thePartitionSettings, theRequestPartitionId, theResourceType, theParamName, StringUtil.left(theValueNormalized, hashPrefixLength));
} }
} }