-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
String.toUpper()
and implement Regular Expression based…
… `StringUtils.startsWithIgnoringCase()`
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,15 @@ | |
import java.lang.ref.SoftReference; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.text.Normalizer; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.IntPredicate; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.h2.api.ErrorCode; | ||
import org.h2.engine.SysProperties; | ||
|
@@ -1383,4 +1386,38 @@ public static String escapeMetaDataPattern(String pattern) { | |
return replaceAll(pattern, "\\", "\\\\"); | ||
} | ||
|
||
/** | ||
* Case-sensitive check if a {@param text} starts with a {@param prefix}. | ||
* It only calls {@code String.startsWith()} and is only here for API consistency | ||
* | ||
* @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 startsWith(String text, String prefix) { | ||
return text.startsWith(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( | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
manticore-projects
Author
Contributor
|
||
"^" + normalizedPrefix | ||
, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE | Pattern.COMMENTS | Pattern.UNICODE_CASE); | ||
|
||
final Matcher matcher = pattern.matcher(normalizedText); | ||
return matcher.find(); | ||
} | ||
|
||
} |
3 comments
on commit 136a77c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really sorry to bother you, but this does not work. I have changed your code to make it working:
Collator collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
return collator.compare(text.substring(0, prefix.length()), prefix)==0;
But this lets the tests fail.
I also tried all other constants Collator.SECONDARY
, Collator.TERTIARY
, NO_DECOMPOSITION
, IDENTICAL
, CANONICAL_DECOMPOSITION
, FULL_DECOMPOSITION
but the tests keep failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the the java doc I understand that PRIMARY
was our friend:
ou can set a Collator's strength property to determine the level of difference considered significant in comparisons. Four strengths are provided: PRIMARY, SECONDARY, TERTIARY, and IDENTICAL. The exact assignment of strengths to language features is locale dependent. For example, in Czech, "e" and "f" are considered primary differences, while "e" and "ě" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical. The following shows how both case and accents could be ignored for US English.
//Get the Collator for US English and set its strength to PRIMARY
Collator usCollator = Collator.getInstance(Locale.US);
usCollator.setStrength(Collator.PRIMARY);
if( usCollator.compare("abc", "ABC") == 0 ) {
System.out.println("Strings are equivalent");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those tests fail:
result = client.get(url, "autoCompleteList.do?query=select 'abc");
assertContains(StringUtils.urlDecode(result), "'");
result = client.get(url, "autoCompleteList.do?query=select 'abc''");
assertContains(StringUtils.urlDecode(result), "'");
It would probably be cheaper to use