to Code Point Array - Android java.lang

Android examples for java.lang:String Unicode

Description

to Code Point Array

Demo Code

/*//from  ww w.  j  av  a  2 s.co m
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import android.text.TextUtils;
import java.util.Arrays;

public class Main{
  private static final int[] EMPTY_CODEPOINTS = {};
    public static int[] toCodePointArray(final CharSequence charSequence) {
        return toCodePointArray(charSequence, 0, charSequence.length());
    }
    /**
     * Converts a range of a string to an array of code points.
     * @param charSequence the source string.
     * @param startIndex the start index inside the string in java chars, inclusive.
     * @param endIndex the end index inside the string in java chars, exclusive.
     * @return a new array of code points. At most endIndex - startIndex, but possibly less.
     */
    public static int[] toCodePointArray(final CharSequence charSequence,
            final int startIndex, final int endIndex) {
        final int length = charSequence.length();
        if (length <= 0) {
            return EMPTY_CODEPOINTS;
        }
        final int[] codePoints = new int[Character.codePointCount(
                charSequence, startIndex, endIndex)];
        copyCodePointsAndReturnCodePointCount(codePoints, charSequence,
                startIndex, endIndex, false /* downCase */);
        return codePoints;
    }
    public static int copyCodePointsAndReturnCodePointCount(
        final int[] destination, final CharSequence charSequence,
        final int startIndex, final int endIndex, final boolean downCase) {
    int destIndex = 0;
    for (int index = startIndex; index < endIndex; index = Character
            .offsetByCodePoints(charSequence, index, 1)) {
        final int codePoint = Character
                .codePointAt(charSequence, index);
        // TODO: stop using this, as it's not aware of the locale and does not always do
        // the right thing.
        destination[destIndex] = downCase ? Character
                .toLowerCase(codePoint) : codePoint;
        destIndex++;
    }
    return destIndex;
}

}

Related Tutorials