Java String Unquote unquoteCString(String str)

Here you can find the source of unquoteCString(String str)

Description

Unescape the textual representation of a string specific to SDF and Maude.

License

Open Source License

Declaration

public static String unquoteCString(String str) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww  w  . j a v  a 2s . co  m
     * Unescape the textual representation of a string specific to SDF and Maude.
     * It removes the double quote at the beginning and end, and transforms special sequence
     * of characters like "\n" into the newline character.
     */
    public static String unquoteCString(String str) {
        return unquoteCString(str, '"');
    }

    public static String unquoteCString(String str, char delimiter) {
        StringBuilder sb = new StringBuilder();
        if (str.charAt(0) != delimiter) {
            throw new IllegalArgumentException(
                    "Expected to find " + delimiter + " at the beginning of string: " + str);
        }
        if (str.charAt(str.length() - 1) != delimiter) {
            throw new IllegalArgumentException("Expected to find " + delimiter + " at the end of string: " + str);
        }
        for (int i = 1; i < str.length() - 1; i++) {
            if (str.charAt(i) == '\\') {
                if (str.charAt(i + 1) == '\\')
                    sb.append('\\');
                else if (str.charAt(i + 1) == 'n')
                    sb.append('\n');
                else if (str.charAt(i + 1) == 'r')
                    sb.append('\r');
                else if (str.charAt(i + 1) == 't')
                    sb.append('\t');
                else if (str.charAt(i + 1) == 'f')
                    sb.append('\f');
                else if (str.charAt(i + 1) == delimiter)
                    sb.append(delimiter);
                else if (str.charAt(i + 1) >= '0' && str.charAt(i + 1) <= '9') {
                    // found an octal value
                    int a2 = str.charAt(i + 1) - '0';
                    int a1 = str.charAt(i + 2) - '0';
                    if (a1 < 0 || a1 > 9)
                        throw new IllegalArgumentException("Malformed octal value in string:" + str);
                    int a0 = str.charAt(i + 3) - '0';
                    if (a0 < 0 || a0 > 9)
                        throw new IllegalArgumentException("Malformed octal value in string:" + str);
                    int decimal = a2 * 8 * 8 + a1 * 8 + a0;
                    sb.append((char) decimal);
                    i++;
                    i++;
                }
                i++;
            } else
                sb.append(str.charAt(i));
        }

        return sb.toString();
    }
}

Related

  1. UnQuote(String value)
  2. unquote(String value)
  3. unquote(String value)
  4. unquoteAtom(String atom)
  5. unquoteCharacterLiteral(String charLiteralStr)
  6. unquotedCharLiteral(char c)
  7. unquotedString(String str)
  8. unquoteHtmlContent(String x)
  9. unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)