am 2e4352c5: am b5d5190d: Merge "Add some utility functions."
* commit '2e4352c53b48366c40d43b32e8acdc28acac2e6f': Add some utility functions.main
commit
27dede24a6
|
@ -222,4 +222,75 @@ public final class StringUtils {
|
|||
if (1 == capsCount) return CAPITALIZE_FIRST;
|
||||
return (letterCount == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE);
|
||||
}
|
||||
|
||||
public static boolean isIdenticalAfterUpcase(final String text) {
|
||||
final int len = text.length();
|
||||
for (int i = 0; i < len; i = text.offsetByCodePoints(i, 1)) {
|
||||
final int codePoint = text.codePointAt(i);
|
||||
if (Character.isLetter(codePoint) && !Character.isUpperCase(codePoint)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isIdenticalAfterDowncase(final String text) {
|
||||
final int len = text.length();
|
||||
for (int i = 0; i < len; i = text.offsetByCodePoints(i, 1)) {
|
||||
final int codePoint = text.codePointAt(i);
|
||||
if (Character.isLetter(codePoint) && !Character.isLowerCase(codePoint)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isIdenticalAfterCapitalizeEachWord(final String text,
|
||||
final String separators) {
|
||||
boolean needCapsNext = true;
|
||||
final int len = text.length();
|
||||
for (int i = 0; i < len; i = text.offsetByCodePoints(i, 1)) {
|
||||
final int codePoint = text.codePointAt(i);
|
||||
if (Character.isLetter(codePoint)) {
|
||||
if ((needCapsNext && !Character.isUpperCase(codePoint))
|
||||
|| (!needCapsNext && !Character.isLowerCase(codePoint))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// We need a capital letter next if this is a separator.
|
||||
needCapsNext = (-1 != separators.indexOf(codePoint));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: like capitalizeFirst*, this does not work perfectly for Dutch because of the IJ digraph
|
||||
// which should be capitalized together in *some* cases.
|
||||
public static String capitalizeEachWord(final String text, final String separators,
|
||||
final Locale locale) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
boolean needCapsNext = true;
|
||||
final int len = text.length();
|
||||
for (int i = 0; i < len; i = text.offsetByCodePoints(i, 1)) {
|
||||
final String nextChar = text.substring(i, text.offsetByCodePoints(i, 1));
|
||||
if (needCapsNext) {
|
||||
builder.append(nextChar.toUpperCase(locale));
|
||||
} else {
|
||||
builder.append(nextChar.toLowerCase(locale));
|
||||
}
|
||||
// We need a capital letter next if this is a separator.
|
||||
needCapsNext = (-1 != separators.indexOf(nextChar.codePointAt(0)));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static boolean containsAny(final String string, final String separators) {
|
||||
final int len = separators.length();
|
||||
for (int i = 0; i < len; i = separators.offsetByCodePoints(i, 1)) {
|
||||
final int separator = separators.codePointAt(i);
|
||||
if (-1 != string.indexOf(separator)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -154,4 +154,83 @@ public class StringUtilsTests extends AndroidTestCase {
|
|||
assertEquals(StringUtils.CAPITALIZE_NONE,
|
||||
StringUtils.getCapitalizationType(""));
|
||||
}
|
||||
|
||||
public void testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase() {
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("capitalize"));
|
||||
assertTrue(StringUtils.isIdenticalAfterDowncase("capitalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("cApITalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase("cApITalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("capitalizE"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase("capitalizE"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("__c a piu$@tali56ze"));
|
||||
assertTrue(StringUtils.isIdenticalAfterDowncase("__c a piu$@tali56ze"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("A__c a piu$@tali56ze"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase("A__c a piu$@tali56ze"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase("Capitalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase("Capitalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterUpcase(" Capitalize"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase(" Capitalize"));
|
||||
assertTrue(StringUtils.isIdenticalAfterUpcase("CAPITALIZE"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase("CAPITALIZE"));
|
||||
assertTrue(StringUtils.isIdenticalAfterUpcase(" PI26LIE"));
|
||||
assertFalse(StringUtils.isIdenticalAfterDowncase(" PI26LIE"));
|
||||
assertTrue(StringUtils.isIdenticalAfterUpcase(""));
|
||||
assertTrue(StringUtils.isIdenticalAfterDowncase(""));
|
||||
}
|
||||
|
||||
private void checkCapitalize(final String src, final String dst, final String separators,
|
||||
final Locale locale) {
|
||||
assertEquals(dst, StringUtils.capitalizeEachWord(src, separators, locale));
|
||||
assert(src.equals(dst)
|
||||
== StringUtils.isIdenticalAfterCapitalizeEachWord(src, separators));
|
||||
}
|
||||
|
||||
public void testCapitalizeEachWord() {
|
||||
checkCapitalize("", "", " ", Locale.ENGLISH);
|
||||
checkCapitalize("test", "Test", " ", Locale.ENGLISH);
|
||||
checkCapitalize(" test", " Test", " ", Locale.ENGLISH);
|
||||
checkCapitalize("Test", "Test", " ", Locale.ENGLISH);
|
||||
checkCapitalize(" Test", " Test", " ", Locale.ENGLISH);
|
||||
checkCapitalize(".Test", ".test", " ", Locale.ENGLISH);
|
||||
checkCapitalize(".Test", ".Test", " .", Locale.ENGLISH);
|
||||
checkCapitalize(".Test", ".Test", ". ", Locale.ENGLISH);
|
||||
checkCapitalize("test and retest", "Test And Retest", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test and retest", "Test And Retest", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test And Retest", "Test And Retest", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test And.Retest ", "Test And.Retest ", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test And.retest ", "Test And.Retest ", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test And.retest ", "Test And.retest ", " ", Locale.ENGLISH);
|
||||
checkCapitalize("Test And.Retest ", "Test And.retest ", " ", Locale.ENGLISH);
|
||||
checkCapitalize("test and ietest", "Test And İetest", " .", new Locale("tr"));
|
||||
checkCapitalize("test and ietest", "Test And Ietest", " .", Locale.ENGLISH);
|
||||
checkCapitalize("Test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
|
||||
checkCapitalize("Test&retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
|
||||
checkCapitalize("test&Retest", "Test&Retest", " \n.!?*()&", Locale.ENGLISH);
|
||||
checkCapitalize("rest\nrecreation! And in the end...",
|
||||
"Rest\nRecreation! And In The End...", " \n.!?*,();&", Locale.ENGLISH);
|
||||
checkCapitalize("lorem ipsum dolor sit amet", "Lorem Ipsum Dolor Sit Amet",
|
||||
" \n.,!?*()&;", Locale.ENGLISH);
|
||||
checkCapitalize("Lorem!Ipsum (Dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
|
||||
" \n,.;!?*()&", Locale.ENGLISH);
|
||||
checkCapitalize("Lorem!Ipsum (dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
|
||||
" \n,.;!?*()&", Locale.ENGLISH);
|
||||
}
|
||||
|
||||
public void testContainsAny() {
|
||||
assertFalse(StringUtils.containsAny("", " "));
|
||||
assertFalse(StringUtils.containsAny("test and retest", ""));
|
||||
assertTrue(StringUtils.containsAny("test and retest", "x3iq o"));
|
||||
assertTrue(StringUtils.containsAny("test and retest", "x3iqo "));
|
||||
assertTrue(StringUtils.containsAny("test and retest", " x3iqo"));
|
||||
assertFalse(StringUtils.containsAny("test and retest", "x3iqo"));
|
||||
assertTrue(StringUtils.containsAny("test and retest", "tse "));
|
||||
assertTrue(StringUtils.containsAny("test and retest.", ".?()"));
|
||||
assertFalse(StringUtils.containsAny("test and retest", ".?()"));
|
||||
// Surrogate pair
|
||||
assertTrue(StringUtils.containsAny("test and \uD861\uDED7 retest.", "\uD861\uDED7"));
|
||||
// Ill-formed string
|
||||
assertFalse(StringUtils.containsAny("test and \uD861 retest.", "\uD861\uDED7"));
|
||||
// Ill-formed string
|
||||
assertFalse(StringUtils.containsAny("test and \uDED7 retest.", "\uD861\uDED7"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue