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

The following examples show how to use android.icu.lang.UCharacter#foldCase() . 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: UCharacterProperty.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
boolean contains(int c) {
    String nfd=Norm2AllModes.getNFCInstance().impl.getDecomposition(c);
    if(nfd!=null) {
        /* c has a decomposition */
        c=nfd.codePointAt(0);
        if(Character.charCount(c)!=nfd.length()) {
            /* multiple code points */
            c=-1;
        }
    } else if(c<0) {
        return false;  /* protect against bad input */
    }
    if(c>=0) {
        /* single code point */
        UCaseProps csp=UCaseProps.INSTANCE;
        UCaseProps.dummyStringBuilder.setLength(0);
        return csp.toFullFolding(c, UCaseProps.dummyStringBuilder,
                                 UCharacter.FOLD_CASE_DEFAULT)>=0;
    } else {
        String folded=UCharacter.foldCase(nfd, true);
        return !folded.equals(nfd);
    }
}
 
Example 2
Source File: BasicTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private int ref_case_compare(String s1, String s2, int options) {
    String t1, t2;

    t1=s1;
    t2=s2;

    t1 = UCharacter.foldCase(t1,((options&Normalizer.FOLD_CASE_EXCLUDE_SPECIAL_I)==0));
    t2 = UCharacter.foldCase(t2,((options&Normalizer.FOLD_CASE_EXCLUDE_SPECIAL_I)==0));

    if((options&Normalizer.COMPARE_CODE_POINT_ORDER)!=0) {
        UTF16.StringComparator comp
                = new UTF16.StringComparator(true, false,
                                UTF16.StringComparator.FOLD_CASE_DEFAULT);
        return comp.compare(t1,t2);
    } else {
        return t1.compareTo(t2);
    }

}
 
Example 3
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 4
Source File: CaseFoldTransliterator.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 (UppercaseTransliterator.class) {
        if (sourceTargetUtility == null) {
            sourceTargetUtility = new SourceTargetUtility(new Transform<String,String>() {
                @Override
                public String transform(String source) {
                    return UCharacter.foldCase(source, true);
                }
            });
        }
    }
    sourceTargetUtility.addSourceTargetSet(this, inputFilter, sourceSet, targetSet);
}
 
Example 5
Source File: BasicTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private int ref_norm_compare(String s1, String s2, int options) {
    String t1, t2,r1,r2;

    int normOptions=options>>Normalizer.COMPARE_NORM_OPTIONS_SHIFT;

    if((options&Normalizer.COMPARE_IGNORE_CASE)!=0) {
        // NFD(toCasefold(NFD(X))) = NFD(toCasefold(NFD(Y)))
        r1 = Normalizer.decompose(s1,false,normOptions);
        r2 = Normalizer.decompose(s2,false,normOptions);
        r1 = UCharacter.foldCase(r1,options);
        r2 = UCharacter.foldCase(r2,options);
    }else{
        r1 = s1;
        r2 = s2;
    }

    t1 = Normalizer.decompose(r1, false, normOptions);
    t2 = Normalizer.decompose(r2, false, normOptions);

    if((options&Normalizer.COMPARE_CODE_POINT_ORDER)!=0) {
        UTF16.StringComparator comp
                = new UTF16.StringComparator(true, false,
                                 UTF16.StringComparator.FOLD_CASE_DEFAULT);
        return comp.compare(t1,t2);
    } else {
        return t1.compareTo(t2);
    }

}
 
Example 6
Source File: CaseInsensitiveString.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static String foldCase(String foldee)
{
    return UCharacter.foldCase(foldee, true);
}
 
Example 7
Source File: BasicTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
int countFoldFCDExceptions(int foldingOptions) {
    String s, d;
    int c;
    int count;
    int/*unsigned*/ cc, trailCC, foldCC, foldTrailCC;
    Normalizer.QuickCheckResult qcResult;
    int category;
    boolean isNFD;


    logln("Test if case folding may un-FCD a string (folding options 0x)"+hex(foldingOptions));

    count=0;
    for(c=0; c<=0x10ffff; ++c) {
        category=UCharacter.getType(c);
        if(category==UCharacterCategory.UNASSIGNED) {
            continue; // skip unassigned code points
        }
        if(c==0xac00) {
            c=0xd7a3; // skip Hangul - no case folding there
            continue;
        }
        // skip Han blocks - no case folding there either
        if(c==0x3400) {
            c=0x4db5;
            continue;
        }
        if(c==0x4e00) {
            c=0x9fa5;
            continue;
        }
        if(c==0x20000) {
            c=0x2a6d6;
            continue;
        }

        s= UTF16.valueOf(c);

        // get leading and trailing cc for c
        d= Normalizer.decompose(s,false);
        isNFD= s==d;
        cc=UCharacter.getCombiningClass(UTF16.charAt(d,0));
        trailCC=UCharacter.getCombiningClass(UTF16.charAt(d,d.length()-1));

        // get leading and trailing cc for the case-folding of c
        UCharacter.foldCase(s,(foldingOptions==0));
        d = Normalizer.decompose(s, false);
        foldCC=UCharacter.getCombiningClass(UTF16.charAt(d,0));
        foldTrailCC=UCharacter.getCombiningClass(UTF16.charAt(d,d.length()-1));

        qcResult=Normalizer.quickCheck(s, Normalizer.FCD,0);


        // bad:
        // - character maps to empty string: adjacent characters may then need reordering
        // - folding has different leading/trailing cc's, and they don't become just 0
        // - folding itself is not FCD
        if( qcResult!=Normalizer.YES ||
            s.length()==0 ||
            (cc!=foldCC && foldCC!=0) || (trailCC!=foldTrailCC && foldTrailCC!=0)
        ) {
            ++count;
            errln("U+"+hex(c)+": case-folding may un-FCD a string (folding options 0x"+hex(foldingOptions)+")");
            //errln("  cc %02x trailCC %02x    foldCC(U+%04lx) %02x foldTrailCC(U+%04lx) %02x   quickCheck(folded)=%d", cc, trailCC, UTF16.charAt(d,0), foldCC, UTF16.charAt(d,d.length()-1), foldTrailCC, qcResult);
            continue;
        }

        // also bad:
        // if a code point is in NFD but its case folding is not, then
        // unorm_compare will also fail
        if(isNFD && Normalizer.YES!=Normalizer.quickCheck(s, Normalizer.NFD,0)) {
            ++count;
            errln("U+"+hex(c)+": case-folding may un-FCD a string (folding options 0x"+hex(foldingOptions)+")");
        }
    }

    logln("There are "+hex(count)+" code points for which case-folding may un-FCD a string (folding options"+foldingOptions+"x)" );
    return count;
}
 
Example 8
Source File: UCharacterCaseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestFolding()
{
    // test simple case folding
    for (int i = 0; i < FOLDING_SIMPLE_.length; i += 3) {
        if (UCharacter.foldCase(FOLDING_SIMPLE_[i], true) !=
            FOLDING_SIMPLE_[i + 1]) {
            errln("FAIL: foldCase(\\u" + hex(FOLDING_SIMPLE_[i]) +
                  ", true) should be \\u" + hex(FOLDING_SIMPLE_[i + 1]));
        }
        if (UCharacter.foldCase(FOLDING_SIMPLE_[i],
                                UCharacter.FOLD_CASE_DEFAULT) !=
                                                  FOLDING_SIMPLE_[i + 1]) {
            errln("FAIL: foldCase(\\u" + hex(FOLDING_SIMPLE_[i]) +
                  ", UCharacter.FOLD_CASE_DEFAULT) should be \\u"
                  + hex(FOLDING_SIMPLE_[i + 1]));
        }
        if (UCharacter.foldCase(FOLDING_SIMPLE_[i], false) !=
            FOLDING_SIMPLE_[i + 2]) {
            errln("FAIL: foldCase(\\u" + hex(FOLDING_SIMPLE_[i]) +
                  ", false) should be \\u" + hex(FOLDING_SIMPLE_[i + 2]));
        }
        if (UCharacter.foldCase(FOLDING_SIMPLE_[i],
                                UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I) !=
                                FOLDING_SIMPLE_[i + 2]) {
            errln("FAIL: foldCase(\\u" + hex(FOLDING_SIMPLE_[i]) +
                  ", UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I) should be \\u"
                  + hex(FOLDING_SIMPLE_[i + 2]));
        }
    }

    // Test full string case folding with default option and separate
    // buffers
    if (!FOLDING_DEFAULT_[0].equals(UCharacter.foldCase(FOLDING_MIXED_[0], true))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[0]) +
              ", true)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[0], true)) +
              " should be " + prettify(FOLDING_DEFAULT_[0]));
    }

    if (!FOLDING_DEFAULT_[0].equals(UCharacter.foldCase(FOLDING_MIXED_[0], UCharacter.FOLD_CASE_DEFAULT))) {
                errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[0]) +
                      ", UCharacter.FOLD_CASE_DEFAULT)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[0], UCharacter.FOLD_CASE_DEFAULT))
                      + " should be " + prettify(FOLDING_DEFAULT_[0]));
            }

    if (!FOLDING_EXCLUDE_SPECIAL_I_[0].equals(
                        UCharacter.foldCase(FOLDING_MIXED_[0], false))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[0]) +
              ", false)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[0], false))
              + " should be " + prettify(FOLDING_EXCLUDE_SPECIAL_I_[0]));
    }

    if (!FOLDING_EXCLUDE_SPECIAL_I_[0].equals(
                                UCharacter.foldCase(FOLDING_MIXED_[0], UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[0]) +
              ", UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[0], UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I))
              + " should be " + prettify(FOLDING_EXCLUDE_SPECIAL_I_[0]));
    }

    if (!FOLDING_DEFAULT_[1].equals(UCharacter.foldCase(FOLDING_MIXED_[1], true))) {
       errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[1]) +
             ", true)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[1], true))
             + " should be " + prettify(FOLDING_DEFAULT_[1]));
    }

    if (!FOLDING_DEFAULT_[1].equals(UCharacter.foldCase(FOLDING_MIXED_[1], UCharacter.FOLD_CASE_DEFAULT))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[1]) +
                     ", UCharacter.FOLD_CASE_DEFAULT)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[1], UCharacter.FOLD_CASE_DEFAULT))
                     + " should be " + prettify(FOLDING_DEFAULT_[1]));
    }

    // alternate handling for dotted I/dotless i (U+0130, U+0131)
    if (!FOLDING_EXCLUDE_SPECIAL_I_[1].equals(
                    UCharacter.foldCase(FOLDING_MIXED_[1], false))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[1]) +
              ", false)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[1], false))
              + " should be " + prettify(FOLDING_EXCLUDE_SPECIAL_I_[1]));
    }

    if (!FOLDING_EXCLUDE_SPECIAL_I_[1].equals(
                            UCharacter.foldCase(FOLDING_MIXED_[1], UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I))) {
        errln("FAIL: foldCase(" + prettify(FOLDING_MIXED_[1]) +
              ", UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I)=" + prettify(UCharacter.foldCase(FOLDING_MIXED_[1], UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I))
              + " should be "
              + prettify(FOLDING_EXCLUDE_SPECIAL_I_[1]));
    }
}