Search in sources :

Example 1 with UCharacterIterator

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

the class CollationTest method TestFCD.

@Test
public void TestFCD() {
    CollationData data = CollationRoot.getData();
    // Input string, not FCD.
    StringBuilder buf = new StringBuilder();
    buf.append("\u0308\u00e1\u0062\u0301\u0327\u0430\u0062").appendCodePoint(// MUSICAL SYMBOL QUARTER NOTE=1D158 1D165, ccc=0, 216
    0x1D15F).append(// ccc=202, 230
    "\u0327\u0308").appendCodePoint(// MUSICAL SYMBOL COMBINING AUGMENTATION DOT, ccc=226
    0x1D16D).appendCodePoint(0x1D15F).appendCodePoint(0x1D16D).append("\uac01").append(// Character with tccc!=0 decomposed together with mis-ordered sequence.
    "\u00e7").appendCodePoint(0x1D16D).appendCodePoint(0x1D165).append(// Character with tccc!=0 decomposed together with decomposed sequence.
    "\u00e1").append(// Tibetan composite vowels must be decomposed.
    "\u0f73\u0f75").append("\u4e00\u0f81");
    String s = buf.toString();
    // Expected code points.
    int[] cp = { 0x308, 0xe1, 0x62, 0x327, 0x301, 0x430, 0x62, 0x1D158, 0x327, 0x1D165, 0x1D16D, 0x308, 0x1D15F, 0x1D16D, 0xac01, 0x63, 0x327, 0x1D165, 0x1D16D, 0x61, 0xf71, 0xf71, 0xf72, 0xf74, 0x301, 0x4e00, 0xf71, 0xf80 };
    FCDUTF16CollationIterator u16ci = new FCDUTF16CollationIterator(data, false, s, 0);
    CodePointIterator cpi = new CodePointIterator(cp);
    checkFCD("FCDUTF16CollationIterator", u16ci, cpi);
    cpi.resetToStart();
    UCharacterIterator iter = UCharacterIterator.getInstance(s);
    FCDIterCollationIterator uici = new FCDIterCollationIterator(data, false, iter, 0);
    checkFCD("FCDIterCollationIterator", uici, cpi);
}
Also used : FCDUTF16CollationIterator(android.icu.impl.coll.FCDUTF16CollationIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CollationData(android.icu.impl.coll.CollationData) FCDIterCollationIterator(android.icu.impl.coll.FCDIterCollationIterator) Test(org.junit.Test)

Example 2 with UCharacterIterator

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

the class IDNAReference 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;
        }
        // increment the sepIndex to skip past the separator
        sepIndex++;
        oldSepIndex = sepIndex;
        result.append((char) FULL_STOP);
    }
    return result;
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 3 with UCharacterIterator

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

the class IDNAReference 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);
    }
    return result;
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator)

Example 4 with UCharacterIterator

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

the class NamePrepTransform method prepare.

public StringBuffer prepare(String src, int options) throws StringPrepParseException {
    int ch;
    String mapOut = map(src, options);
    UCharacterIterator iter = UCharacterIterator.getInstance(mapOut);
    int direction = UCharacterDirection.CHAR_DIRECTION_COUNT, firstCharDir = UCharacterDirection.CHAR_DIRECTION_COUNT;
    int rtlPos = -1, ltrPos = -1;
    boolean rightToLeft = false, leftToRight = false;
    while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
        if (transform.prohibitedSet.contains(ch) == true && ch != 0x0020) {
            throw new StringPrepParseException("A prohibited code point was found in the input", StringPrepParseException.PROHIBITED_ERROR, iter.getText(), iter.getIndex());
        }
        direction = UCharacter.getDirection(ch);
        if (firstCharDir == UCharacterDirection.CHAR_DIRECTION_COUNT) {
            firstCharDir = direction;
        }
        if (direction == UCharacterDirection.LEFT_TO_RIGHT) {
            leftToRight = true;
            ltrPos = iter.getIndex() - 1;
        }
        if (direction == UCharacterDirection.RIGHT_TO_LEFT || direction == UCharacterDirection.RIGHT_TO_LEFT_ARABIC) {
            rightToLeft = true;
            rtlPos = iter.getIndex() - 1;
        }
    }
    // satisfy 2
    if (leftToRight == true && rightToLeft == true) {
        throw new StringPrepParseException("The input does not conform to the rules for BiDi code points.", StringPrepParseException.CHECK_BIDI_ERROR, iter.getText(), (rtlPos > ltrPos) ? rtlPos : ltrPos);
    }
    // satisfy 3
    if (rightToLeft == true && !((firstCharDir == UCharacterDirection.RIGHT_TO_LEFT || firstCharDir == UCharacterDirection.RIGHT_TO_LEFT_ARABIC) && (direction == UCharacterDirection.RIGHT_TO_LEFT || direction == UCharacterDirection.RIGHT_TO_LEFT_ARABIC))) {
        throw new StringPrepParseException("The input does not conform to the rules for BiDi code points.", StringPrepParseException.CHECK_BIDI_ERROR, iter.getText(), (rtlPos > ltrPos) ? rtlPos : ltrPos);
    }
    return new StringBuffer(mapOut);
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 5 with UCharacterIterator

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

the class TestIDNARef method TestNamePrepConformance.

@Test
public void TestNamePrepConformance() throws Exception {
    try {
        NamePrepTransform namePrep = NamePrepTransform.getInstance();
        if (!namePrep.isReady()) {
            logln("Transliterator is not available on this environment.");
            return;
        }
        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, NamePrepTransform.NONE);
                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, NamePrepTransform.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());
                }
            }
        }
    } catch (java.lang.ExceptionInInitializerError e) {
        warnln("Could not load NamePrepTransformData");
    } catch (java.lang.NoClassDefFoundError ex) {
        warnln("Could not load NamePrepTransform data");
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) 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