Java String Unescape unescape(String s)

Here you can find the source of unescape(String s)

Description

Unescape the specified string.

License

Open Source License

Parameter

Parameter Description
s The string to unescape.

Exception

Parameter Description
IllegalArgumentException Signals a malformed string.

Return

The unescaped string.

Declaration

public static String unescape(String s) 

Method Source Code

//package com.java2s;
/*//from w w w  .j  a v a 2  s  .  c  om
 * xtc - The eXTensible Compiler
 * Copyright (C) 2004-2011 Robert Grimm
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

public class Main {
    /**
     * Unescape the specified string. This method unescapes basic Java
     * escapes ('<code>\b</code>', '<code>\f</code>', '<code>\n</code>',
     * '<code>\r</code>', '<code>\t</code>', '<code>\"</code>',
     * '<code>\'</code>', and '<code>\\</code>'), the additional
     * standard C escapes ('<code>\a</code>', '<code>\v</code>', and
     * '<code>\?</code>'), standard C's octal escapes, and standard C's
     * and Java's Unicode escapes.  To support regex-like character
     * classes, it also unescapes '<code>\-</code>', '<code>\[</code>',
     * and '<code>\]</code>'.
     *
     * @param s The string to unescape.
     * @return The unescaped string.
     * @throws IllegalArgumentException Signals a malformed string.
     */
    public static String unescape(String s) {
        if (-1 == s.indexOf('\\')) {
            return s;
        }

        final int length = s.length();
        StringBuilder buf = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if ('\\' != c) {
                buf.append(c);
            } else {
                i++;
                if (i >= length) {
                    throw new IllegalArgumentException("incomplete escape sequence");
                }

                c = s.charAt(i);

                switch (c) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7': {
                    int val = c;

                    if (i + 1 < length) {
                        c = s.charAt(i + 1);

                        if (('0' <= c) && (c <= '7')) {
                            val = (val * 8) + c;
                            i++;

                            if (i + 1 < length) {
                                c = s.charAt(i + 1);

                                if (('0' <= c) && (c <= '7')) {
                                    val = (val * 8) + c;
                                    i++;
                                }
                            }
                        }
                    }

                    buf.append((char) val);
                }
                    break;
                case '"':
                    buf.append('"');
                    break;
                case '\'':
                    buf.append('\'');
                    break;
                case '-':
                    buf.append('-');
                    break;
                case '?':
                    buf.append('?');
                    break;
                case '[':
                    buf.append('[');
                    break;
                case '\\':
                    buf.append('\\');
                    break;
                case ']':
                    buf.append(']');
                    break;
                case 'a':
                    buf.append('\u0007');
                    break;
                case 'b':
                    buf.append('\b');
                    break;
                case 'f':
                    buf.append('\f');
                    break;
                case 'n':
                    buf.append('\n');
                    break;
                case 'r':
                    buf.append('\r');
                    break;
                case 't':
                    buf.append('\t');
                    break;
                case 'u':
                    i += 4;
                    int n;

                    if (i >= length) {
                        throw new IllegalArgumentException(
                                "incomplete universal character" + " name " + s.substring(i - 3));
                    }

                    try {
                        n = Integer.parseInt(s.substring(i - 3, i + 1), 16);
                    } catch (NumberFormatException x) {
                        throw new IllegalArgumentException(
                                "malformed universal character" + " name " + s.substring(i - 3, i + 1));
                    }
                    buf.append((char) n);
                    break;
                case 'v':
                    buf.append('\u000b');
                    break;
                default:
                    throw new IllegalArgumentException("illegal escaped character \'\\" + c + "\'");
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. unescape(String s)
  2. unescape(String s)
  3. Unescape(String s)
  4. unescape(String s)
  5. unescape(String s)
  6. unescape(String s)
  7. unescape(String s)
  8. unescape(String s)
  9. unescape(String s)