Java String Quote Unescape unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb)

Here you can find the source of unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb)

Description

Unescape the given string

License

Apache License

Parameter

Parameter Description
string a parameter
quoteChar a parameter
useAsciiExcapes a parameter
sb a scratch buffer to use

Declaration

public static String unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from  w w  w . ja  va2s .  c o m
     * Unescape the given string
     * @param string
     * @param quoteChar
     * @param useAsciiExcapes
     * @param sb a scratch buffer to use
     * @return
     */
    public static String unescape(CharSequence string, int quoteChar, boolean useAsciiEscapes, StringBuilder sb) {
        boolean escaped = false;

        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (escaped) {
                switch (c) {
                case 'b':
                    sb.append('\b');
                    break;
                case 't':
                    sb.append('\t');
                    break;
                case 'n':
                    sb.append('\n');
                    break;
                case 'f':
                    sb.append('\f');
                    break;
                case 'r':
                    sb.append('\r');
                    break;
                case 'u':
                    i = parseNumericValue(string, sb, i, 0, 4, 4);
                    //TODO: this should probably be strict about needing 4 digits
                    break;
                default:
                    if (c == quoteChar) {
                        sb.append(quoteChar);
                    } else if (useAsciiEscapes) {
                        int value = Character.digit(c, 8);
                        if (value == -1) {
                            sb.append(c);
                        } else {
                            int possibleDigits = value < 3 ? 2 : 1;
                            int radixExp = 3;
                            i = parseNumericValue(string, sb, i, value, possibleDigits, radixExp);
                        }
                    }
                }
                escaped = false;
            } else {
                if (c == '\\') {
                    escaped = true;
                } else if (c == quoteChar) {
                    break;
                } else {
                    sb.append(c);
                }
            }
        }
        //TODO: should this be strict?
        //if (escaped) {
        //throw new FunctionExecutionException();
        //}
        return sb.toString();
    }

    private static int parseNumericValue(CharSequence string, StringBuilder sb, int i, int value,
            int possibleDigits, int radixExp) {
        for (int j = 0; j < possibleDigits; j++) {
            if (i + 1 == string.length()) {
                break;
            }
            char digit = string.charAt(i + 1);
            int val = Character.digit(digit, 1 << radixExp);
            if (val == -1) {
                break;
            }
            i++;
            value = (value << radixExp) + val;
        }
        sb.append((char) value);
        return i;
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim) {
        return toString(array, delim, true);
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim, boolean includeBrackets) {
        if (array == null) {
            return ""; //$NON-NLS-1$
        }
        final StringBuffer sb = new StringBuffer();
        if (includeBrackets) {
            sb.append('[');
        }
        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(array[i]);
        }
        if (includeBrackets) {
            sb.append(']');
        }
        return sb.toString();
    }

    /**
     * Return a stringified version of the array, using a ',' as a delimiter
     * @param array the array
     * @return the string form of the array
     * @see #toString(Object[], String)
     */
    public static String toString(final Object[] array) {
        return toString(array, ",", true); //$NON-NLS-1$
    }
}

Related

  1. unescapeDoubleQuote(String string)
  2. unescapeDoubleQuote(String stringToReplace)
  3. unescapeDoubleQuotes(String string)
  4. unescapeDoubleQuotesAndBackslashes(final String input)