Example usage for java.lang Character isSupplementaryCodePoint

List of usage examples for java.lang Character isSupplementaryCodePoint

Introduction

In this page you can find the example usage for java.lang Character isSupplementaryCodePoint.

Prototype

public static boolean isSupplementaryCodePoint(int codePoint) 

Source Link

Document

Determines whether the specified character (Unicode code point) is in the supplementary character range.

Usage

From source file:se.sawano.java.security.otp.google.keyuri.UnicodeEscaper.java

/**
 * Scans a sub-sequence of characters from a given {@link CharSequence}, returning the index of
 * the next character that requires escaping.
 *
 * <p><b>Note:</b> When implementing an escaper, it is a good idea to override this method for
 * efficiency. The base class implementation determines successive Unicode code points and invokes
 * {@link #escape(int)} for each of them. If the semantics of your escaper are such that code
 * points in the supplementary range are either all escaped or all unescaped, this method can be
 * implemented more efficiently using {@link CharSequence#charAt(int)}.
 *
 * <p>Note however that if your escaper does not escape characters in the supplementary range, you
 * should either continue to validate the correctness of any surrogate characters encountered or
 * provide a clear warning to users that your escaper does not validate its input.
 *
 * <p>See {@link com.google.common.net.PercentEscaper} for an example.
 *
 * @param csq/*from ww  w . jav  a2 s.  com*/
 *         a sequence of characters
 * @param start
 *         the index of the first character to be scanned
 * @param end
 *         the index immediately after the last character to be scanned
 *
 * @throws IllegalArgumentException
 *         if the scanned sub-sequence of {@code csq} contains invalid surrogate pairs
 */
protected int nextEscapeIndex(CharSequence csq, int start, int end) {
    int index = start;
    while (index < end) {
        int cp = codePointAt(csq, index, end);
        if (cp < 0 || escape(cp) != null) {
            break;
        }
        index += Character.isSupplementaryCodePoint(cp) ? 2 : 1;
    }
    return index;
}

From source file:se.sawano.java.security.otp.google.keyuri.UnicodeEscaper.java

/**
 * Returns the escaped form of a given literal string, starting at the given index. This method is
 * called by the {@link #escape(String)} method when it discovers that escaping is required. It is
 * protected to allow subclasses to override the fastpath escaping function to inline their
 * escaping test. See {@link CharEscaperBuilder} for an example usage.
 *
 * <p>This method is not reentrant and may only be invoked by the top level
 * {@link #escape(String)} method.//from  w  w  w.j  a  va 2s . c o  m
 *
 * @param s
 *         the literal string to be escaped
 * @param index
 *         the index to start escaping from
 *
 * @return the escaped form of {@code string}
 *
 * @throws NullPointerException
 *         if {@code string} is null
 * @throws IllegalArgumentException
 *         if invalid surrogate characters are encountered
 */
protected final String escapeSlow(String s, int index) {
    int end = s.length();

    // Get a destination buffer and setup some loop variables.
    char[] dest = new char[1024];
    int destIndex = 0;
    int unescapedChunkStart = 0;

    while (index < end) {
        int cp = codePointAt(s, index, end);
        if (cp < 0) {
            throw new IllegalArgumentException("Trailing high surrogate at end of input");
        }
        // It is possible for this to return null because nextEscapeIndex() may
        // (for performance reasons) yield some false positives but it must never
        // give false negatives.
        char[] escaped = escape(cp);
        int nextIndex = index + (Character.isSupplementaryCodePoint(cp) ? 2 : 1);
        if (escaped != null) {
            int charsSkipped = index - unescapedChunkStart;

            // This is the size needed to add the replacement, not the full
            // size needed by the string. We only regrow when we absolutely must.
            int sizeNeeded = destIndex + charsSkipped + escaped.length;
            if (dest.length < sizeNeeded) {
                int destLength = sizeNeeded + (end - index) + DEST_PAD;
                dest = growBuffer(dest, destIndex, destLength);
            }
            // If we have skipped any characters, we need to copy them now.
            if (charsSkipped > 0) {
                s.getChars(unescapedChunkStart, index, dest, destIndex);
                destIndex += charsSkipped;
            }
            if (escaped.length > 0) {
                System.arraycopy(escaped, 0, dest, destIndex, escaped.length);
                destIndex += escaped.length;
            }
            // If we dealt with an escaped character, reset the unescaped range.
            unescapedChunkStart = nextIndex;
        }
        index = nextEscapeIndex(s, nextIndex, end);
    }

    // Process trailing unescaped characters - no need to account for escaped
    // length or padding the allocation.
    int charsSkipped = end - unescapedChunkStart;
    if (charsSkipped > 0) {
        int endIndex = destIndex + charsSkipped;
        if (dest.length < endIndex) {
            dest = growBuffer(dest, destIndex, endIndex);
        }
        s.getChars(unescapedChunkStart, end, dest, destIndex);
        destIndex = endIndex;
    }
    return new String(dest, 0, destIndex);
}

From source file:txyd.util.StringUtils.java

/**
 * ?????// w ww  .  j  a  v  a 2  s. co  m
 * charjavachar16????16???????
 * <p>
 * ??U+0000U+10FFFFU+0000U+FFFFU+10000U+10FFFF
 * <p>
 * ????1216??UTF-16
 * ??????????U+D800U+DFFF
 * ???????
 *
 * @param sentence
 */
public static void printlnChar(String sentence) {
    //        String sentence = "\u03C0\uD835\uDD6B";//?
    //        String sentence = "";
    int lengthU = sentence.length();
    int lengthP = sentence.codePointCount(0, lengthU);
    //      System.out.println(lengthU);        // ??
    //      System.out.println(lengthP);        // ???
    if (lengthU != lengthP) {//
        for (int i = 0; i < lengthU; i++) {
            int codePoint = sentence.codePointAt(i);
            if (Character.isSupplementaryCodePoint(codePoint)) {
                System.out.println(String.valueOf(Character.toChars(codePoint)));
                i++;
            } else {
                System.out.println(sentence.charAt(i));
            }
        }
    }
}