Search in sources :

Example 16 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestToRules.

@Test
public void TestToRules() {
    String RBT = "rbt";
    String SET = "set";
    String[] DATA = { RBT, "$a=\\u4E61; [$a] > A;", "[\\u4E61] > A;", RBT, "$white=[[:Zs:][:Zl:]]; $white{a} > A;", "[[:Zs:][:Zl:]]{a} > A;", SET, "[[:Zs:][:Zl:]]", "[[:Zs:][:Zl:]]", SET, "[:Ps:]", "[:Ps:]", SET, "[:L:]", "[:L:]", SET, "[[:L:]-[A]]", "[[:L:]-[A]]", SET, "[~[:Lu:][:Ll:]]", "[~[:Lu:][:Ll:]]", SET, "[~[a-z]]", "[~[a-z]]", RBT, "$white=[:Zs:]; $black=[^$white]; $black{a} > A;", "[^[:Zs:]]{a} > A;", RBT, "$a=[:Zs:]; $b=[[a-z]-$a]; $b{a} > A;", "[[a-z]-[:Zs:]]{a} > A;", RBT, "$a=[:Zs:]; $b=[$a&[a-z]]; $b{a} > A;", "[[:Zs:]&[a-z]]{a} > A;", RBT, "$a=[:Zs:]; $b=[x$a]; $b{a} > A;", "[x[:Zs:]]{a} > A;", RBT, "$accentMinus = [ [\\u0300-\\u0345] & [:M:] - [\\u0338]] ;" + "$macron = \\u0304 ;" + "$evowel = [aeiouyAEIOUY] ;" + "$iotasub = \\u0345 ;" + "($evowel $macron $accentMinus *) i > | $1 $iotasub ;", "([AEIOUYaeiouy]\\u0304[[\\u0300-\\u0345]&[:M:]-[\\u0338]]*)i > | $1 \\u0345;", RBT, "([AEIOUYaeiouy]\\u0304[[:M:]-[\\u0304\\u0345]]*)i > | $1 \\u0345;", "([AEIOUYaeiouy]\\u0304[[:M:]-[\\u0304\\u0345]]*)i > | $1 \\u0345;" };
    for (int d = 0; d < DATA.length; d += 3) {
        if (DATA[d] == RBT) {
            // Transliterator test
            Transliterator t = Transliterator.createFromRules("ID", DATA[d + 1], Transliterator.FORWARD);
            if (t == null) {
                errln("FAIL: createFromRules failed");
                return;
            }
            String rules, escapedRules;
            rules = t.toRules(false);
            escapedRules = t.toRules(true);
            String expRules = Utility.unescape(DATA[d + 2]);
            String expEscapedRules = DATA[d + 2];
            if (rules.equals(expRules)) {
                logln("Ok: " + DATA[d + 1] + " => " + Utility.escape(rules));
            } else {
                errln("FAIL: " + DATA[d + 1] + " => " + Utility.escape(rules + ", exp " + expRules));
            }
            if (escapedRules.equals(expEscapedRules)) {
                logln("Ok: " + DATA[d + 1] + " => " + escapedRules);
            } else {
                errln("FAIL: " + DATA[d + 1] + " => " + escapedRules + ", exp " + expEscapedRules);
            }
        } else {
            // UnicodeSet test
            String pat = DATA[d + 1];
            String expToPat = DATA[d + 2];
            UnicodeSet set = new UnicodeSet(pat);
            // Adjust spacing etc. as necessary.
            String toPat;
            toPat = set.toPattern(true);
            if (expToPat.equals(toPat)) {
                logln("Ok: " + pat + " => " + toPat);
            } else {
                errln("FAIL: " + pat + " => " + Utility.escape(toPat) + ", exp " + Utility.escape(pat));
            }
        }
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 17 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestVariableRange.

/**
 * Test the use variable range pragma, making sure that use of
 * variable range characters is detected and flagged as an error.
 */
@Test
public void TestVariableRange() {
    String rule = "use variable range 0x70 0x72; a > A; b > B; q > Q;";
    try {
        Transliterator t = Transliterator.createFromRules("ID", rule, Transliterator.FORWARD);
        if (t != null) {
            errln("FAIL: Did not get the expected exception");
        }
    } catch (IllegalArgumentException e) {
        logln("Ok: " + e.getMessage());
        return;
    }
    errln("FAIL: No syntax error");
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 18 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestNFDChainRBT.

/**
 * Test NFD chaining with RBT
 */
@Test
public void TestNFDChainRBT() {
    Transliterator t = Transliterator.createFromRules("TEST", "::NFD; aa > Q; a > q;", Transliterator.FORWARD);
    logln(t.toRules(true));
    expect(t, "aa", "Q");
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 19 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestArbitraryVariableValues.

/**
 * Test zero length and > 1 char length variable values.  Test
 * use of variable refs in UnicodeSets.
 */
@Test
public void TestArbitraryVariableValues() {
    // Array of 3n items
    // Each item is <rules>, <input>, <expected output>
    String[] DATA = { "$abe = ab;" + "$pat = x[yY]z;" + "$ll  = 'a-z';" + "$llZ = [$ll];" + "$llY = [$ll$pat];" + "$emp = ;" + "$abe > ABE;" + "$pat > END;" + "$llZ > 1;" + "$llY > 2;" + "7$emp 8 > 9;" + "", "ab xYzxyz stY78", "ABE ENDEND 1129" };
    for (int i = 0; i < DATA.length; i += 3) {
        logln("Pattern: " + Utility.escape(DATA[i]));
        Transliterator t = Transliterator.createFromRules("<ID>", DATA[i], Transliterator.FORWARD);
        expect(t, DATA[i + 1], DATA[i + 2]);
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 20 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestSimpleRules.

@Test
public void TestSimpleRules() {
    /* Example: rules 1. ab>x|y
         *                2. yc>z
         *
         * []|eabcd  start - no match, copy e to tranlated buffer
         * [e]|abcd  match rule 1 - copy output & adjust cursor
         * [ex|y]cd  match rule 2 - copy output & adjust cursor
         * [exz]|d   no match, copy d to transliterated buffer
         * [exzd]|   done
         */
    expect("ab>x|y;" + "yc>z", "eabcd", "exzd");
    /* Another set of rules:
         *    1. ab>x|yzacw
         *    2. za>q
         *    3. qc>r
         *    4. cw>n
         *
         * []|ab       Rule 1
         * [x|yzacw]   No match
         * [xy|zacw]   Rule 2
         * [xyq|cw]    Rule 4
         * [xyqn]|     Done
         */
    expect("ab>x|yzacw;" + "za>q;" + "qc>r;" + "cw>n", "ab", "xyqn");
    /* Test categories
         */
    Transliterator t = Transliterator.createFromRules("<ID>", "$dummy=\uE100;" + "$vowel=[aeiouAEIOU];" + "$lu=[:Lu:];" + "$vowel } $lu > '!';" + "$vowel > '&';" + "'!' { $lu > '^';" + "$lu > '*';" + "a>ERROR", Transliterator.FORWARD);
    expect(t, "abcdefgABCDEFGU", "&bcd&fg!^**!^*&");
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Aggregations

Transliterator (android.icu.text.Transliterator)97 Test (org.junit.Test)88 ReplaceableString (android.icu.text.ReplaceableString)66 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)57 UnicodeSet (android.icu.text.UnicodeSet)19 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)5 ULocale (android.icu.util.ULocale)3 Enumeration (java.util.Enumeration)3 UnicodeFilter (android.icu.text.UnicodeFilter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 UnicodeMap (android.icu.dev.util.UnicodeMap)1 CanonicalIterator (android.icu.text.CanonicalIterator)1 Normalizer2 (android.icu.text.Normalizer2)1 Replaceable (android.icu.text.Replaceable)1 BufferedWriter (java.io.BufferedWriter)1 HashSet (java.util.HashSet)1