Java String Unquote unquoteAtom(String atom)

Here you can find the source of unquoteAtom(String atom)

Description

Unquote atom (replace ' at the start and end).

License

Open Source License

Parameter

Parameter Description
atom quoted atom

Return

unquoted atom

Declaration

public static String unquoteAtom(String atom) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of the Prolog Development Tool (PDT)
 * /*from   ww w. j  av  a 2s . c  o  m*/
 * Author: Lukas Degener (among others)
 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
 * Mail: pdt@lists.iai.uni-bonn.de
 * Copyright (C): 2004-2012, CS Dept. III, University of Bonn
 * 
 * All rights reserved. This program is  made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 ****************************************************************************/

public class Main {
    /**
     * Unquote atom (replace ' at the start and end).
     * 
     * @param atom quoted atom
     * @return unquoted atom
     */
    public static String unquoteAtom(String atom) {
        atom = atom.trim();
        if (atom.length() == 0 || atom.charAt(0) != '\'') {
            return atom;
        }
        atom = atom.substring(1, atom.length() - 1);
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < atom.length(); i++) {
            char c = atom.charAt(i);
            if (c == '\\') {
                int len = appendUnescapedChar(atom, i, sb);
                i += len - 1;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static int appendUnescapedChar(String image, int i, StringBuffer sb) {
        if (image.length() <= i + 1) {
            sb.append('\\');
            return 1;
        }
        char c = image.charAt(i + 1);
        if (Character.isDigit(c)) {
            return appendUnescapedOctalCharSpec(image, i, sb);
        }
        switch (c) {
        case 'a':
            // sb.append('\a'); there is no bell char in java
            return 2;
        case 'b':
            sb.append('\b');
            return 2;
        case 'c':
            // sb.append('\c'); not supported
            return 2;
        case '\n':
            // ignoring
            return 2;
        case 'f':
            sb.append('\f');
            return 2;
        case 'n':
            sb.append('\n');
            return 2;
        case 'r':
            sb.append('\r');
            return 2;
        case 't':
            sb.append('\t');
            return 2;
        case 'v':
            // sb.append('\v'); vertical tabs are not supported in java
            return 2;
        case 'x':
            return appendUnescapedHexCharSpec(image, i, sb);
        case '\\':
            sb.append('\\');
            return 2;
        case '\'':
            sb.append('\'');
            return 2;
        default:
            sb.append('\\');
            return 1;
        //         sb.append(c);
        //         return 2;
        }
    }

    private static int appendUnescapedOctalCharSpec(String image, int i, StringBuffer sb) {
        String val = "";
        int j = i + 1;
        while (j < image.length() && j < i + 4 && isOctDigit(image.charAt(j))) {
            val += image.charAt(j);
            j++;
        }
        sb.append((char) Integer.parseInt(val, 8));
        if (j < image.length() && image.charAt(j) == '\\') {
            return 1 + j - i;
        }
        return j - i;
    }

    private static int appendUnescapedHexCharSpec(String image, int i, StringBuffer sb) {

        String val = "";
        int j = i + 2;
        while (j < image.length() && j < i + 4 && isHexDigit(image.charAt(j))) {
            val += image.charAt(j);
            j++;
        }
        sb.append((char) Byte.parseByte(val));
        if (j < image.length() && image.charAt(j) == '\\') {
            return 1 + j - i;
        }
        return j - i;
    }

    private static boolean isOctDigit(char c) {

        return Character.isDigit(c) || '0' <= c && c <= '7';
    }

    private static boolean isHexDigit(char c) {

        return Character.isDigit(c) || 'a' <= Character.toLowerCase(c) && Character.toLowerCase(c) <= 'f';
    }
}

Related

  1. unquote(String value)
  2. unquote(String value)
  3. UnQuote(String value)
  4. unquote(String value)
  5. unquote(String value)
  6. unquoteCharacterLiteral(String charLiteralStr)
  7. unquoteCString(String str)
  8. unquotedCharLiteral(char c)
  9. unquotedString(String str)