Java Java String Unescape unescapeJava(final String str)

Here you can find the source of unescapeJava(final String str)

Description

Unescape java.

License

Open Source License

Parameter

Parameter Description
str the str

Return

the string

Declaration

static public String unescapeJava(final String str) 

Method Source Code

//package com.java2s;
/*********************************************************************************************
 *
 *
 * 'StringUtils.java', in plugin 'msi.gama.core', is part of the source code of the
 * GAMA modeling and simulation platform.
 * (c) 2007-2014 UMI 209 UMMISCO IRD/UPMC & Partners
 *
 * Visit https://code.google.com/p/gama-platform/ for license information and developers contact.
 *
 *
 **********************************************************************************************/

public class Main {
    /**// w  ww  .j  a va  2 s. c o  m
     * Unescape java.
     *
     * @param str the str
     *
     * @return the string
     */
    static public String unescapeJava(final String str) {
        if (str == null) {
            return null;
        }
        StringBuilder writer = new StringBuilder(str.length());
        unescapeJava(writer, str);
        String result = writer.toString();
        writer.setLength(0);
        return result;

    }

    /**
     * Unescape java.
     *
     * @param out the out
     * @param str the str
     */
    static private void unescapeJava(final StringBuilder writer, final String str) {
        if (str == null) {
            return;
        }
        final int sz = str.length();

        boolean hadSlash = false;
        boolean inUnicode = false;
        for (int i = 0; i < sz; i++) {
            final char ch = str.charAt(i);
            if (inUnicode) {
                // if in unicode, then we're reading unicode
                // values in somehow
                StringBuilder unicode = new StringBuilder(4);
                unicode.append(ch);
                if (unicode.length() == 4) {
                    // digits
                    // which represents our unicode character
                    final int value = Integer.parseInt(unicode.toString(), 16);
                    writer.append((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                }
                continue;
            }
            if (hadSlash) {
                // handle an escaped value
                hadSlash = false;
                switch (ch) {
                case '\\':
                    writer.append('\\');
                    break;
                case '\'':
                    writer.append('\'');
                    break;
                case '\"':
                    writer.append('"');
                    break;
                case 'r':
                    writer.append('\r');
                    break;
                case 'f':
                    writer.append('\f');
                    break;
                case 't':
                    writer.append('\t');
                    break;
                case 'n':
                    writer.append('\n');
                    break;
                case 'b':
                    writer.append('\b');
                    break;
                case 'u': {
                    // uh-oh, we're in unicode country....
                    inUnicode = true;
                    break;
                }
                default:
                    writer.append(ch);
                    break;
                }
                continue;
            } else if (ch == '\\') {
                hadSlash = true;
                continue;
            }
            writer.append(ch);
        }
        if (hadSlash) {
            // string, let's output it anyway.
            writer.append('\\');
        }
    }
}

Related

  1. unescapeJava(final String s)
  2. unescapeJava(final String str)
  3. unescapeJava(String escaped)
  4. unescapeJava(String s)
  5. unescapeJava(String str)
  6. unescapeJava(String value)