review request for 6798511/6860431: Include functionality of Surrogate in Character

Martin Buchholz martinrb at google.com
Tue Mar 16 21:36:18 UTC 2010


On Tue, Mar 16, 2010 at 13:58, Ulf Zibis <Ulf.Zibis at gmx.de> wrote:

> Additionally, toUpperCaseCharArray(), codePointCountImpl(), String(int[],
> int, int) would profit from consecutive use of isBMPCodePoint +
> isSupplementaryCodePoint() or isHighSurrogate() + isLowSurrogate.

For codePointCountImpl(), I do not agree.

For String(int[], int, int), I do agree.

Here is my latest more readable and more performant implementation:

        int end = offset + count;

        // Pass 1: Compute precise size of char[]
        int n = 0;
        for (int i = offset; i < end; i++) {
            int c = codePoints[i];
            if (Character.isBMPCodePoint(c))
                n += 1;
            else if (Character.isSupplementaryCodePoint(c))
                n += 2;
            else throw new IllegalArgumentException(Integer.toString(c));
        }

        // Pass 2: Allocate and fill in char[]
        char[] v = new char[n];
        for (int i = offset, j = 0; i < end; i++) {
            int c = codePoints[i];
            if (Character.isBMPCodePoint(c)) {
                v[j++] = (char) c;
            } else {
                Character.toSurrogates(c, v, j);
                j += 2;
            }
        }

Martin



More information about the core-libs-dev mailing list