Java Code Examples for android.icu.lang.UCharacter#toCodePoint()

The following examples show how to use android.icu.lang.UCharacter#toCodePoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UCharacterSurrogateTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void TestToCodePoint() {
    final char[] pairs = {(char) (UCharacter.MIN_HIGH_SURROGATE + 0),
            (char) (UCharacter.MIN_LOW_SURROGATE + 0),
            (char) (UCharacter.MIN_HIGH_SURROGATE + 1),
            (char) (UCharacter.MIN_LOW_SURROGATE + 1),
            (char) (UCharacter.MIN_HIGH_SURROGATE + 2),
            (char) (UCharacter.MIN_LOW_SURROGATE + 2),
            (char) (UCharacter.MAX_HIGH_SURROGATE - 2),
            (char) (UCharacter.MAX_LOW_SURROGATE - 2),
            (char) (UCharacter.MAX_HIGH_SURROGATE - 1),
            (char) (UCharacter.MAX_LOW_SURROGATE - 1),
            (char) (UCharacter.MAX_HIGH_SURROGATE - 0),
            (char) (UCharacter.MAX_LOW_SURROGATE - 0),};
    for (int i = 0; i < pairs.length; i += 2) {
        int cp = UCharacter.toCodePoint(pairs[i], pairs[i + 1]);
        if (pairs[i] != UTF16.getLeadSurrogate(cp)
                || pairs[i + 1] != UTF16.getTrailSurrogate(cp)) {

            errln(Integer.toHexString(pairs[i]) + ", " + pairs[i + 1]);
            break;
        }
    }
}
 
Example 2
Source File: UCharacterSurrogateTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@org.junit.Test
public void TestToChars() {
    char[] chars = new char[3];
    int cp = UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
            UCharacter.MIN_LOW_SURROGATE);
    UCharacter.toChars(cp, chars, 1);
    if (chars[1] != UCharacter.MIN_HIGH_SURROGATE
            || chars[2] != UCharacter.MIN_LOW_SURROGATE) {

        errln("fail");
    }

    chars = UCharacter.toChars(cp);
    if (chars[0] != UCharacter.MIN_HIGH_SURROGATE
            || chars[1] != UCharacter.MIN_LOW_SURROGATE) {

        errln("fail");
    }
}
 
Example 3
Source File: ScientificNumberFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static int char32AtAndAdvance(AttributedCharacterIterator iterator) {
    char c1 = iterator.current();
    char c2 = iterator.next();
    if (UCharacter.isHighSurrogate(c1)) {
        // If c2 is DONE, it will fail the low surrogate test and we
        // skip this block.
        if (UCharacter.isLowSurrogate(c2)) {
            iterator.next();
            return UCharacter.toCodePoint(c1, c2);
        }
    }
    return c1;
}
 
Example 4
Source File: UCharacterSurrogateTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void TestCodePointAtBefore() {
    String s = "" + UCharacter.MIN_HIGH_SURROGATE + // isolated high
            UCharacter.MIN_HIGH_SURROGATE + // pair
            UCharacter.MIN_LOW_SURROGATE + UCharacter.MIN_LOW_SURROGATE; // isolated
                                                                         // low
    char[] c = s.toCharArray();
    int[] avalues = {
            UCharacter.MIN_HIGH_SURROGATE,
            UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
                    UCharacter.MIN_LOW_SURROGATE),
            UCharacter.MIN_LOW_SURROGATE, UCharacter.MIN_LOW_SURROGATE};
    int[] bvalues = {
            UCharacter.MIN_HIGH_SURROGATE,
            UCharacter.MIN_HIGH_SURROGATE,
            UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE,
                    UCharacter.MIN_LOW_SURROGATE),
            UCharacter.MIN_LOW_SURROGATE,};
    StringBuffer b = new StringBuffer(s);
    for (int i = 0; i < avalues.length; ++i) {
        if (UCharacter.codePointAt(s, i) != avalues[i])
            errln("string at: " + i);
        if (UCharacter.codePointAt(c, i) != avalues[i])
            errln("chars at: " + i);
        if (UCharacter.codePointAt(b, i) != avalues[i])
            errln("stringbuffer at: " + i);

        if (UCharacter.codePointBefore(s, i + 1) != bvalues[i])
            errln("string before: " + i);
        if (UCharacter.codePointBefore(c, i + 1) != bvalues[i])
            errln("chars before: " + i);
        if (UCharacter.codePointBefore(b, i + 1) != bvalues[i])
            errln("stringbuffer before: " + i);
    }

    //cover codePointAtBefore with limit
    logln("Testing codePointAtBefore with limit ...");
    for (int i = 0; i < avalues.length; ++i) {
        if (UCharacter.codePointAt(c, i, 4) != avalues[i])
            errln("chars at: " + i);
        if (UCharacter.codePointBefore(c, i + 1, 0) != bvalues[i])
            errln("chars before: " + i);
    }

}
 
Example 5
Source File: UTF16Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestNewString() {
final int[] codePoints = {
    UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE, UCharacter.MAX_LOW_SURROGATE),
    UCharacter.toCodePoint(UCharacter.MAX_HIGH_SURROGATE, UCharacter.MIN_LOW_SURROGATE),
    UCharacter.MAX_HIGH_SURROGATE,
    'A',
    -1,
};


final String cpString = "" +
    UCharacter.MIN_HIGH_SURROGATE +
    UCharacter.MAX_LOW_SURROGATE +
    UCharacter.MAX_HIGH_SURROGATE +
    UCharacter.MIN_LOW_SURROGATE +
    UCharacter.MAX_HIGH_SURROGATE +
    'A';

final int[][] tests = {
    { 0, 1, 0, 2 },
    { 0, 2, 0, 4 },
    { 1, 1, 2, 2 },
    { 1, 2, 2, 3 },
    { 1, 3, 2, 4 },
    { 2, 2, 4, 2 },
    { 2, 3, 0, -1 },
    { 4, 5, 0, -1 },
    { 3, -1, 0, -1 }
};

 for (int i = 0; i < tests.length; ++i) {
    int[] t = tests[i];
    int s = t[0];
    int c = t[1];
    int rs = t[2];
    int rc = t[3];

    Exception e = null;
    try {
    String str = UTF16.newString(codePoints, s, c);
    if (rc == -1 || !str.equals(cpString.substring(rs, rs+rc))) {
        errln("failed codePoints iter: " + i + " start: " + s + " len: " + c);
    }
    continue;
    }
    catch (IndexOutOfBoundsException e1) {
    e = e1;
    }
    catch (IllegalArgumentException e2) {
    e = e2;
    }
    if (rc != -1) {
    errln(e.getMessage());
    }
}
}