Skip to content

Commit

Permalink
Fix: use Collator for case-insensitive String comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
manticore-projects committed Feb 13, 2024
1 parent 792da88 commit ae63339
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions h2/src/main/org/h2/util/StringUtils.java
Expand Up @@ -9,6 +9,7 @@
import java.lang.ref.SoftReference;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.Collator;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -1400,24 +1401,18 @@ public static boolean startsWith(String text, String prefix) {

/**
* Case-Insensitive check if a {@param text} starts with a {@param prefix}.
* It is used
*
* @param text the full text starting with a prefix
* @param prefix the full text starting with a prefix
* @return TRUE only if text starts with the prefix
*/
public static boolean startsWithIgnoringCase(String text, String prefix) {
String normalizedText = Normalizer.normalize(text, Normalizer.Form.NFD)
.replaceAll("\\p{M}", "");
String normalizedPrefix = Normalizer.normalize(prefix, Normalizer.Form.NFD)
.replaceAll("\\p{M}", "");

final Pattern pattern = Pattern.compile(
"^" + Pattern.quote(normalizedPrefix)
, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS | Pattern.UNICODE_CASE);

final Matcher matcher = pattern.matcher(normalizedText);
return matcher.find();
if (text.length() < prefix.length()) {
return false;
} else {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
return collator.equals(text.substring(0, prefix.length()), prefix);
}
}

}

0 comments on commit ae63339

Please sign in to comment.