Merge pull request #2397 from hapifhir/ng_codepoint_fix
UTF-16 characters might cause errors when calculating hash values
This commit is contained in:
commit
ddd7c0f267
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"));
|
||||||
|
|
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue