Java Unicode Escape unicodeEscape(StringBuilder result, int in, int index)

Here you can find the source of unicodeEscape(StringBuilder result, int in, int index)

Description

Given a char, and its location in a StringBuilder, replace it with an escaped unicode if necessary

License

Open Source License

Return

The new index into StringBuilder

Declaration

private static int unicodeEscape(StringBuilder result, int in, int index) 

Method Source Code

    //package com.java2s;
    //License from project: Open Source License 

    public class Main {
        static final char[] HEX_DIGITS = new char[] { '0', '1', '2', '3', '4',
                '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        /**/* w  w  w.  jav  a  2s . c o m*/
         * Given a char, and its location in a StringBuilder,
         *  replace it with an escaped unicode if necessary
         * @return The new index into StringBuilder
         */
        private static int unicodeEscape(StringBuilder result, int in, int index) {
            // utf8 within [32, 127] are fine
            if (in >= 32 && in <= 127) {
                return index;
            }

            if (in > '\uffff') {
                final char[] surrogatePair = Character.toChars(in);
                final String first = hex(surrogatePair[0]);
                final String second = hex(surrogatePair[1]);

                // ensure we have capacity to replace 2 chars
                // (IE a surrogate pair) with \\u + first + \\u + second
result.ensureCapacity(result.capacity() + 2 + first.length()
                        + second.length());
                result.delete(index, index + 2);
                result.insert(index++, '\\');
                result.insert(index++, 'u');
                result.insert(index, first);
                index += first.length();

                result.insert(index++, '\\');
                result.insert(index++, 'u');
                result.insert(index, second);
                index += second.length() - 1;
            } else {
                // ensure we have capacity to replace 1 char with 6
                result.ensureCapacity(result.capacity() + 5);
                result.deleteCharAt(index);
                result.insert(index++, '\\');
                result.insert(index++, 'u');
                result.insert(index++, HEX_DIGITS[in >> 12 & 15]);
                result.insert(index++, HEX_DIGITS[in >> 8 & 15]);
                result.insert(index++, HEX_DIGITS[in >> 4 & 15]);
                result.insert(index, HEX_DIGITS[in & 15]);
            }

            return index;
        }

        private static String hex(int codepoint) {
            return Integer.toHexString(codepoint).toUpperCase();
        }
    }

Related

  1. unicodeEscape(char c)
  2. unicodeEscape(Character ch)
  3. unicodeEscape(String s)
  4. unicodeEscape(String s)
  5. unicodeEscaped(char ch)
  6. unicodeEscaped(Character ch)
  7. unicodeEscaped(Character ch)
  8. unicodeEscaped(final Character ch)