Search in sources :

Example 6 with UCharacterIterator

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

the class IDNA2003 method convertIDNToASCII.

public static StringBuffer convertIDNToASCII(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);
        // make sure this is not a root label separator.
        if (!(label.length() == 0 && sepIndex == srcArr.length)) {
            UCharacterIterator iter = UCharacterIterator.getInstance(label);
            result.append(convertToASCII(iter, options));
        }
        if (sepIndex == srcArr.length) {
            break;
        }
        // increment the sepIndex to skip past the separator
        sepIndex++;
        oldSepIndex = sepIndex;
        result.append((char) FULL_STOP);
    }
    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)

Example 7 with UCharacterIterator

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

the class BasicTest method assertEqual.

private void assertEqual(String expected, Normalizer iter, String msg) {
    int index = 0;
    int ch;
    UCharacterIterator cIter = UCharacterIterator.getInstance(expected);
    while ((ch = iter.next()) != Normalizer.DONE) {
        if (index >= expected.length()) {
            errln("FAIL: " + msg + "Unexpected character '" + (char) ch + "' (" + hex(ch) + ")" + " at index " + index);
            break;
        }
        int want = UTF16.charAt(expected, index);
        if (ch != want) {
            errln("FAIL: " + msg + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
        index += UTF16.getCharCount(ch);
    }
    if (index < expected.length()) {
        errln("FAIL: " + msg + "Only got " + index + " chars, expected " + expected.length());
    }
    cIter.setToLimit();
    while ((ch = iter.previous()) != Normalizer.DONE) {
        int want = cIter.previousCodePoint();
        if (ch != want) {
            errln("FAIL: " + msg + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator)

Example 8 with UCharacterIterator

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

the class NFS4StringPrep method prepare.

private static byte[] prepare(byte[] src, StringPrep strprep) throws StringPrepParseException, UnsupportedEncodingException {
    String s = new String(src, "UTF-8");
    UCharacterIterator iter = UCharacterIterator.getInstance(s);
    StringBuffer out = strprep.prepare(iter, StringPrep.DEFAULT);
    return out.toString().getBytes("UTF-8");
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator)

Example 9 with UCharacterIterator

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

the class TestIDNA method doTestIDNToASCII.

private void doTestIDNToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.convertIDNToASCII(src, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToASCII(inBuf, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToASCII(inIter, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertIDNToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertIDNToASCII 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 10 with UCharacterIterator

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

the class TestIDNA method TestNamePrepConformance.

@Test
public void TestNamePrepConformance() throws Exception {
    StringPrep namePrep = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
    for (int i = 0; i < TestData.conformanceTestCases.length; i++) {
        TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
        UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
        try {
            StringBuffer output = namePrep.prepare(iter, StringPrep.DEFAULT);
            if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
                errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
            }
            if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
                errln("Did not get the expected exception. The operation succeeded!");
            }
        } catch (StringPrepParseException ex) {
            if (testCase.expected == null || !ex.equals(testCase.expected)) {
                errln("Did not get the expected exception for source: " + testCase.input + " Got:  " + ex.toString());
            }
        }
        try {
            iter.setToStart();
            StringBuffer output = namePrep.prepare(iter, StringPrep.ALLOW_UNASSIGNED);
            if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
                errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
            }
            if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
                errln("Did not get the expected exception. The operation succeeded!");
            }
        } catch (StringPrepParseException ex) {
            if (testCase.expected == null || !ex.equals(testCase.expected)) {
                errln("Did not get the expected exception for source: " + testCase.input + " Got:  " + ex.toString());
            }
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) StringPrep(android.icu.text.StringPrep) UCharacterIterator(android.icu.text.UCharacterIterator) Test(org.junit.Test)

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