Java Code Examples for android.icu.lang.UCharacter#toTitleCase()

The following examples show how to use android.icu.lang.UCharacter#toTitleCase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuleBasedNumberFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Adjust capitalization of formatted result for display context
 */
private String adjustForContext(String result) {
    if (result != null && result.length() > 0 && UCharacter.isLowerCase(result.codePointAt(0))) {
        DisplayContext capitalization = getContext(DisplayContext.Type.CAPITALIZATION);
        if (  capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
              (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationForListOrMenu) ||
              (capitalization == DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationForStandAlone) ) {
            if (capitalizationBrkIter == null) {
                // should only happen when deserializing, etc.
                capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
            }
            return UCharacter.toTitleCase(locale, result, capitalizationBrkIter,
                            UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
        }
    }
    return result;
}
 
Example 2
Source File: LocaleDisplayNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private String adjustForUsageAndContext(CapitalizationContextUsage usage, String name) {
    if (name != null && name.length() > 0 && UCharacter.isLowerCase(name.codePointAt(0)) &&
            (capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
            (capitalizationUsage != null && capitalizationUsage[usage.ordinal()]) )) {
        // Note, won't have capitalizationUsage != null && capitalizationUsage[usage.ordinal()]
        // unless capitalization is CAPITALIZATION_FOR_UI_LIST_OR_MENU or CAPITALIZATION_FOR_STANDALONE
        synchronized (this) {
            if (capitalizationBrkIter == null) {
                // should only happen when deserializing, etc.
                capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
            }
            return UCharacter.toTitleCase(locale, name, capitalizationBrkIter,
                    UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
        }
    }
    return name;
}
 
Example 3
Source File: UCharacterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestToTitleCaseCoverage(){
    //Calls the function "toTitleCase(Locale locale, String str, BreakIterator breakiter)"
    String[] locale={"en","fr","zh","ko","ja","it","de",""};
    for(int i=0; i<locale.length; i++){
        UCharacter.toTitleCase(new Locale(locale[i]), "", null);
    }

    // Calls the function "String toTitleCase(ULocale locale, String str, BreakIterator titleIter, int options)"
    // Tests when "if (locale == null)" is true
    UCharacter.toTitleCase((ULocale)null, "", null, 0);

    // TODO: Tests when "if(index==BreakIterator.DONE || index>srcLength)" is true
    // TODO: Tests when "while((c=iter.nextCaseMapCP())>=0 && UCaseProps.NONE==gCsp.getType(c))" is false
    // TODO: Tests when "if(prev<titleStart)" is false
    // TODO: Tests when "if(c<=0xffff)" is false
    // TODO: Tests when "if(c<=0xffff)" is false
    // TODO: Tests when "if(titleLimit<index)" is false
    // TODO: Tests when "else if((nc=iter.nextCaseMapCP())>=0)" is false
}
 
Example 4
Source File: TransliteratorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestCasing() {
    Transliterator toLower = Transliterator.getInstance("lower");
    Transliterator toCasefold = Transliterator.getInstance("casefold");
    Transliterator toUpper = Transliterator.getInstance("upper");
    Transliterator toTitle = Transliterator.getInstance("title");
    for (int i = 0; i < 0x600; ++i) {
        String s = UTF16.valueOf(i);

        String lower = UCharacter.toLowerCase(ULocale.ROOT, s);
        assertEquals("Lowercase", lower, toLower.transform(s));

        String casefold = UCharacter.foldCase(s, true);
        assertEquals("Casefold", casefold, toCasefold.transform(s));

        String title = UCharacter.toTitleCase(ULocale.ROOT, s, null);
        assertEquals("Title", title, toTitle.transform(s));

        String upper = UCharacter.toUpperCase(ULocale.ROOT, s);
        assertEquals("Upper", upper, toUpper.transform(s));
    }
}
 
Example 5
Source File: TransliteratorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestSurrogateCasing () {
    // check that casing handles surrogates
    // titlecase is currently defective
    int dee = UTF16.charAt(DESERET_dee,0);
    int DEE = UCharacter.toTitleCase(dee);
    if (!UTF16.valueOf(DEE).equals(DESERET_DEE)) {
        errln("Fails titlecase of surrogates" + Integer.toString(dee,16) + ", " + Integer.toString(DEE,16));
    }

    if (!UCharacter.toUpperCase(DESERET_dee + DESERET_DEE).equals(DESERET_DEE + DESERET_DEE)) {
        errln("Fails uppercase of surrogates");
    }

    if (!UCharacter.toLowerCase(DESERET_dee + DESERET_DEE).equals(DESERET_dee + DESERET_dee)) {
        errln("Fails lowercase of surrogates");
    }
}
 
Example 6
Source File: RelativeDateTimeFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private String adjustForContext(String originalFormattedString) {
    if (breakIterator == null || originalFormattedString.length() == 0
            || !UCharacter.isLowerCase(UCharacter.codePointAt(originalFormattedString, 0))) {
        return originalFormattedString;
    }
    synchronized (breakIterator) {
        return UCharacter.toTitleCase(
                locale,
                originalFormattedString,
                breakIterator,
                UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
    }
}
 
Example 7
Source File: TitlecaseTransliterator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public void addSourceTargetSet(UnicodeSet inputFilter, UnicodeSet sourceSet, UnicodeSet targetSet) {
    synchronized (this) {
        if (sourceTargetUtility == null) {
            sourceTargetUtility = new SourceTargetUtility(new Transform<String,String>() {
                @Override
                public String transform(String source) {
                    return UCharacter.toTitleCase(locale, source, null);
                }
            });
        }
    }
    sourceTargetUtility.addSourceTargetSet(this, inputFilter, sourceSet, targetSet);
}
 
Example 8
Source File: RelativeDateFormat.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public StringBuffer format(Calendar cal, StringBuffer toAppendTo,
        FieldPosition fieldPosition) {

    String relativeDayString = null;
    DisplayContext capitalizationContext = getContext(DisplayContext.Type.CAPITALIZATION);

    if (fDateStyle != DateFormat.NONE) {
        // calculate the difference, in days, between 'cal' and now.
        int dayDiff = dayDifference(cal);

        // look up string
        relativeDayString = getStringForDay(dayDiff);
    }

    if (fDateTimeFormat != null) {
        if (relativeDayString != null && fDatePattern != null &&
                (fTimePattern == null || fCombinedFormat == null || combinedFormatHasDateAtStart) ) {
            // capitalize relativeDayString according to context for relative, set formatter no context
            if ( relativeDayString.length() > 0 && UCharacter.isLowerCase(relativeDayString.codePointAt(0)) &&
                 (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationOfRelativeUnitsForListOrMenu) ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationOfRelativeUnitsForStandAlone) )) {
                if (capitalizationBrkIter == null) {
                    // should only happen when deserializing, etc.
                    capitalizationBrkIter = BreakIterator.getSentenceInstance(fLocale);
                }
                relativeDayString = UCharacter.toTitleCase(fLocale, relativeDayString, capitalizationBrkIter,
                                UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
            }
            fDateTimeFormat.setContext(DisplayContext.CAPITALIZATION_NONE);
        } else {
            // set our context for the formatter
            fDateTimeFormat.setContext(capitalizationContext);
        }
    }

    if (fDateTimeFormat != null && (fDatePattern != null || fTimePattern != null)) {
        // The new way
        if (fDatePattern == null) {
            // must have fTimePattern
            fDateTimeFormat.applyPattern(fTimePattern);
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        } else if (fTimePattern == null) {
            // must have fDatePattern
            if (relativeDayString != null) {
                toAppendTo.append(relativeDayString);
            } else {
                fDateTimeFormat.applyPattern(fDatePattern);
                fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
            }
        } else {
            String datePattern = fDatePattern; // default;
            if (relativeDayString != null) {
                // Need to quote the relativeDayString to make it a legal date pattern
                datePattern = "'" + relativeDayString.replace("'", "''") + "'";
            }
            StringBuffer combinedPattern = new StringBuffer("");
            fCombinedFormat.format(new Object[] {fTimePattern, datePattern}, combinedPattern, new FieldPosition(0));
            fDateTimeFormat.applyPattern(combinedPattern.toString());
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        }
    } else if (fDateFormat != null) {
        // A subset of the old way, for serialization compatibility
        // (just do the date part)
        if (relativeDayString != null) {
            toAppendTo.append(relativeDayString);
        } else {
            fDateFormat.format(cal, toAppendTo, fieldPosition);
        }
    }

    return toAppendTo;
}
 
Example 9
Source File: UCharacterCompare.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Main testing method
 */
public static void main(String arg[]) {
    System.out.println("Starting character compare");
    try {
        FileWriter f;
        if (arg.length == 0)
            f = new FileWriter("compare.txt");
        else
            f = new FileWriter(arg[0]);
        PrintWriter p = new PrintWriter(f);
        p.print("char  character name                                                           ");
        p.println("method name               ucharacter character");
        for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
            System.out.println("character \\u" + Integer.toHexString(i));
            if (UCharacter.isDefined(i) != Character.isDefined(i))
                trackDifference(p, i, "isDefined()", "" + UCharacter.isDefined(i), "" + Character.isDefined(i));
            else {
                if (UCharacter.digit(i, 10) != Character.digit(i, 10))
                    trackDifference(p, i, "digit()", "" + UCharacter.digit(i, 10), "" + Character.digit(i, 10));
                if (UCharacter.getNumericValue(i) != Character.getNumericValue(i))
                    trackDifference(p, i, "getNumericValue()", "" + UCharacter.getNumericValue(i), ""
                            + Character.getNumericValue(i));
                if (!compareType(UCharacter.getType(i), Character.getType(i)))
                    trackDifference(p, i, "getType()", "" + UCharacter.getType(i), "" + Character.getType(i));
                if (UCharacter.isDigit(i) != Character.isDigit(i))
                    trackDifference(p, i, "isDigit()", "" + UCharacter.isDigit(i), "" + Character.isDigit(i));
                if (UCharacter.isISOControl(i) != Character.isISOControl(i))
                    trackDifference(p, i, "isISOControl()", "" + UCharacter.isISOControl(i), ""
                            + Character.isISOControl(i));
                if (UCharacter.isLetter(i) != Character.isLetter(i))
                    trackDifference(p, i, "isLetter()", "" + UCharacter.isLetter(i), "" + Character.isLetter(i));
                if (UCharacter.isLetterOrDigit(i) != Character.isLetterOrDigit(i))
                    trackDifference(p, i, "isLetterOrDigit()", "" + UCharacter.isLetterOrDigit(i), ""
                            + Character.isLetterOrDigit(i));
                if (UCharacter.isLowerCase(i) != Character.isLowerCase(i))
                    trackDifference(p, i, "isLowerCase()", "" + UCharacter.isLowerCase(i), ""
                            + Character.isLowerCase(i));
                if (UCharacter.isWhitespace(i) != Character.isWhitespace(i))
                    trackDifference(p, i, "isWhitespace()", "" + UCharacter.isWhitespace(i), ""
                            + Character.isWhitespace(i));
                if (UCharacter.isSpaceChar(i) != Character.isSpaceChar(i))
                    trackDifference(p, i, "isSpaceChar()", "" + UCharacter.isSpaceChar(i), ""
                            + Character.isSpaceChar(i));
                if (UCharacter.isTitleCase(i) != Character.isTitleCase(i))
                    trackDifference(p, i, "isTitleChar()", "" + UCharacter.isTitleCase(i), ""
                            + Character.isTitleCase(i));
                if (UCharacter.isUnicodeIdentifierPart(i) != Character.isUnicodeIdentifierPart(i))
                    trackDifference(p, i, "isUnicodeIdentifierPart()", "" + UCharacter.isUnicodeIdentifierPart(i),
                            "" + Character.isUnicodeIdentifierPart(i));
                if (UCharacter.isUnicodeIdentifierStart(i) != Character.isUnicodeIdentifierStart(i))
                    trackDifference(p, i, "isUnicodeIdentifierStart()",
                            "" + UCharacter.isUnicodeIdentifierStart(i), "" + Character.isUnicodeIdentifierStart(i));
                if (UCharacter.isIdentifierIgnorable(i) != Character.isIdentifierIgnorable(i))
                    trackDifference(p, i, "isIdentifierIgnorable()", "" + UCharacter.isIdentifierIgnorable(i), ""
                            + Character.isIdentifierIgnorable(i));
                if (UCharacter.isUpperCase(i) != Character.isUpperCase(i))
                    trackDifference(p, i, "isUpperCase()", "" + UCharacter.isUpperCase(i), ""
                            + Character.isUpperCase(i));
                if (UCharacter.toLowerCase(i) != Character.toLowerCase(i))
                    trackDifference(p, i, "toLowerCase()", Integer.toHexString(UCharacter.toLowerCase(i)), Integer
                            .toHexString(Character.toLowerCase(i)));
                if (!UCharacter.toString(i).equals(new Character(i).toString()))
                    trackDifference(p, i, "toString()", UCharacter.toString(i), new Character(i).toString());
                if (UCharacter.toTitleCase(i) != Character.toTitleCase(i))
                    trackDifference(p, i, "toTitleCase()", Integer.toHexString(UCharacter.toTitleCase(i)), Integer
                            .toHexString(Character.toTitleCase(i)));
                if (UCharacter.toUpperCase(i) != Character.toUpperCase(i))
                    trackDifference(p, i, "toUpperCase()", Integer.toHexString(UCharacter.toUpperCase(i)), Integer
                            .toHexString(Character.toUpperCase(i)));
            }
        }
        summary(p);
        p.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: UCharacterCaseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Testing the uppercase and lowercase function of UCharacter
 */
@Test
public void TestCharacter()
{
    for (int i = 0; i < CHARACTER_LOWER_.length; i ++) {
        if (UCharacter.isLetter(CHARACTER_LOWER_[i]) &&
            !UCharacter.isLowerCase(CHARACTER_LOWER_[i])) {
            errln("FAIL isLowerCase test for \\u" +
                  hex(CHARACTER_LOWER_[i]));
            break;
        }
        if (UCharacter.isLetter(CHARACTER_UPPER_[i]) &&
            !(UCharacter.isUpperCase(CHARACTER_UPPER_[i]) ||
              UCharacter.isTitleCase(CHARACTER_UPPER_[i]))) {
            errln("FAIL isUpperCase test for \\u" +
                  hex(CHARACTER_UPPER_[i]));
            break;
        }
        if (CHARACTER_LOWER_[i] !=
            UCharacter.toLowerCase(CHARACTER_UPPER_[i]) ||
            (CHARACTER_UPPER_[i] !=
            UCharacter.toUpperCase(CHARACTER_LOWER_[i]) &&
            CHARACTER_UPPER_[i] !=
            UCharacter.toTitleCase(CHARACTER_LOWER_[i]))) {
            errln("FAIL case conversion test for \\u" +
                  hex(CHARACTER_UPPER_[i]) +
                  " to \\u" + hex(CHARACTER_LOWER_[i]));
            break;
        }
        if (CHARACTER_LOWER_[i] !=
            UCharacter.toLowerCase(CHARACTER_LOWER_[i])) {
            errln("FAIL lower case conversion test for \\u" +
                  hex(CHARACTER_LOWER_[i]));
            break;
        }
        if (CHARACTER_UPPER_[i] !=
            UCharacter.toUpperCase(CHARACTER_UPPER_[i]) &&
            CHARACTER_UPPER_[i] !=
            UCharacter.toTitleCase(CHARACTER_UPPER_[i])) {
            errln("FAIL upper case conversion test for \\u" +
                  hex(CHARACTER_UPPER_[i]));
            break;
        }
        logln("Ok    \\u" + hex(CHARACTER_UPPER_[i]) + " and \\u" +
              hex(CHARACTER_LOWER_[i]));
    }
}
 
Example 11
Source File: UCharacterCaseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestTitle()
{
     try{
        for (int i = 0; i < TITLE_DATA_.length;) {
            String test = TITLE_DATA_[i++];
            String expected = TITLE_DATA_[i++];
            ULocale locale = new ULocale(TITLE_DATA_[i++]);
            int breakType = Integer.parseInt(TITLE_DATA_[i++]);
            String optionsString = TITLE_DATA_[i++];
            BreakIterator iter =
                breakType >= 0 ?
                    BreakIterator.getBreakInstance(locale, breakType) :
                    breakType == -2 ?
                        // Open a trivial break iterator that only delivers { 0, length }
                        // or even just { 0 } as boundaries.
                        new RuleBasedBreakIterator(".*;") :
                        null;
            int options = 0;
            if (optionsString.indexOf('L') >= 0) {
                options |= UCharacter.TITLECASE_NO_LOWERCASE;
            }
            if (optionsString.indexOf('A') >= 0) {
                options |= UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT;
            }
            String result = UCharacter.toTitleCase(locale, test, iter, options);
            if (!expected.equals(result)) {
                errln("titlecasing for " + prettify(test) + " (options " + options + ") should be " +
                      prettify(expected) + " but got " +
                      prettify(result));
            }
            if (options == 0) {
                result = UCharacter.toTitleCase(locale, test, iter);
                if (!expected.equals(result)) {
                    errln("titlecasing for " + prettify(test) + " should be " +
                          prettify(expected) + " but got " +
                          prettify(result));
                }
            }
        }
     }catch(Exception ex){
        warnln("Could not find data for BreakIterators");
     }
}
 
Example 12
Source File: UCharacterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void testToTitleCase_Locale_String_BreakIterator_I() {
    String titleCase = UCharacter.toTitleCase(new Locale("nl"), "ijsland", null,
            UCharacter.FOLD_CASE_DEFAULT);
    assertEquals("Wrong title casing", "IJsland", titleCase);
}
 
Example 13
Source File: UCharacterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void testToTitleCase_String_BreakIterator_en() {
    String titleCase = UCharacter.toTitleCase(new Locale("en"), "ijsland", null);
    assertEquals("Wrong title casing", "Ijsland", titleCase);
}