/*
Perform an unescape operation based on a Reader, writing the results to a Writer.

Note this reader is going to be read char-by-char, so some kind of buffering might be appropriate if this
is an inconvenience for the specific Reader implementation.
558994
static void unescape(final Reader reader, final Writer writer) throws IOException {

        if (reader == null) {
            return;
        }

        boolean isQuoted = false;

        int c1, c2; // c1: current char, c2: next char

        c2 = reader.read();

        if (c2 < 0) {
            // Nothing to output
            return;
        } else if (c2 == DOUBLE_QUOTE) {
            c1 = c2;
            c2 = reader.read();
            if (c2 < 0) {
                // Output is just a double-quote symbol
                // (...which by the way is not a valid CSV value, as a " is non-alphanumeric)
                writer.write(c1);
                return;
            } else {
                isQuoted = true;
            }
        }

        while (c2 >= 0) {

            c1 = c2;
            c2 = reader.read();

            /*
             * Shortcut: from an unescape point of view, we will ignore most characters
             */
            if (c1 != DOUBLE_QUOTE) {
                writer.write(c1);
                continue;
            }

            if (c2 < 0) {

                if (!isQuoted) {
                    // Last char is double-quote. If last and value is quoted, ignore - if not, write.
                    writer.write(c1);
                }
                continue;

            } else if (c2 == DOUBLE_QUOTE) {

                // This is an escaped double quote

                writer.write(DOUBLE_QUOTE);
                c1 = c2;
                c2 = reader.read();

            } else {
                // This is a non-escaped quote, which should only happen at the end, so this is actually
                // non-valid CSV... but anyway, we will be lenient and just write it

                writer.write(DOUBLE_QUOTE);

            }

        }

    }
755520
public static String unescape(String input, char escapeCharacter) {
      if (input == null) {
         return null;
      }
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < input.length(); ) {
         if (input.charAt(i) == escapeCharacter) {
            builder.append(input.charAt(i + 1));
            i = i + 2;
         } else {
            builder.append(input.charAt(i));
            i++;
         }
      }
      return builder.toString();
   }
435253
private String unescape(String value) {
    if (value.indexOf('&') < 0) {
      return value;
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < value.length(); i++) {
      char c = value.charAt(i);
      if (c == '&') {
        if (i + 2 < value.length() && value.charAt(i + 1) == '#') {
          if (value.charAt(i + 2) == 'x') {
            int x = i + 3;
            int charval = 0;
            while (x < value.length() && value.charAt(x) != ';') {
              int digit = "0123456789abcdef".indexOf(value.charAt(x));
              if (digit < 0) {
                digit = "0123456789ABCDEF".indexOf(value.charAt(x));
              }
              if (digit < 0) {
                return null;
              }
              charval = charval * 16 + digit;
              x++;
            }
            char hexchar = (char) charval;
            sb.append(hexchar);
            i = x;
          } else {
            int x = i + 2;
            int charval = 0;
            while (x < value.length() && value.charAt(x) != ';') {
              int digit = "0123456789".indexOf(value.charAt(x));
              if (digit < 0) {
                return null;
              }
              charval = charval * 10 + digit;
              x++;
            }
            char decchar = (char) charval;
            sb.append(decchar);
            i = x;
          }
        } else if (value.substring(i + 1).startsWith("lt;")) {
          sb.append('<');
          i += 3;
        } else if (value.substring(i + 1).startsWith("gt;")) {
          sb.append('>');
          i += 3;
        } else if (value.substring(i + 1).startsWith("amp;")) {
          sb.append('&');
          i += 4;
        } else if (value.substring(i + 1).startsWith("quot;")) {
          sb.append('"');
          i += 5;
        } else if (value.substring(i + 1).startsWith("apos;")) {
          sb.append('\'');
          i += 5;
        } else {
          return null;
        }

      } else {
        sb.append(c);
      }
    }
    return sb.toString();
  }
913079
public static String unescape(String str, char escape)
    {
        if (str == null) {
            return "";
        }
        StringBuffer buf = new StringBuffer();
        char[] array = str.toCharArray();
        boolean escaped = false;
        for (int i = 0; i < array.length; i++) {
            char ch = array[i];
            if (escaped) {
                buf.append(ch);
                escaped = false;
            } else {
                escaped = (ch == escape);
                if (!escaped) {
                    buf.append(ch);
                }
            }
        }
        return buf.toString();
    }
644560
static void unescape(final char[] text, final int offset, final int len, final Writer writer)
                         throws IOException {

        if (text == null) {
            return;
        }


        final int max = (offset + len);

        int readOffset = offset;
        int referenceOffset = offset;

        boolean isQuoted = false;

        for (int i = offset; i < max; i++) {

            final char c = text[i];

            /*
             * Shortcut: from an unescape point of view, we will ignore most characters
             */
            if (i > offset && c != DOUBLE_QUOTE) {
                continue;
            }


            /*
             * Check the only character that is really involved in unescape operations: the double-quote
             */
            if (c == DOUBLE_QUOTE) {

                if (i == offset) {

                    // If the first char is a double-quote, and so is the final one, we will need
                    // to remove them both.
                    if (i + 1 >= max) {
                        // Shortcut: The double-quote is the only char, just don't do anything
                        continue;
                    }

                    if (text[max - 1] == DOUBLE_QUOTE) {
                        // Confirmed: the value is enclosed in double-quotes. We should remove them.
                        isQuoted = true;
                        // Skip these double quotes in the final result;
                        referenceOffset = i + 1;
                        readOffset = i + 1;
                        continue;
                    }

                    // if none of the above are true, just consider the double-quotes a normal char
                    continue;

                } else {

                    if (isQuoted && i + 2 < max) {
                        // Value is quoted, and we are in the middle of it

                        final char c1 = text[i + 1];
                        if (c1 == DOUBLE_QUOTE) {
                            // This is an escaped double-quote: skip one of the chars (= unescape)
                            referenceOffset = i + 1;
                        } // else just write the quotes anyway (lenient behaviour). Not the last char, so don't remove.

                    } else if (isQuoted && i + 1 >= max) {

                        // This is the closing-double-quote, skip
                        referenceOffset = i + 1;

                    } else {
                        // else not quoted. Write the quotes anyway (lenient behaviour).
                        continue;
                    }

                }

            } else {

                // If the character is not a double-quote, we don't need to do anything at all
                continue;

            }


            /*
             * At this point we know for sure we will need some kind of unescape, so we
             * can copy all the contents pending up to this point.
             */

            if (i - readOffset > 0) {
                writer.write(text, readOffset, (i - readOffset));
            }

            i = referenceOffset;
            readOffset = i + 1;

            /*
             * --------------------------
             * Write the character
             * --------------------------
             */

            if (referenceOffset < max) {
                writer.write(c);
            }

        }


        /*
         * -----------------------------------------------------------------------------------------------
         * Final cleaning: append the remaining escaped text to the string builder and return.
         * -----------------------------------------------------------------------------------------------
         */

        if (max - readOffset > 0) {
            writer.write(text, readOffset, (max - readOffset));
        }

    }
736285
def unescape( text ):
    """"""
    
    if isinstance( text, basestring ):
        if '&amp;' in text:
            text = text.replace( '&amp;', '&' )
        if '&gt;' in text:
            text = text.replace( '&gt;', '>' )
        if '&lt;' in text:
            text = text.replace( '&lt;', '<' )
        if '&quot;' in text:
            text = text.replace( '&quot;', '\"' )

    return text
515949
static String unescapeText(String input)
    {
        ByteBuffer buffer = unescapeBytes(input);
        return new String(buffer.array(), buffer.position(), buffer.limit(), ISO_8859_1);
    }
1370975
private static String readLineFromReaderWithoutMark(Reader input)
            throws IOException {

        int c = input.read();
        if (c == -1)
            return null;
        StringBuilder line = new StringBuilder(expectedLineLength);

        while (c != EOF && c != '\n' && c != '\r') {
            char ch = (char) c;
            line.append(ch);
            c = input.read();
        }
        return line.toString();
    }
557895
function decodeStr(s, decoder) {
  try {
    return decoder(s);
  } catch (e) {
    return QueryString.unescape(s, true);
  }
}
745920
private static byte unescape(String s, int i) {
        return (byte) Integer.parseInt(s.substring(i+1,i+3),16);
    }
479662
private static String unescapeLiteral(final String text) {

        if (text == null) {
            return null;
        }

        StringBuilder strBuilder = null;

        final int max = text.length();

        int readOffset = 0;
        int referenceOffset = 0;

        char c;

        for (int i = 0; i < max; i++) {

            c = text.charAt(i);

            /*
             * Check the need for an unescape operation at this point
             */

            if (c != ESCAPE_PREFIX || (i + 1) >= max) {
                continue;
            }

            if (c == ESCAPE_PREFIX) {

                switch (text.charAt(i + 1)) {
                    case '\'': c = '\''; referenceOffset = i + 1; break;
                    case '\\': c = '\\'; referenceOffset = i + 1; break;
                    // We weren't able to consume any valid escape chars, just not consider it an escape operation
                    default: referenceOffset = i; break;
                }

            }


            /*
             * At this point we know for sure we will need some kind of unescape, so we
             * can increase the offset and initialize the string builder if needed, along with
             * copying to it all the contents pending up to this point.
             */

            if (strBuilder == null) {
                strBuilder = new StringBuilder(max + 5);
            }

            if (i - readOffset > 0) {
                strBuilder.append(text, readOffset, i);
            }

            i = referenceOffset;
            readOffset = i + 1;

            /*
             * Write the unescaped char
             */
            strBuilder.append(c);

        }


        /*
         * -----------------------------------------------------------------------------------------------
         * Final cleaning: return the original String object if no unescape was actually needed. Otherwise
         *                 append the remaining escaped text to the string builder and return.
         * -----------------------------------------------------------------------------------------------
         */

        if (strBuilder == null) {
            return text;
        }

        if (max - readOffset > 0) {
            strBuilder.append(text, readOffset, max);
        }

        return strBuilder.toString();

    }
407502
def unescape(cls, text: str) -> str:
        
        chop = text.split("\\", 1)
        try:
            return (chop[0] if len(chop) == 1
                    else chop[0] + cls.unescape_map[chop[1][0]] +
                    cls.unescape(chop[1][1:]))
        except KeyError:
            raise InvalidArgument(text) from None
87644
public static String unescape(String s) {
        StringBuilder buf = new StringBuilder();
        int[] pos = new int[1];
        for (int i=0; i<s.length(); ) {
            char c = s.charAt(i++);
            if (c == '\\') {
                pos[0] = i;
                int e = unescapeAt(s, pos);
                if (e < 0) {
                    throw new IllegalArgumentException("Invalid escape sequence " +
                            s.substring(i-1, Math.min(i+8, s.length())));
                }
                buf.appendCodePoint(e);
                i = pos[0];
            } else {
                buf.append(c);
            }
        }
        return buf.toString();
    }
1258983
def _unescape_match(self, match):
        """"""
        char = match.group(1)
        if char in self.ESCAPE_LOOKUP:
            return self.ESCAPE_LOOKUP[char]
        elif not char:
            raise KatcpSyntaxError("Escape slash at end of argument.")
        else:
            raise KatcpSyntaxError("Invalid escape character %r." % (char,))
595828
def unescape(self):
        
        if self.escaped:
            post = sax.encoder.decode(self)
            return Text(post, lang=self.lang)
        return self
390535
function (bytes) {
                var str = '';
                for (var i = 0, l = bytes.length; i < l; i++) {
                    if (bytes[i] < 0x7F) {
                        str += String.fromCharCode(bytes[i]);
                    }
                    else {
                        str += '%' + (256 + bytes[i]).toString(16).slice(1);
                    }
                }

                return unescape(str);
            }
471245
public static String unescapeNewlines(String str) {
    boolean hadSlash = false;
    final int len = str.length();
    StringWriter out = new StringWriter(len);
    for (int i = 0; i < len; i++) {
      char c = str.charAt(i);
      if (hadSlash) {
        switch (c) {
          case 'n':
            out.write('\n');
            break;
          case '\\':
            out.write('\\');
            break;
          default:
            out.write(c);
        }
        hadSlash = false;
      } else {
        if (c == '\\')
          hadSlash = true;
        else
          out.write(c);
      }
    }
    return out.toString();
  }