Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,90 @@ public static UTF8String lowercaseReplace(final UTF8String src, final UTF8String
return buf.build();
}

/**
* Convert the input string to uppercase using the ICU root locale rules.
*
* @param target the input string
* @return the uppercase string
*/
public static UTF8String toUpperCase(final UTF8String target) {
return UTF8String.fromString(toUpperCase(target.toString()));
}
public static String toUpperCase(final String target) {
return UCharacter.toUpperCase(target);
}

/**
* Convert the input string to uppercase using the specified ICU collation rules.
*
* @param target the input string
* @return the uppercase string
*/
public static UTF8String toUpperCase(final UTF8String target, final int collationId) {
return UTF8String.fromString(toUpperCase(target.toString(), collationId));
}
public static String toUpperCase(final String target, final int collationId) {
ULocale locale = CollationFactory.fetchCollation(collationId)
.collator.getLocale(ULocale.ACTUAL_LOCALE);
return UCharacter.toUpperCase(locale, target);
}

/**
* Convert the input string to lowercase using the ICU root locale rules.
*
* @param target the input string
* @return the lowercase string
*/
public static UTF8String toLowerCase(final UTF8String target) {
return UTF8String.fromString(toLowerCase(target.toString()));
}
public static String toLowerCase(final String target) {
return UCharacter.toLowerCase(target);
}

/**
* Convert the input string to lowercase using the specified ICU collation rules.
*
* @param target the input string
* @return the lowercase string
*/
public static UTF8String toLowerCase(final UTF8String target, final int collationId) {
return UTF8String.fromString(toLowerCase(target.toString(), collationId));
}
public static String toLowerCase(final String target, final int collationId) {
ULocale locale = CollationFactory.fetchCollation(collationId)
.collator.getLocale(ULocale.ACTUAL_LOCALE);
return UCharacter.toLowerCase(locale, target);
}

/**
* Convert the input string to lowercase using the ICU root locale rules.
*
* @param target the input string
* @return the lowercase string
*/
public static UTF8String toTitleCase(final UTF8String target) {
return UTF8String.fromString(toTitleCase(target.toString()));
}
public static String toTitleCase(final String target) {
BreakIterator wordIterator = BreakIterator.getWordInstance();
return UCharacter.toTitleCase(target, wordIterator);
}

/**
* Convert the input string to lowercase using the specified ICU collation rules.
*
* @param target the input string
* @return the lowercase string
*/
public static UTF8String toTitleCase(final UTF8String target, final int collationId) {
return UTF8String.fromString(toTitleCase(target.toString(), collationId));
}
public static String toTitleCase(final String target, final int collationId) {
ULocale locale = CollationFactory.fetchCollation(collationId)
.collator.getLocale(ULocale.ACTUAL_LOCALE);
return UCharacter.toTitleCase(locale, target, BreakIterator.getWordInstance(locale));
BreakIterator wordIterator = BreakIterator.getWordInstance(locale);
return UCharacter.toTitleCase(locale, target, wordIterator);
}

public static int findInSet(final UTF8String match, final UTF8String set, int collationId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,87 +208,99 @@ public static boolean execICU(final UTF8String l, final UTF8String r,
public static class Upper {
public static UTF8String exec(final UTF8String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return execUTF8(v);
} else {
} else if (collation.supportsLowercaseEquality) {
return execLowercase(v);
} else {
return execICU(v, collationId);
}
}
public static String genCode(final String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
String expr = "CollationSupport.Upper.exec";
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return String.format(expr + "UTF8(%s)", v);
} else {
} else if (collation.supportsLowercaseEquality) {
return String.format(expr + "Lowercase(%s)", v);
} else {
return String.format(expr + "ICU(%s, %d)", v, collationId);
}
}
public static UTF8String execUTF8(final UTF8String v) {
return v.toUpperCase();
}
public static UTF8String execLowercase(final UTF8String v) {
return CollationAwareUTF8String.toUpperCase(v);
}
public static UTF8String execICU(final UTF8String v, final int collationId) {
return UTF8String.fromString(CollationAwareUTF8String.toUpperCase(v.toString(), collationId));
return CollationAwareUTF8String.toUpperCase(v, collationId);
}
}

public static class Lower {
public static UTF8String exec(final UTF8String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return execUTF8(v);
} else if (collation.supportsLowercaseEquality) {
return execLowercase(v);
} else {
return execICU(v, collationId);
}
}
public static String genCode(final String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
String expr = "CollationSupport.Lower.exec";
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return String.format(expr + "UTF8(%s)", v);
} else {
} else if (collation.supportsLowercaseEquality) {
return String.format(expr + "Lowercase(%s)", v);
} else {
return String.format(expr + "ICU(%s, %d)", v, collationId);
}
}
public static UTF8String execUTF8(final UTF8String v) {
return v.toLowerCase();
}
public static UTF8String execLowercase(final UTF8String v) {
return CollationAwareUTF8String.toLowerCase(v);
}
public static UTF8String execICU(final UTF8String v, final int collationId) {
return UTF8String.fromString(CollationAwareUTF8String.toLowerCase(v.toString(), collationId));
return CollationAwareUTF8String.toLowerCase(v, collationId);
}
}

public static class InitCap {
public static UTF8String exec(final UTF8String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return execUTF8(v);
} else if (collation.supportsLowercaseEquality) {
return execLowercase(v);
} else {
return execICU(v, collationId);
}
}

public static String genCode(final String v, final int collationId) {
CollationFactory.Collation collation = CollationFactory.fetchCollation(collationId);
String expr = "CollationSupport.InitCap.exec";
if (collation.supportsBinaryEquality || collation.supportsLowercaseEquality) {
if (collation.supportsBinaryEquality) {
return String.format(expr + "UTF8(%s)", v);
} else if (collation.supportsLowercaseEquality) {
return String.format(expr + "Lowercase(%s)", v);
} else {
return String.format(expr + "ICU(%s, %d)", v, collationId);
}
}

public static UTF8String execUTF8(final UTF8String v) {
return v.toLowerCase().toTitleCase();
}

public static UTF8String execLowercase(final UTF8String v) {
return CollationAwareUTF8String.toTitleCase(v);
}
public static UTF8String execICU(final UTF8String v, final int collationId) {
return UTF8String.fromString(
CollationAwareUTF8String.toTitleCase(
CollationAwareUTF8String.toLowerCase(
v.toString(),
collationId
),
collationId));
return CollationAwareUTF8String.toTitleCase(v, collationId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import static org.junit.jupiter.api.Assertions.*;


// checkstyle.off: AvoidEscapedUnicodeCharacters
public class CollationSupportSuite {

/**
Expand Down Expand Up @@ -526,10 +526,10 @@ public void testInitCap() throws SparkException {
assertInitCap("aB 世 de", "UNICODE_CI", "Ab 世 De");
assertInitCap("ÄBĆΔE", "UNICODE_CI", "Äbćδe");
// Case-variable character length
assertInitCap("İo", "UTF8_BINARY", "İo");
assertInitCap("İo", "UTF8_BINARY_LCASE", "İo");
assertInitCap("İo", "UNICODE", "İo");
assertInitCap("İo", "UNICODE_CI", "İo");
assertInitCap("İo", "UTF8_BINARY", "I\u0307o");
assertInitCap("İo", "UTF8_BINARY_LCASE", "İo");
assertInitCap("İo", "UNICODE", "I\u0307o");
assertInitCap("İo", "UNICODE_CI", "İo");
}

private void assertStringInstr(String string, String substring, String collationName,
Expand Down Expand Up @@ -1008,3 +1008,4 @@ public void testStringTrim() throws SparkException {
// TODO: Test other collation-aware expressions.

}
// checkstyle.on: AvoidEscapedUnicodeCharacters