Java String Unescape unescape(String original, char[] spec)

Here you can find the source of unescape(String original, char[] spec)

Description

unescape

License

Open Source License

Declaration

public static String unescape(String original, char[] spec) 

Method Source Code

//package com.java2s;
/*   Avuna HTTPD - General Server Applications
 Copyright (C) 2015 Maxwell Bruce/*from  w w w.  j a v a  2s .  c o  m*/

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String unescape(String original, char[] spec) {
        String n = original;
        for (char c : spec) {
            n = n.replace("\\" + c, "" + c);
        }
        n = original.replace("\\\\", "\\");

        return n;
    }

    public static char[] replace(char[] input, char[] rf, char[] rt) {
        int isl = input.length;
        int rfsl = rf.length;
        int rtsl = rt.length;
        int ml = 0;
        int mc = 0;
        for (int i = 0; i < isl; i++) {
            boolean im = input[i] == rf[ml];
            if (im) {
                ml++;
                if (ml == rfsl) {
                    mc++;
                    ml = 0;
                    continue;
                }
            } else if (ml > 0) {
                ml = 0;
            }
        }
        ml = 0;
        char[] output = new char[isl - ((rfsl - rtsl) * mc)];
        int mi = 0;
        for (int i = 0; i < isl; i++) {
            boolean im = input[i] == rf[ml];
            if (im) {
                ml++;
                if (ml == rfsl) {
                    int os = mi - ml + 1;
                    System.arraycopy(rt, 0, output, os, rt.length);
                    if (ml > rtsl) {
                        int ofs = ml - rtsl;
                        int oi = os + ofs;
                        if (oi + ofs < output.length)
                            System.arraycopy(new char[ofs], 0, output, oi,
                                    ofs);
                    }
                    mi += ml - rtsl - 1;
                    ml = 0;
                    continue;
                }
            } else if (ml > 0) {
                ml = 0;
            }
            output[mi++] = input[i];
        }
        return output;
    }

    public static String replace(String input, String rf, String rt) {
        return new String(replace(input.toCharArray(), rf.toCharArray(),
                rt.toCharArray()));
    }
}

Related

  1. unescape(String line)
  2. unescape(String line, int start, int end)
  3. unescape(String literal, String escapedChars)
  4. unescape(String name)
  5. unescape(String oldStr)
  6. unescape(String property)
  7. unescape(String quoted)
  8. unEscape(String s)
  9. unescape(String s)