Search in sources :

Example 1 with ReplaceableString

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

the class CompoundTransliteratorTest method expect.

private void expect(Transliterator t, String source, String expectedResult) {
    String result = t.transliterate(source);
    expectAux(t.getID() + ":String", source, result, expectedResult);
    ReplaceableString rsource = new ReplaceableString(source);
    t.transliterate(rsource);
    result = rsource.toString();
    expectAux(t.getID() + ":Replaceable", source, result, expectedResult);
    // Test keyboard (incremental) transliteration -- this result
    // must be the same after we finalize (see below).
    rsource.replace(0, rsource.length(), "");
    Transliterator.Position index = new Transliterator.Position();
    StringBuffer log = new StringBuffer();
    for (int i = 0; i < source.length(); ++i) {
        if (i != 0) {
            log.append(" + ");
        }
        log.append(source.charAt(i)).append(" -> ");
        t.transliterate(rsource, index, String.valueOf(source.charAt(i)));
        // Append the string buffer with a vertical bar '|' where
        // the committed index is.
        String s = rsource.toString();
        log.append(s.substring(0, index.start)).append('|').append(s.substring(index.start));
    }
    // 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());
    expectAux(t.getID() + ":Keyboard", log.toString(), result.equals(expectedResult), expectedResult);
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator)

Example 2 with ReplaceableString

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

the class ErrorTest method TestTransliteratorErrors.

@Test
public void TestTransliteratorErrors() {
    String trans = "Latin-Greek";
    String bogusID = "LATINGREEK-GREEKLATIN";
    String newID = "Bogus-Latin";
    String newIDRules = "zzz > Z; f <> ph";
    String bogusRules = "a } [b-g m-p ";
    ReplaceableString testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
    String insertString = "cats and dogs";
    int stoppedAt = 0, len;
    Transliterator.Position pos = new Transliterator.Position();
    Transliterator t = Transliterator.getInstance(trans, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: construction of Latin-Greek");
        return;
    }
    len = testString.length();
    stoppedAt = t.transliterate(testString, 0, 100);
    if (stoppedAt != -1) {
        errln("FAIL: Out of bounds check failed (1).");
    } else if (testString.length() != len) {
        testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
        errln("FAIL: Transliterate fails and the target string was modified.");
    }
    stoppedAt = t.transliterate(testString, 100, testString.length() - 1);
    if (stoppedAt != -1) {
        errln("FAIL: Out of bounds check failed (2).");
    } else if (testString.length() != len) {
        testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
        errln("FAIL: Transliterate fails and the target string was modified.");
    }
    pos.start = 100;
    pos.limit = testString.length();
    try {
        t.transliterate(testString, pos);
        errln("FAIL: Start offset is out of bounds, error not reported.");
    } catch (IllegalArgumentException e) {
        logln("Start offset is out of bounds and detected.");
    }
    pos.limit = 100;
    pos.start = 0;
    try {
        t.transliterate(testString, pos);
        errln("FAIL: Limit offset is out of bounds, error not reported.\n");
    } catch (IllegalArgumentException e) {
        logln("Start offset is out of bounds and detected.");
    }
    len = pos.contextLimit = testString.length();
    pos.contextStart = 0;
    pos.limit = len - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, insertString);
        if (len == pos.limit) {
            errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
        }
    } catch (IllegalArgumentException e) {
        errln("Insertion test with string failed for some reason.");
    }
    pos.contextStart = 0;
    pos.contextLimit = testString.length();
    pos.limit = testString.length() - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, 0x0061);
        if (len == pos.limit) {
            errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
        }
    } catch (IllegalArgumentException e) {
        errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
    }
    len = pos.limit = testString.length();
    pos.contextStart = 0;
    pos.contextLimit = testString.length() - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, insertString);
        errln("FAIL: Out of bounds check failed (3).");
        if (testString.length() != len) {
            errln("FAIL: The input string was modified though the offsets were out of bounds.");
        }
    } catch (IllegalArgumentException e) {
        logln("Insertion test with out of bounds indexes.");
    }
    Transliterator t1 = null;
    try {
        t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);
        if (t1 != null) {
            errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
        }
    } catch (IllegalArgumentException e) {
    }
    // try { // unneeded - Exception cannot be thrown
    Transliterator t2 = Transliterator.createFromRules(newID, newIDRules, Transliterator.FORWARD);
    try {
        Transliterator t3 = t2.getInverse();
        errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
        if (t3 != null) {
            errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
        }
    } catch (Exception e) {
    }
    // } catch (Exception e) { }
    try {
        Transliterator t4 = Transliterator.createFromRules(newID, bogusRules, Transliterator.FORWARD);
        if (t4 != null) {
            errln("FAIL: The rules is malformed but error was not reported.");
        }
    } catch (Exception e) {
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 3 with ReplaceableString

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

the class UtilityExtensions method formatInput.

/**
 * For debugging purposes; format the given text in the form
 * aaa{bbb|ccc|ddd}eee, where the {} indicate the context start
 * and limit, and the || indicate the start and limit.
 */
public static StringBuffer formatInput(StringBuffer appendTo, ReplaceableString input, Transliterator.Position pos) {
    if (0 <= pos.contextStart && pos.contextStart <= pos.start && pos.start <= pos.limit && pos.limit <= pos.contextLimit && pos.contextLimit <= input.length()) {
        String b, c, d;
        // a = input.substring(0, pos.contextStart);
        b = input.substring(pos.contextStart, pos.start);
        c = input.substring(pos.start, pos.limit);
        d = input.substring(pos.limit, pos.contextLimit);
        // e = input.substring(pos.contextLimit, input.length());
        // append(a).
        appendTo.append('{').append(b).append('|').append(c).append('|').append(d).append('}');
    } else {
        appendTo.append("INVALID Position {cs=" + pos.contextStart + ", s=" + pos.start + ", l=" + pos.limit + ", cl=" + pos.contextLimit + "} on " + input);
    }
    return appendTo;
}
Also used : ReplaceableString(android.icu.text.ReplaceableString)

Example 4 with ReplaceableString

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

the class TransliteratorTest method TestCompoundHex.

/**
 * Compose the hex transliterators forward and reverse.
 */
@Test
public void TestCompoundHex() {
    Transliterator a = Transliterator.getInstance("Any-Hex");
    Transliterator b = Transliterator.getInstance("Hex-Any");
    // Transliterator[] trans = { a, b };
    // Transliterator ab = Transliterator.getInstance(trans);
    Transliterator ab = Transliterator.getInstance("Any-Hex;Hex-Any");
    // Do some basic tests of b
    expect(b, "\\u0030\\u0031", "01");
    String s = "abcde";
    expect(ab, s, s);
    // trans = new Transliterator[] { b, a };
    // Transliterator ba = Transliterator.getInstance(trans);
    Transliterator ba = Transliterator.getInstance("Hex-Any;Any-Hex");
    ReplaceableString str = new ReplaceableString(s);
    a.transliterate(str);
    expect(ba, str.toString(), str.toString());
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 5 with ReplaceableString

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

the class UTF16Test method TestCharAt.

/**
 * Testing UTF16 class methods charAt and charAtCodePoint
 */
@Test
public void TestCharAt() {
    StringBuffer strbuff = new StringBuffer("12345\ud800\udc0167890\ud800\udc02");
    if (UTF16.charAt(strbuff, 0) != '1' || UTF16.charAt(strbuff, 2) != '3' || UTF16.charAt(strbuff, 5) != 0x10001 || UTF16.charAt(strbuff, 6) != 0x10001 || UTF16.charAt(strbuff, 12) != 0x10002 || UTF16.charAt(strbuff, 13) != 0x10002) {
        errln("FAIL Getting character from string buffer error");
    }
    String str = strbuff.toString();
    if (UTF16.charAt(str, 0) != '1' || UTF16.charAt(str, 2) != '3' || UTF16.charAt(str, 5) != 0x10001 || UTF16.charAt(str, 6) != 0x10001 || UTF16.charAt(str, 12) != 0x10002 || UTF16.charAt(str, 13) != 0x10002) {
        errln("FAIL Getting character from string error");
    }
    char[] array = str.toCharArray();
    int start = 0;
    int limit = str.length();
    if (UTF16.charAt(array, start, limit, 0) != '1' || UTF16.charAt(array, start, limit, 2) != '3' || UTF16.charAt(array, start, limit, 5) != 0x10001 || UTF16.charAt(array, start, limit, 6) != 0x10001 || UTF16.charAt(array, start, limit, 12) != 0x10002 || UTF16.charAt(array, start, limit, 13) != 0x10002) {
        errln("FAIL Getting character from array error");
    }
    // check the sub array here.
    start = 6;
    limit = 13;
    try {
        UTF16.charAt(array, start, limit, -1);
        errln("FAIL out of bounds error expected");
    } catch (Exception e) {
        System.out.print("");
    }
    try {
        UTF16.charAt(array, start, limit, 8);
        errln("FAIL out of bounds error expected");
    } catch (Exception e) {
        System.out.print("");
    }
    if (UTF16.charAt(array, start, limit, 0) != 0xdc01) {
        errln("FAIL Expected result in subarray 0xdc01");
    }
    if (UTF16.charAt(array, start, limit, 6) != 0xd800) {
        errln("FAIL Expected result in subarray 0xd800");
    }
    ReplaceableString replaceable = new ReplaceableString(str);
    if (UTF16.charAt(replaceable, 0) != '1' || UTF16.charAt(replaceable, 2) != '3' || UTF16.charAt(replaceable, 5) != 0x10001 || UTF16.charAt(replaceable, 6) != 0x10001 || UTF16.charAt(replaceable, 12) != 0x10002 || UTF16.charAt(replaceable, 13) != 0x10002) {
        errln("FAIL Getting character from replaceable error");
    }
    StringBuffer strbuffer = new StringBuffer("0xD805");
    UTF16.charAt((CharSequence) strbuffer, 0);
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) 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