Verify and decode an encoded string. Return an object or None.
337421
def cookie_decode(data, key, digestmod=None):
    
    depr(0, 13, "cookie_decode() will be removed soon.",
                "Do not use this API directly.")
    data = tob(data)
    if cookie_is_encoded(data):
        sig, msg = data.split(tob('?'), 1)
        digestmod = digestmod or hashlib.sha256
        hashed = hmac.new(tob(key), msg, digestmod=digestmod).digest()
        if _lscmp(sig[1:], base64.b64encode(hashed)):
            return pickle.loads(base64.b64decode(msg))
    return None
369812
def decode(var, encoding):
    
    if PY2:
        if isinstance(var, unicode):
            ret = var
        elif isinstance(var, str):
            if encoding:
                ret = var.decode(encoding)
            else:
                ret = unicode(var)
        else:
            ret = unicode(var)
    else:
        ret = str(var)
    return ret
124530
def _decode_string(buf, pos):
    
    for i in range(pos, len(buf)):
        if buf[i:i+1] == _compat_bytes('\x00'):
            try:
                return (buf[pos:i].decode(_CHARSET), i+1)
            # Uncomment the following two lines for detailled information
            #except UnicodeDecodeError as ude:
            #   raise MinusconfError(str(ude))
            except UnicodeDecodeError:
                raise MinusconfError('Not a valid ' + _CHARSET + ' string: ' + repr(buf[pos:i]))

    raise MinusconfError("Premature end of string (Forgot trailing \\0?), buf=" + repr(buf))
650264
def encoded(string, encoding='utf-8'):
    
    assert isinstance(string, string_types) or isinstance(string, binary_type)
    if isinstance(string, text_type):
        return string.encode(encoding)
    try:
        # make sure the string can be decoded in the specified encoding ...
        string.decode(encoding)
        return string
    except UnicodeDecodeError:
        # ... if not use latin1 as best guess to decode the string before encoding as
        # specified.
        return string.decode('latin1').encode(encoding)
221376
def as_text(str_or_bytes, encoding='utf-8', errors='strict'):
    
    if isinstance(str_or_bytes, text):
        return str_or_bytes
    return str_or_bytes.decode(encoding, errors)
169839
def decode_string(v, encoding="utf-8"):
    """"""
    if isinstance(encoding, basestring):
        encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore"))
    if isinstance(v, binary_type):
        for e in encoding:
            try:
                return v.decode(*e)
            except:
                pass
        return v
    return unicode(v)
556621
def encode_non_ascii_string(string):
    
    encoded_string = string.encode('utf-8', 'replace')
    if six.PY3:
        encoded_string = encoded_string.decode()

    return encoded_string
1037830
def safe_str(obj):
    
    try:
        return str(obj)
    except UnicodeEncodeError:
        # obj is unicode
        try:
            return unicode(obj).encode('unicode_escape')  # noqa for undefined-variable
        except NameError:
            # This is Python 3, just return the obj as it's already unicode
            return obj
264409
def get_decoder(encoding, flexible=False):
    
    if encoding == None:
        def no_decode(v):
            return v
        return no_decode
    elif flexible:
        def do_decode1(v):
            return v.decode(encoding, 'ignore')
        return do_decode1
    else:
        def do_decode2(v):
            return v.decode(encoding)
        return do_decode2
1125948
def safe_encode(s):
    """"""
    if isinstance(s, unicode):
        return s.encode(defenc)
    elif isinstance(s, bytes):
        return s
    elif s is not None:
        raise TypeError('Expected bytes or text, but got %r' % (s,))
125851
def decode(self, s, _w=WHITESPACE.match, _PY3=PY3):
        
        if _PY3 and isinstance(s, binary_type):
            s = s.decode(self.encoding)
        obj, end = self.raw_decode(s)
        end = _w(s, end).end()
        if end != len(s):
            raise JSONDecodeError("Extra data", s, end, len(s))
        return obj
1207803
private static Object decodeObject(String text) {
        Object result = null;
        if (text.startsWith("[]"))
            result = HexString.hexToBin(text, 2);
        else
            result = URLDecode(text);
        return result;
    }
139913
def decode_ay(ay):
    """"""
    if ay is None:
        return ''
    elif isinstance(ay, str):
        return ay
    elif isinstance(ay, bytes):
        return ay.decode('utf-8')
    else:
        # dbus.Array([dbus.Byte]) or any similar sequence type:
        return bytearray(ay).rstrip(bytearray((0,))).decode('utf-8')
1030428
def polite_string(a_string):
    """"""
    if is_py3() and hasattr(a_string, 'decode'):
        try:
            return a_string.decode('utf-8')
        except UnicodeDecodeError:
            return a_string

    return a_string
346841
def decode(self, s, _w=WHITESPACE.match):
        
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
        end = _w(s, end).end()
        if end != len(s):
            raise JSONDecodeError("Extra data", s, end, len(s))
        return obj
1144629
def _decode_var(cls, string):
        

        str_match = cls.quoted_string_regex.match(string)
        if str_match:
            return string.strip("'" if str_match.groups()[0] else '"')
        # NOTE: "¹".isdigit() results in True because they are idiots
        elif string.isdigit() and cls.is_digit_regex.match(string) is not None:
            return int(string)
        elif string.lower() in ("true", "false"):
            return string.lower() == "true"
        elif string.lstrip("-").isdigit():
            try:
                return int(string)
            except ValueError:
                # case where we mistake something like "--0" as a int
                return string
        elif "." in string.lstrip("-"):
            try:
                return float(string)
            except ValueError:
                # one off case where we mistake a single "." as a float
                return string
        else:
            return string
53569
def safe_decode(s):
    """"""
    if isinstance(s, unicode):
        return s
    elif isinstance(s, bytes):
        return s.decode(defenc, 'surrogateescape')
    elif s is not None:
        raise TypeError('Expected bytes or text, but got %r' % (s,))