Java String Unescape unescape(String s, char[] charsToEscape)

Here you can find the source of unescape(String s, char[] charsToEscape)

Description

Given an escaped string returned by #escape and the original set of characters to escape, returns the original string.

License

Apache License

Parameter

Parameter Description
s the escaped string
charsToEscape the set of characters that need to be escaped

Return

the original unescaped string

Declaration

public static String unescape(String s, char[] charsToEscape) 

Method Source Code

//package com.java2s;
/**/*from   w w  w.  j a va  2 s. co  m*/
 * Copyright 2008 - CommonCrawl Foundation
 * 
 * CommonCrawl licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * A backslash, the character used to begin an escape sequence.
     */
    public static final char ESCAPE = '\\';

    /**
     * Given an escaped string returned by {@link #escape} and the original set of
     * characters to escape, returns the original string.
     * 
     * @see #ESCAPE
     * @see #escape
     * 
     * @param s
     *          the escaped string
     * @param charsToEscape
     *          the set of characters that need to be escaped
     * 
     * @return the original unescaped string
     */
    public static String unescape(String s, char[] charsToEscape) {
        StringBuilder buf = new StringBuilder(s.length());
        boolean inEscapeSequence = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (inEscapeSequence) {
                if (c != ESCAPE && !hasChar(charsToEscape, c)) {
                    throw new IllegalArgumentException(c + " is not a valid escape sequence character");
                }
                buf.append(c);
                inEscapeSequence = false;
            } else if (hasChar(charsToEscape, c)) {
                throw new IllegalArgumentException(c + " must be escaped");
            } else if (c == ESCAPE) {
                inEscapeSequence = true;
            } else {
                buf.append(c);
            }
        }
        if (inEscapeSequence) {
            throw new IllegalArgumentException("Unterminated escape sequence");
        }
        return buf.toString();
    }

    /**
     * Checks if a <tt>char[]</tt> contains a particular character.
     * 
     * @param chars
     *          the array to search in
     * @param target
     *          the <tt>char</tt> to search for
     * 
     * @return <tt>true</tt> if <tt>chars</tt> contains <tt>target</tt>, or
     *         <tt>false</tt> otherwise
     */
    private static boolean hasChar(char[] chars, char target) {
        // The escape set will most likely be small, so just loop through it
        for (char c : chars) {
            if (c == target) {
                return true;
            }
        }
        return false;
    }
}

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, int i)
  7. unescape(String s, String toUnescape)
  8. unescape(String source)
  9. unescape(String src)