Search in sources :

Example 11 with UCharacterIterator

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

the class TestIDNA method doTestToASCII.

private void doTestToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.convertToASCII(src, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + "\n Got:  " + ex.toString() + "\n Expected: " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertToASCII(inBuf, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertToASCII(inIter, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 12 with UCharacterIterator

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

the class BasicTest method TestDebugIter.

@Test
public void TestDebugIter() {
    String src = Utility.unescape("\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e");
    String expected = Utility.unescape("\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e");
    Normalizer iter = new Normalizer(new StringCharacterIterator(Utility.unescape(src)), Normalizer.NONE, 0);
    int index = 0;
    int ch;
    UCharacterIterator cIter = UCharacterIterator.getInstance(expected);
    while ((ch = iter.next()) != Normalizer.DONE) {
        if (index >= expected.length()) {
            errln("FAIL: " + "Unexpected character '" + (char) ch + "' (" + hex(ch) + ")" + " at index " + index);
            break;
        }
        int want = UTF16.charAt(expected, index);
        if (ch != want) {
            errln("FAIL: " + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
        index += UTF16.getCharCount(ch);
    }
    if (index < expected.length()) {
        errln("FAIL: " + "Only got " + index + " chars, expected " + expected.length());
    }
    cIter.setToLimit();
    while ((ch = iter.previous()) != Normalizer.DONE) {
        int want = cIter.previousCodePoint();
        if (ch != want) {
            errln("FAIL: " + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 13 with UCharacterIterator

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

the class TestUCharacterIterator method TestClone.

// public methods --------------------------------------------------
/**
 * Testing cloning
 */
@Test
public void TestClone() throws CloneNotSupportedException {
    UCharacterIterator iterator = UCharacterIterator.getInstance("testing");
    UCharacterIterator cloned = (UCharacterIterator) iterator.clone();
    int completed = 0;
    while (completed != UCharacterIterator.DONE) {
        completed = iterator.next();
        if (completed != cloned.next()) {
            errln("Cloned operation failed");
        }
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) Test(org.junit.Test)

Example 14 with UCharacterIterator

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

the class TestUCharacterIterator method TestJitterbug1952.

@Test
public void TestJitterbug1952() {
    // test previous code point
    char[] src = new char[] { '\uDC00', '\uD800', '\uDC01', '\uD802', '\uDC02', '\uDC03' };
    UCharacterIterator iter = UCharacterIterator.getInstance(src);
    iter.setIndex(1);
    int ch;
    // if it does then we have a problem
    while ((ch = iter.previousCodePoint()) != UCharacterIterator.DONE) {
        if (ch != 0xDc00) {
            errln("iter.previousCodePoint() failed");
        }
    }
    iter.setIndex(5);
    while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
        if (ch != 0xDC03) {
            errln("iter.nextCodePoint() failed");
        }
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) Test(org.junit.Test)

Example 15 with UCharacterIterator

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

the class IDNA2003 method convertIDNToUnicode.

public static StringBuffer convertIDNToUnicode(String src, int options) throws StringPrepParseException {
    char[] srcArr = src.toCharArray();
    StringBuffer result = new StringBuffer();
    int sepIndex = 0;
    int oldSepIndex = 0;
    for (; ; ) {
        sepIndex = getSeparatorIndex(srcArr, sepIndex, srcArr.length);
        String label = new String(srcArr, oldSepIndex, sepIndex - oldSepIndex);
        if (label.length() == 0 && sepIndex != srcArr.length) {
            throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepParseException.ZERO_LENGTH_LABEL);
        }
        UCharacterIterator iter = UCharacterIterator.getInstance(label);
        result.append(convertToUnicode(iter, options));
        if (sepIndex == srcArr.length) {
            break;
        }
        // Unlike the ToASCII operation we don't normalize the label separators
        result.append(srcArr[sepIndex]);
        // increment the sepIndex to skip past the separator
        sepIndex++;
        oldSepIndex = sepIndex;
    }
    if (result.length() > MAX_DOMAIN_NAME_LENGTH) {
        throw new StringPrepParseException("The output exceed the max allowed length.", StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR);
    }
    return result;
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Aggregations

UCharacterIterator (android.icu.text.UCharacterIterator)33 StringPrepParseException (android.icu.text.StringPrepParseException)17 Test (org.junit.Test)15 StringCharacterIterator (java.text.StringCharacterIterator)5 CharacterIterator (java.text.CharacterIterator)4 Normalizer (android.icu.text.Normalizer)3 ReplaceableString (android.icu.text.ReplaceableString)3 CollationElementIterator (android.icu.text.CollationElementIterator)2 RuleBasedCollator (android.icu.text.RuleBasedCollator)2 CollationData (android.icu.impl.coll.CollationData)1 FCDIterCollationIterator (android.icu.impl.coll.FCDIterCollationIterator)1 FCDUTF16CollationIterator (android.icu.impl.coll.FCDUTF16CollationIterator)1 Collator (android.icu.text.Collator)1 StringPrep (android.icu.text.StringPrep)1