Decode a folder name from IMAP modified UTF-7 encoding to unicode.

    Despite the function's name, the input may still be a unicode
    string. If the input is bytes, it's first decoded to unicode.
163313
def decode(s):
    
    if isinstance(s, binary_type):
        s = s.decode('latin-1')
    if not isinstance(s, text_type):
        return s

    r = []
    _in = []
    for c in s:
        if c == '&' and not _in:
            _in.append('&')
        elif c == '-' and _in:
            if len(_in) == 1:
                r.append('&')
            else:
                r.append(modified_deutf7(''.join(_in[1:])))
            _in = []
        elif _in:
            _in.append(c)
        else:
            r.append(c)
    if _in:
        r.append(modified_deutf7(''.join(_in[1:])))

    return ''.join(r)
685497
def convert_to_utf8(string):
    '''  '''
    if (isinstance(string, unicode)):
        return string.encode('utf-8')
    try:
        u = unicode(string, 'utf-8')
    except TypeError:
        return str(string)
    utf8 = u.encode('utf-8')
    return utf8
1125317
def unicodify(filename):
    
    if isinstance(filename, bytes):
        return filename.decode(locale.getpreferredencoding())
    else:
        return filename
954839
def utf8_encode_str(string, encoding='UTF-8'):
    
    if not string:
        return ''
    src_enc = chardet.detect(string)['encoding']
    try:
        return string.decode(src_enc).encode(encoding)
    except:
        return string.decode('ascii', errors='replace').encode(encoding)
250168
def modutf7_decode(data: bytes) -> str:
    
    parts = []
    is_usascii = True
    buf = memoryview(data)
    while buf:
        byte = buf[0]
        if is_usascii:
            if buf[0:2] == b'&-':
                parts.append('&')
                buf = buf[2:]
            elif byte == 0x26:
                is_usascii = False
                buf = buf[1:]
            else:
                parts.append(chr(byte))
                buf = buf[1:]
        else:
            for i, byte in enumerate(buf):
                if byte == 0x2d:
                    to_decode = buf[:i].tobytes()
                    decoded = _modified_b64decode(to_decode)
                    parts.append(decoded)
                    buf = buf[i + 1:]
                    is_usascii = True
                    break
    if not is_usascii:
        to_decode = buf.tobytes()
        decoded = _modified_b64decode(to_decode)
        parts.append(decoded)
    return ''.join(parts)
1147689
def read_zipfile(self, encoding='utf8'):
        
        from zipfile import ZipFile
        with ZipFile(self.abspath) as zipped:
            for num, zip_name in enumerate(zipped.namelist()):
                return zipped.open(zip_name).read().decode(encoding)
131517
function getFromFileNameV1 (filename) {
	var result = mime.lookup(filename);

	// Add charset just in case for some buggy browsers.
	if (result === 'application/javascript' || result === 'application/json' || result === 'text/plain') {
		result += '; charset=UTF-8';
	}

	return result;
}
777451
def decode(self, charset='utf-8', errors='replace'):
        
        return URL(
            self.scheme.decode('ascii'),
            self.decode_netloc(),
            self.path.decode(charset, errors),
            self.query.decode(charset, errors),
            self.fragment.decode(charset, errors)
        )
232350
def _decode(self, infile, encoding):
        
        if isinstance(infile, string_types):
            # can't be unicode
            # NOTE: Could raise a ``UnicodeDecodeError``
            return infile.decode(encoding).splitlines(True)
        for i, line in enumerate(infile):
            # NOTE: The isinstance test here handles mixed lists of unicode/string
            # NOTE: But the decode will break on any non-string values
            # NOTE: Or could raise a ``UnicodeDecodeError``
            if PY3K:
                if not isinstance(line, str):
                    infile[i] = line.decode(encoding)
            else:
                if not isinstance(line, unicode):
                    infile[i] = line.decode(encoding)
        return infile
47461
def get_encoding_name(self, name):
        """"""

        name = codecs.lookup(
            filters.PYTHON_ENCODING_NAMES.get(name, name).lower()
        ).name

        if name.startswith(('utf-32', 'utf-16')):
            name = name[:6]
            if CURRENT_ENDIAN == BIG_ENDIAN:
                name += '-be'
            else:
                name += '-le'

        if name == 'utf-8-sig':
            name = 'utf-8'
        return name
567046
def _raw_name_string(bufr, strings_offset, str_offset, length):
        
        offset = strings_offset + str_offset
        tmpl = '%ds' % length
        return unpack_from(tmpl, bufr, offset)[0]
362930
function (/*String*/path, /*String?*/encoding) {
            if (encoding === 'utf-8') {
                encoding = 'utf8';
            }
            if (!encoding) {
                encoding = 'utf8';
            }

            var text = fs.readFileSync(path, encoding);

            //Hmm, would not expect to get A BOM, but it seems to happen,
            //remove it just in case.
            if (text.indexOf('\uFEFF') === 0) {
                text = text.substring(1, text.length);
            }

            return text;
        }
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)
831212
def fs_decode(path):
    

    path = _get_path(path)
    if path is None:
        raise TypeError("expected a valid path to decode")
    if isinstance(path, six.binary_type):
        if six.PY2:
            from array import array

            indexes = _invalid_utf8_indexes(array(str("B"), path))
            return "".join(
                chunk.decode(_fs_encoding, _fs_decode_errors)
                for chunk in _chunks(path, indexes)
            )
        return path.decode(_fs_encoding, _fs_decode_errors)
    return path
892886
def decode_input(text_in):
    
    
    if type(text_in) == list:
        text_out = u' '.join([t.decode('utf-8') for t in text_in])
    else:
        text_out = text_in.decode('utf-8')
    return text_out
380091
def detect_encoding(filename):
    """"""
    try:
        with open(filename, 'rb') as input_file:
            from lib2to3.pgen2 import tokenize as lib2to3_tokenize
            encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]

            # Check for correctness of encoding.
            with open_with_encoding(filename, encoding) as input_file:
                input_file.read()

        return encoding
    except (SyntaxError, LookupError, UnicodeDecodeError):
        return 'latin-1'
1181303
function decode (str) {
  return str.trim().split(' ').map((s) => {
    if (s.charCodeAt(0) - 0x2800 > 127) return s
    return String.fromCharCode(s.charCodeAt(0) - 0x2800)
  }).join('')
}