Search in sources :

Example 11 with ReplaceableString

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

the class TransliteratorTest method expect.

static void expect(Transliterator t, String source, String expectedResult, Transliterator.Position pos) {
    if (pos == null) {
        String result = t.transliterate(source);
        if (!expectAux(t.getID() + ":String", source, result, expectedResult))
            return;
    }
    Transliterator.Position index = null;
    if (pos == null) {
        index = new Transliterator.Position(0, source.length(), 0, source.length());
    } else {
        index = new Transliterator.Position(pos.contextStart, pos.contextLimit, pos.start, pos.limit);
    }
    ReplaceableString rsource = new ReplaceableString(source);
    t.finishTransliteration(rsource, index);
    if (index.start != index.limit) {
        expectAux(t.getID() + ":UNFINISHED", source, "start: " + index.start + ", limit: " + index.limit, false, expectedResult);
        return;
    }
    String result = rsource.toString();
    if (!expectAux(t.getID() + ":Replaceable", source, result, expectedResult))
        return;
    if (pos == null) {
        index = new Transliterator.Position();
    } else {
        index = new Transliterator.Position(pos.contextStart, pos.contextLimit, pos.start, pos.limit);
    }
    // Test incremental transliteration -- this result
    // must be the same after we finalize (see below).
    List<String> v = new ArrayList<String>();
    v.add(source);
    rsource.replace(0, rsource.length(), "");
    if (pos != null) {
        rsource.replace(0, 0, source);
        v.add(UtilityExtensions.formatInput(rsource, index));
        t.transliterate(rsource, index);
        v.add(UtilityExtensions.formatInput(rsource, index));
    } else {
        for (int i = 0; i < source.length(); ++i) {
            // v.add(i == 0 ? "" : " + " + source.charAt(i) + "");
            // log.append(source.charAt(i)).append(" -> "));
            t.transliterate(rsource, index, source.charAt(i));
            // v.add(UtilityExtensions.formatInput(rsource, index) + source.substring(i+1));
            v.add(UtilityExtensions.formatInput(rsource, index) + ((i < source.length() - 1) ? (" + '" + source.charAt(i + 1) + "' ->") : " =>"));
        }
    }
    // As a final step in keyboard transliteration, we must call
    // transliterate to finish off any pending partial matches that
    // were waiting for more input.
    t.finishTransliteration(rsource, index);
    result = rsource.toString();
    // log.append(" => ").append(rsource.toString());
    v.add(result);
    String[] results = new String[v.size()];
    v.toArray(results);
    expectAux(t.getID() + ":Incremental", results, result.equals(expectedResult), expectedResult);
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ArrayList(java.util.ArrayList) CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator)

Example 12 with ReplaceableString

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

the class TestUCharacterIterator method TestPreviousNext.

@Test
public void TestPreviousNext() {
    // src and expect strings
    char[] src = { UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999), UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f), 0xc4, 0x1ed0 };
    // iterators
    UCharacterIterator iter1 = UCharacterIterator.getInstance(new ReplaceableString(new String(src)));
    UCharacterIterator iter2 = UCharacterIterator.getInstance(src);
    UCharacterIterator iter3 = UCharacterIterator.getInstance(new StringCharacterIterator(new String(src)));
    UCharacterIterator iter4 = UCharacterIterator.getInstance(new StringBuffer(new String(src)));
    previousNext(iter1);
    previousNext(iter2);
    previousNext(iter3);
    previousNext(iter4);
    getText(iter1, new String(src));
    getText(iter2, new String(src));
    getText(iter3, new String(src));
    /* getCharacterIterator */
    CharacterIterator citer1 = iter1.getCharacterIterator();
    CharacterIterator citer2 = iter2.getCharacterIterator();
    CharacterIterator citer3 = iter3.getCharacterIterator();
    if (citer1.first() != iter1.current()) {
        errln("getCharacterIterator for iter1 failed");
    }
    if (citer2.first() != iter2.current()) {
        errln("getCharacterIterator for iter2 failed");
    }
    if (citer3.first() != iter3.current()) {
        errln("getCharacterIterator for iter3 failed");
    }
    /* Test clone()  && moveIndex()*/
    try {
        UCharacterIterator clone1 = (UCharacterIterator) iter1.clone();
        UCharacterIterator clone2 = (UCharacterIterator) iter2.clone();
        UCharacterIterator clone3 = (UCharacterIterator) iter3.clone();
        if (clone1.moveIndex(3) != iter1.moveIndex(3)) {
            errln("moveIndex for iter1 failed");
        }
        if (clone2.moveIndex(3) != iter2.moveIndex(3)) {
            errln("moveIndex for iter2 failed");
        }
        if (clone3.moveIndex(3) != iter3.moveIndex(3)) {
            errln("moveIndex for iter1 failed");
        }
    } catch (Exception e) {
        errln("could not clone the iterator");
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) ReplaceableString(android.icu.text.ReplaceableString) Test(org.junit.Test)

Aggregations

ReplaceableString (android.icu.text.ReplaceableString)12 Transliterator (android.icu.text.Transliterator)8 Test (org.junit.Test)7 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)3 Replaceable (android.icu.text.Replaceable)1 UCharacterIterator (android.icu.text.UCharacterIterator)1 UnicodeSet (android.icu.text.UnicodeSet)1 CharacterIterator (java.text.CharacterIterator)1 StringCharacterIterator (java.text.StringCharacterIterator)1 ArrayList (java.util.ArrayList)1