Unquote a string, NOT converting unicode sequences

@param s       The string to unquote.
@param lenient if true, will leave in backslashes that aren't valid escapes
@return quoted string
425277
public static String unquoteOnly(String s, boolean lenient) {
        if (s == null)
            return null;
        if (s.length() < 2)
            return s;

        char first = s.charAt(0);
        char last = s.charAt(s.length() - 1);
        if (first != last || (first != '"' && first != '\''))
            return s;

        StringBuilder b = new StringBuilder(s.length() - 2);
        boolean escape = false;
        for (int i = 1; i < s.length() - 1; i++) {
            char c = s.charAt(i);

            if (escape) {
                escape = false;
                if (lenient && !isValidEscaping(c)) {
                    b.append('\\');
                }
                b.append(c);
            } else if (c == '\\') {
                escape = true;
            } else {
                b.append(c);
            }
        }

        return b.toString();
    }
139457
public static StringBuilder maybeQuoteIdentifier(String s) {
        final EscapeStringResult escapeString = escapeString(s, '\'');
        final StringBuilder result = escapeString.buffer;

        final boolean need_quotes = s.isEmpty() ||
                escapeString.needQuotes ||
                KEYWORDS.contains(s) ||
                !IDENTIFIER_MATCHER.test(s);

        if (need_quotes) {
            result.insert(0, '\'');
            result.append('\'');
        }
        return result;
    }
641887
private static int quoteSingle(final StringBuilder sb, final String string, final int start) {
        int i = start+1;
        if (needsBlockQuoting(string.charAt(start))) return -i;
        int unquoteLen = 0;
        int quotedMap = 1;
        for (int j = 1; i < string.length(); i++, j++) {
            char c = string.charAt(i);
            if (needsQuoting(c)) {
                if (j > MAX_SINGLE_QUOTE_LEN || needsBlockQuoting(c)) return -i-1;
                quotedMap |= 1 << j;
                unquoteLen = 0;
            } else {
                if (unquoteLen == MAX_UNQUOTE_LEN ||
                        (quotedMap == 1 && 
                         unquoteLen == MAX_SINGLE_UNQUOTE_LEN)) {
                    break;
                }
                unquoteLen++;
            }
        }
        appendSingleQuoted(sb, string, start, i - unquoteLen, quotedMap);
        sb.append(string, i - unquoteLen, i);
        return i;
    }
812344
public static String quote(String s, String delim)
    {
        if (s==null)
            return null;
        if (s.length()==0)
            return "\"\"";

        
        for (int i=0;i<s.length();i++)
        {
            char c = s.charAt(i);
            if (c=='"' ||
                c=='\\' ||
                c=='\'' ||
                delim.indexOf(c)>=0)
            {
                StringBuffer b=new StringBuffer(s.length()+8);
                quote(b,s);
                return b.toString();
            }
        }
        
        return s;
    }
1307671
public static String escape(CharSequence s) {
		StringBuilder str = new StringBuilder();
		int len = s.length();
		for (int i = 0; i < len; ++i) {
			char c = s.charAt(i);
			if (c == '&') str.append("&amp;");
			else if (c == '"') str.append("&quot;");
			else if (c == '\'') str.append("&apos;");
			else if (c == '>') str.append("&gt;");
			else if (c == '<') str.append("&lt;");
			else if (c < 32) str.append("&#").append((int)c).append(';');
			else str.append(c);
		}
		return str.toString();
	}
573672
def unquote(value):
    
    if len(value) > 1 and value[0] == '"' and value[-1] == '"':
        value = value[1:-1].replace(r'\"', '"')
    return value
716014
private static String escapeDoubleQuotes(String s, int beginIndex, int endIndex) {

        if (s == null || s.length() == 0 || s.indexOf('"') == -1) {
            return s;
        }

        StringBuilder b = new StringBuilder();
        for (int i = beginIndex; i < endIndex; i++) {
            char c = s.charAt(i);
            if (c == '\\' ) {
                b.append(c);
                //ignore the character after an escape, just append it
                if (++i>=endIndex) throw UndertowMessages.MESSAGES.invalidEscapeCharacter();
                b.append(s.charAt(i));
            } else if (c == '"')
                b.append('\\').append('"');
            else
                b.append(c);
        }

        return b.toString();
    }
1280043
def _unquote_cookie(s: str) -> str:
    
    # If there aren't any doublequotes,
    # then there can't be any special characters.  See RFC 2109.
    if s is None or len(s) < 2:
        return s
    if s[0] != '"' or s[-1] != '"':
        return s

    # We have to assume that we must decode this string.
    # Down to work.

    # Remove the "s
    s = s[1:-1]

    # Check for special sequences.  Examples:
    #    \012 --> \n
    #    \"   --> "
    #
    i = 0
    n = len(s)
    res = []
    while 0 <= i < n:
        o_match = _OctalPatt.search(s, i)
        q_match = _QuotePatt.search(s, i)
        if not o_match and not q_match:  # Neither matched
            res.append(s[i:])
            break
        # else:
        j = k = -1
        if o_match:
            j = o_match.start(0)
        if q_match:
            k = q_match.start(0)
        if q_match and (not o_match or k < j):  # QuotePatt matched
            res.append(s[i:k])
            res.append(s[k + 1])
            i = k + 2
        else:  # OctalPatt matched
            res.append(s[i:j])
            res.append(chr(int(s[j + 1 : j + 4], 8)))
            i = j + 4
    return _nulljoin(res)
55745
public static String fsToWellFormed(String value, boolean lenient) {
        if (value == null || value.isEmpty()) {
            return LogicalValue.ANY.getAbbreviation();
        }
        if (LogicalValue.ANY.getAbbreviation().equals(value) || LogicalValue.NA.getAbbreviation().equals(value)) {
            return value;
        }
        //return value.replaceAll("([._-])", "\\\\$1");

        int startLenient = -1;
        int endLenient = value.length() - 1;
        if (lenient) {
            char prev = ' ';
            for (int x = 0; x < value.length(); x++) {
                char c = value.charAt(x);
                if (startLenient < 0 && c != '?' && c != '*') {
                    startLenient = x;
                }
                switch (c) {
                    case '*':
                    case '?':
                        if (prev != '*' && prev != '?') {
                            endLenient = x - 1;
                        }
                        break;
                    case '\\':
                        //skip the next character as it is quoted
                        x += 1;
                        //don't break because we need to set endLenient
                    default:
                        endLenient = value.length() - 1;
                        break;
                }
                prev = c;
            }
        }

        boolean quoted = false;
        StringBuilder buffer = new StringBuilder(value);
        for (int x = 0; x < buffer.length(); x++) {
            char c = buffer.charAt(x);
            if (c == '.' || c == '_' || c == '-') {
                buffer.insert(x++, '\\');
                endLenient += 1;
            } else if (lenient && x >= startLenient && x <= endLenient) {
                if (!quoted && c == '\\') {
                    quoted = true;
                    continue;
                }
                if (!quoted && !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                    buffer.insert(x++, '\\');
                    endLenient += 1;
                }
                quoted = false;
            }
        }

        return buffer.toString();
    }
786235
def quote(text):
  ''

  # Convert to unicode.
  if not isinstance(text, unicode):
    text = text.decode('utf-8')

  # Look for quote characters. Keep the text as is if it's already quoted.
  for qp in QUOTEPAIRS:
    if text[0] == qp[0] and text[-1] == qp[-1] and len(text) >= 2:
      return text

  # If it's not quoted, try quoting
  for qp in QUOTEPAIRS:
    if qp[1] not in text:
      return qp[0] + text + qp[1]

  #Darn
  raise ValueError(u'The value "%s" is not quoted and contains too many quote characters to quote' % text)
41464
public static String unescape(String text) {
        StringBuilder builder = new StringBuilder();
        boolean foundQuote = false;

        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);

            if (i == 0 && c == QUOTE || i == text.length() - 1 && c == QUOTE) {
                continue;
            }

            if (c == QUOTE) {
                if (foundQuote) {
                    builder.append(QUOTE);
                    foundQuote = false;
                } else {
                    foundQuote = true;
                }
            } else {
                foundQuote = false;
                builder.append(c);
            }
        }

        return builder.toString();
    }
864211
def unquote_to_bytes(string):
    """"""
    # Note: strings are encoded as UTF-8. This is only an issue if it contains
    # unescaped non-ASCII characters, which URIs should not.
    if not string:
        # Is it a string-like object?
        string.split
        return bytes(b'')
    if isinstance(string, str):
        string = string.encode('utf-8')
    ### For Python-Future:
    # It is already a byte-string object, but force it to be newbytes here on
    # Py2:
    string = bytes(string)
    ###
    bits = string.split(b'%')
    if len(bits) == 1:
        return string
    res = [bits[0]]
    append = res.append
    for item in bits[1:]:
        try:
            append(_hextobyte[item[:2]])
            append(item[2:])
        except KeyError:
            append(b'%')
            append(item)
    return bytes(b'').join(res)
830861
private static String convertToQuotedString(String s) {
    if (s == null || s.isEmpty()) {
      return null;
    }
    // If already quoted, return as-is
    if (s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
      return s;
    }
    return '\"' + s + '\"';
  }
308788
def _unquote(string):
    """"""
    result = string.split('%')
    for i, item in enumerate(result[1:]):
        i += 1
        try:
            result[i] = _HEXTOCHR[item[:2]] + item[2:]
        except KeyError:
            result[i] = '%' + item
        except UnicodeDecodeError:
            result[i] = chr(int(item[:2], 16)) + item[2:]
    return ''.join(result)
1144461
static String unescapeCommandAttributeValue(String s, QuoteStyle quoteStyle) {
    // NOTE: we don't just use String.replace since it internally allocates/compiles a regular
    // expression.  Instead we have a handrolled loop.
    int index = s.indexOf(quoteStyle == QuoteStyle.DOUBLE ? "\\\"" : "\\\'");
    if (index == -1) {
      return s;
    }
    StringBuilder buf = new StringBuilder(s.length());
    buf.append(s);
    boolean nextIsQ = buf.charAt(s.length() - 1) == quoteStyle.getQuoteChar();
    for (int i = s.length() - 2; i >= index; i--) {
      char c = buf.charAt(i);
      if (c == '\\' && nextIsQ) {
        buf.deleteCharAt(i);
      }
      nextIsQ = c == quoteStyle.getQuoteChar();
    }
    return buf.toString();
  }
290573
public static String unicodeToString(String s) {
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(s, "\\u");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token.length() > 4) {
                sb.append((char) Integer.parseInt(token.substring(0, 4), 16));
                sb.append(token.substring(4));
            } else {
                sb.append((char) Integer.parseInt(token, 16));
            }
        }
        return sb.toString();
    }
92320
public static String quoteEscaped(final String str) {
        if (N.isNullOrEmpty(str)) {
            return str;
        }

        final StringBuilder sb = Objectory.createStringBuilder();
        final char[] chars = getCharsForReadOnly(str);

        try {
            char ch = 0;
            for (int i = 0, len = str.length(); i < len; i++) {
                ch = chars[i];

                if ((ch == _BACKSLASH) && (i < (len - 1))) {
                    sb.append(ch);
                    sb.append(str.charAt(++i));
                } else if ((ch == _QUOTATION_S) || (ch == _QUOTATION_D)) {
                    sb.append(_BACKSLASH);
                    sb.append(ch);
                } else {
                    sb.append(ch);
                }
            }

            return sb.toString();
        } finally {
            Objectory.recycle(sb);
        }
    }