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

The following examples show how to use android.icu.lang.UCharacter#digit() . 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: TimeZoneFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a single decimal digit, either localized digits used by this object
 * or any Unicode numeric character.
 * @param text the text
 * @param start the start index
 * @param len the actual length read from the text
 * the start index is not a decimal number.
 * @return the integer value of the parsed digit, or -1 on failure.
 */
private int parseSingleLocalizedDigit(String text, int start, int[] len) {
    int digit = -1;
    len[0] = 0;
    if (start < text.length()) {
        int cp = Character.codePointAt(text, start);

        // First, try digits configured for this instance
        for (int i = 0; i < _gmtOffsetDigits.length; i++) {
            if (cp == _gmtOffsetDigits[i].codePointAt(0)) {
                digit = i;
                break;
            }
        }
        // If failed, check if this is a Unicode digit
        if (digit < 0) {
            digit = UCharacter.digit(cp);
        }

        if (digit >= 0) {
            len[0] = Character.charCount(cp);
        }
    }
    return digit;
}
 
Example 2
Source File: ScientificNumberFormatter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static void copyAsSuperscript(
        AttributedCharacterIterator iterator, int start, int limit, StringBuilder result) {
    int oldIndex = iterator.getIndex();
    iterator.setIndex(start);
    while (iterator.getIndex() < limit) {
        int aChar = char32AtAndAdvance(iterator);
        int digit = UCharacter.digit(aChar);
        if (digit < 0) {
            throw new IllegalArgumentException();
        }
        result.append(SUPERSCRIPT_DIGITS[digit]);
    }
    iterator.setIndex(oldIndex);
}
 
Example 3
Source File: Utility.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Parse an integer at pos, either of the form \d+ or of the form
 * 0x[0-9A-Fa-f]+ or 0[0-7]+, that is, in standard decimal, hex,
 * or octal format.
 * @param pos INPUT-OUTPUT parameter.  On input, the first
 * character to parse.  On output, the character after the last
 * parsed character.
 */
public static int parseInteger(String rule, int[] pos, int limit) {
    int count = 0;
    int value = 0;
    int p = pos[0];
    int radix = 10;

    if (rule.regionMatches(true, p, "0x", 0, 2)) {
        p += 2;
        radix = 16;
    } else if (p < limit && rule.charAt(p) == '0') {
        p++;
        count = 1;
        radix = 8;
    }

    while (p < limit) {
        int d = UCharacter.digit(rule.charAt(p++), radix);
        if (d < 0) {
            --p;
            break;
        }
        ++count;
        int v = (value * radix) + d;
        if (v <= value) {
            // If there are too many input digits, at some point
            // the value will go negative, e.g., if we have seen
            // "0x8000000" already and there is another '0', when
            // we parse the next 0 the value will go negative.
            return 0;
        }
        value = v;
    }
    if (count > 0) {
        pos[0] = p;
    }
    return value;
}
 
Example 4
Source File: Utility.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Parse an unsigned 31-bit integer at the given offset.  Use
 * UCharacter.digit() to parse individual characters into digits.
 * @param text the text to be parsed
 * @param pos INPUT-OUTPUT parameter.  On entry, pos[0] is the
 * offset within text at which to start parsing; it should point
 * to a valid digit.  On exit, pos[0] is the offset after the last
 * parsed character.  If the parse failed, it will be unchanged on
 * exit.  Must be >= 0 on entry.
 * @param radix the radix in which to parse; must be >= 2 and <=
 * 36.
 * @return a non-negative parsed number, or -1 upon parse failure.
 * Parse fails if there are no digits, that is, if pos[0] does not
 * point to a valid digit on entry, or if the number to be parsed
 * does not fit into a 31-bit unsigned integer.
 */
public static int parseNumber(String text, int[] pos, int radix) {
    // assert(pos[0] >= 0);
    // assert(radix >= 2);
    // assert(radix <= 36);
    int n = 0;
    int p = pos[0];
    while (p < text.length()) {
        int ch = Character.codePointAt(text, p);
        int d = UCharacter.digit(ch, radix);
        if (d < 0) {
            break;
        }
        n = radix*n + d;
        // ASSUME that when a 32-bit integer overflows it becomes
        // negative.  E.g., 214748364 * 10 + 8 => negative value.
        if (n < 0) {
            return -1;
        }
        ++p;
    }
    if (p == pos[0]) {
        return -1;
    }
    pos[0] = p;
    return n;
}
 
Example 5
Source File: UCharacterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestGetEuropeanDigit(){
    //The number retrieved from 0xFF41 to 0xFF5A is due to
    //  exhaustive testing from UTF16.CODEPOINT_MIN_VALUE to
    //  UTF16.CODEPOINT_MAX_VALUE return a value of -1.

    int[] radixResult = {
            10,11,12,13,14,15,16,17,18,19,20,21,22,
            23,24,25,26,27,28,29,30,31,32,33,34,35};
    // Invalid and too-small-for-these-digits radix values.
    int[] radixCase1 = {0,1,5,10,100};
    // Radix values that work for at least some of the "digits".
    int[] radixCase2 = {12,16,20,36};

    for(int i=0xFF41; i<=0xFF5A; i++){
        for(int j=0; j < radixCase1.length; j++){
            if(UCharacter.digit(i, radixCase1[j]) != -1){
                errln("UCharacter.digit(int,int) was supposed to return -1 for radix " + radixCase1[j]
                        + ". Value passed: U+" + Integer.toHexString(i) + ". Got: " + UCharacter.digit(i, radixCase1[j]));
            }
        }
        for(int j=0; j < radixCase2.length; j++){
            int radix = radixCase2[j];
            int expected = (radixResult[i-0xFF41] < radix) ? radixResult[i-0xFF41] : -1;
            int actual = UCharacter.digit(i, radix);
            if(actual != expected){
                errln("UCharacter.digit(int,int) was supposed to return " +
                        expected + " for radix " + radix +
                        ". Value passed: U+" + Integer.toHexString(i) + ". Got: " + actual);
                break;
            }
        }
    }
}
 
Example 6
Source File: DateNumberFormat.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public Number parse(String text, ParsePosition parsePosition) {
    long num = 0;
    boolean sawNumber = false;
    boolean negative = false;
    int base = parsePosition.getIndex();
    int offset = 0;
    for (; base + offset < text.length(); offset++) {
        char ch = text.charAt(base + offset);
        if (offset == 0 && ch == minusSign) {
            if (positiveOnly) {
                break;
            }
            negative = true;
        } else {
            int digit = ch - digits[0];
            if (digit < 0 || 9 < digit) {
                digit = UCharacter.digit(ch);
            }
            if (digit < 0 || 9 < digit) {
                for ( digit = 0 ; digit < 10 ; digit++ ) {
                    if ( ch == digits[digit]) {
                        break;
                    }
                }
            }
            if (0 <= digit && digit <= 9 && num < PARSE_THRESHOLD) {
                sawNumber = true;
                num = num * 10 + digit;
            } else {
                break;
            }
        }
    }
    Number result = null;
    if (sawNumber) {
        num = negative ? num * (-1) : num;
        result = Long.valueOf(num);
        parsePosition.setIndex(base + offset);
    }
    return result;
}
 
Example 7
Source File: UCharacterCompare.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Main testing method
 */
public static void main(String arg[]) {
    System.out.println("Starting character compare");
    try {
        FileWriter f;
        if (arg.length == 0)
            f = new FileWriter("compare.txt");
        else
            f = new FileWriter(arg[0]);
        PrintWriter p = new PrintWriter(f);
        p.print("char  character name                                                           ");
        p.println("method name               ucharacter character");
        for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
            System.out.println("character \\u" + Integer.toHexString(i));
            if (UCharacter.isDefined(i) != Character.isDefined(i))
                trackDifference(p, i, "isDefined()", "" + UCharacter.isDefined(i), "" + Character.isDefined(i));
            else {
                if (UCharacter.digit(i, 10) != Character.digit(i, 10))
                    trackDifference(p, i, "digit()", "" + UCharacter.digit(i, 10), "" + Character.digit(i, 10));
                if (UCharacter.getNumericValue(i) != Character.getNumericValue(i))
                    trackDifference(p, i, "getNumericValue()", "" + UCharacter.getNumericValue(i), ""
                            + Character.getNumericValue(i));
                if (!compareType(UCharacter.getType(i), Character.getType(i)))
                    trackDifference(p, i, "getType()", "" + UCharacter.getType(i), "" + Character.getType(i));
                if (UCharacter.isDigit(i) != Character.isDigit(i))
                    trackDifference(p, i, "isDigit()", "" + UCharacter.isDigit(i), "" + Character.isDigit(i));
                if (UCharacter.isISOControl(i) != Character.isISOControl(i))
                    trackDifference(p, i, "isISOControl()", "" + UCharacter.isISOControl(i), ""
                            + Character.isISOControl(i));
                if (UCharacter.isLetter(i) != Character.isLetter(i))
                    trackDifference(p, i, "isLetter()", "" + UCharacter.isLetter(i), "" + Character.isLetter(i));
                if (UCharacter.isLetterOrDigit(i) != Character.isLetterOrDigit(i))
                    trackDifference(p, i, "isLetterOrDigit()", "" + UCharacter.isLetterOrDigit(i), ""
                            + Character.isLetterOrDigit(i));
                if (UCharacter.isLowerCase(i) != Character.isLowerCase(i))
                    trackDifference(p, i, "isLowerCase()", "" + UCharacter.isLowerCase(i), ""
                            + Character.isLowerCase(i));
                if (UCharacter.isWhitespace(i) != Character.isWhitespace(i))
                    trackDifference(p, i, "isWhitespace()", "" + UCharacter.isWhitespace(i), ""
                            + Character.isWhitespace(i));
                if (UCharacter.isSpaceChar(i) != Character.isSpaceChar(i))
                    trackDifference(p, i, "isSpaceChar()", "" + UCharacter.isSpaceChar(i), ""
                            + Character.isSpaceChar(i));
                if (UCharacter.isTitleCase(i) != Character.isTitleCase(i))
                    trackDifference(p, i, "isTitleChar()", "" + UCharacter.isTitleCase(i), ""
                            + Character.isTitleCase(i));
                if (UCharacter.isUnicodeIdentifierPart(i) != Character.isUnicodeIdentifierPart(i))
                    trackDifference(p, i, "isUnicodeIdentifierPart()", "" + UCharacter.isUnicodeIdentifierPart(i),
                            "" + Character.isUnicodeIdentifierPart(i));
                if (UCharacter.isUnicodeIdentifierStart(i) != Character.isUnicodeIdentifierStart(i))
                    trackDifference(p, i, "isUnicodeIdentifierStart()",
                            "" + UCharacter.isUnicodeIdentifierStart(i), "" + Character.isUnicodeIdentifierStart(i));
                if (UCharacter.isIdentifierIgnorable(i) != Character.isIdentifierIgnorable(i))
                    trackDifference(p, i, "isIdentifierIgnorable()", "" + UCharacter.isIdentifierIgnorable(i), ""
                            + Character.isIdentifierIgnorable(i));
                if (UCharacter.isUpperCase(i) != Character.isUpperCase(i))
                    trackDifference(p, i, "isUpperCase()", "" + UCharacter.isUpperCase(i), ""
                            + Character.isUpperCase(i));
                if (UCharacter.toLowerCase(i) != Character.toLowerCase(i))
                    trackDifference(p, i, "toLowerCase()", Integer.toHexString(UCharacter.toLowerCase(i)), Integer
                            .toHexString(Character.toLowerCase(i)));
                if (!UCharacter.toString(i).equals(new Character(i).toString()))
                    trackDifference(p, i, "toString()", UCharacter.toString(i), new Character(i).toString());
                if (UCharacter.toTitleCase(i) != Character.toTitleCase(i))
                    trackDifference(p, i, "toTitleCase()", Integer.toHexString(UCharacter.toTitleCase(i)), Integer
                            .toHexString(Character.toTitleCase(i)));
                if (UCharacter.toUpperCase(i) != Character.toUpperCase(i))
                    trackDifference(p, i, "toUpperCase()", Integer.toHexString(UCharacter.toUpperCase(i)), Integer
                            .toHexString(Character.toUpperCase(i)));
            }
        }
        summary(p);
        p.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: UnicodeSetTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static int unescapeAt(String s, int[] offset16) {
    int c;
    int result = 0;
    int n = 0;
    int minDig = 0;
    int maxDig = 0;
    int bitsPerDigit = 4;
    int dig;
    int i;

    /* Check that offset is in range */
    int offset = offset16[0];
    int length = s.length();
    if (offset < 0 || offset >= length) {
        return -1;
    }

    /* Fetch first UChar after '\\' */
    c = UTF16.charAt(s, offset);
    offset += UTF16.getCharCount(c);

    /* Convert hexadecimal and octal escapes */
    switch (c) {
    case 'u':
        minDig = maxDig = 4;
        break;
        /*
     case 'U':
     minDig = maxDig = 8;
     break;
     case 'x':
     minDig = 1;
     maxDig = 2;
     break;
         */
    default:
        dig = UCharacter.digit(c, 8);
        if (dig >= 0) {
            minDig = 1;
            maxDig = 3;
            n = 1; /* Already have first octal digit */
            bitsPerDigit = 3;
            result = dig;
        }
        break;
    }
    if (minDig != 0) {
        while (offset < length && n < maxDig) {
            // TEMPORARY
            // TODO: Restore the char32-based code when UCharacter.digit
            // is working (Bug 66).

            //c = UTF16.charAt(s, offset);
            //dig = UCharacter.digit(c, (bitsPerDigit == 3) ? 8 : 16);
            c = s.charAt(offset);
            dig = Character.digit((char)c, (bitsPerDigit == 3) ? 8 : 16);
            if (dig < 0) {
                break;
            }
            result = (result << bitsPerDigit) | dig;
            //offset += UTF16.getCharCount(c);
            ++offset;
            ++n;
        }
        if (n < minDig) {
            return -1;
        }
        offset16[0] = offset;
        return result;
    }

    /* Convert C-style escapes in table */
    for (i=0; i<UNESCAPE_MAP.length; i+=2) {
        if (c == UNESCAPE_MAP[i]) {
            offset16[0] = offset;
            return UNESCAPE_MAP[i+1];
        } else if (c < UNESCAPE_MAP[i]) {
            break;
        }
    }

    /* If no special forms are recognized, then consider
     * the backslash to generically escape the next character. */
    offset16[0] = offset;
    return c;
}
 
Example 9
Source File: UCharacterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
* Tests for digit characters
*/
@Test
public void TestDigits()
{
    int digits[] = {0x0030, 0x000662, 0x000F23, 0x000ED5, 0x002160};

    //special characters not in the properties table
    int digits2[] = {0x3007, 0x004e00, 0x004e8c, 0x004e09, 0x0056d8,
                     0x004e94, 0x00516d, 0x4e03, 0x00516b, 0x004e5d};
    int nondigits[] = {0x0010, 0x000041, 0x000122, 0x0068FE};

    int digitvalues[] = {0, 2, 3, 5, 1};
    int digitvalues2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    int size  = digits.length;
    for (int i = 0; i < size; i ++) {
        if (UCharacter.isDigit(digits[i]) &&
            UCharacter.digit(digits[i]) != digitvalues[i])
        {
            errln("FAIL \\u" + hex(digits[i]) +
                    " expected digit with value " + digitvalues[i]);
            break;
        }
    }
    size = nondigits.length;
    for (int i = 0; i < size; i ++)
        if (UCharacter.isDigit(nondigits[i]))
        {
            errln("FAIL \\u" + hex(nondigits[i]) + " expected nondigit");
            break;
        }

    size = digits2.length;
    for (int i = 0; i < 10; i ++) {
        if (UCharacter.isDigit(digits2[i]) &&
            UCharacter.digit(digits2[i]) != digitvalues2[i])
        {
            errln("FAIL \\u" + hex(digits2[i]) +
                " expected digit with value " + digitvalues2[i]);
            break;
        }
    }
}