Special HTML encoding check.
553365
def header_check(self, content):
        """"""

        encode = None

        # Look for meta charset
        m = RE_HTML_ENCODE.search(content)
        if m:
            enc = m.group(1).decode('ascii')

            try:
                codecs.getencoder(enc)
                encode = enc
            except LookupError:
                pass
        else:
            encode = self._has_xml_encode(content)
        return encode
915466
def smart_content_encoding(self):
        """"""
        encoding = self.content_encoding
        if not encoding:
            base_list = self.basename.split('.')
            while (not encoding) and len(base_list) > 1:
                _, encoding = mimetypes.guess_type('.'.join(base_list))
                base_list.pop()

        return encoding
542366
protected function detectCharset(): bool
    {
        // set the default
        $encode = new Encode;
        $encode->from($this->defaultCharset);
        $encode->to($this->defaultCharset);

        if ( ! is_null($this->options->enforceEncoding)) {
            //  they want to enforce the given encoding
            $encode->from($this->options->enforceEncoding);
            $encode->to($this->options->enforceEncoding);

            return false;
        }

        $meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
        if (is_null($meta)) {
            // could not find meta tag
            $this->root->propagateEncoding($encode);

            return false;
        }
        $content = $meta->content;
        if (empty($content)) {
            // could not find content
            $this->root->propagateEncoding($encode);

            return false;
        }
        $matches = [];
        if (preg_match('/charset=(.+)/', $content, $matches)) {
            $encode->from(trim($matches[1]));
            $this->root->propagateEncoding($encode);

            return true;
        }

        // no charset found
        $this->root->propagateEncoding($encode);

        return false;
    }
730010
def get_encodings_from_content(content):
    

    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)

    return charset_re.findall(content)
65368
def utf(text):
    """"""
    try:
        output = unicode(text, encoding='utf-8')
    except UnicodeDecodeError:
        output = text
    except TypeError:
        output = text
    return output
624315
def _ensure_unicode_string(string):
        
        if not isinstance(string, six.text_type):
            string = string.decode('utf-8')
        return string
660725
def detect_charset(self):
        

        charset = None

        body_chunk = self.get_body_chunk()

        if body_chunk:
            # Try to extract charset from http-equiv meta tag
            match_charset = RE_META_CHARSET.search(body_chunk)
            if match_charset:
                charset = match_charset.group(1)
            else:
                match_charset_html5 = RE_META_CHARSET_HTML5.search(body_chunk)
                if match_charset_html5:
                    charset = match_charset_html5.group(1)

            # TODO: <meta charset="utf-8" />
            bom_enc, bom = read_bom(body_chunk)
            if bom_enc:
                charset = bom_enc
                self.bom = bom

            # Try to process XML declaration
            if not charset:
                if body_chunk.startswith(b'<?xml'):
                    match = RE_XML_DECLARATION.search(body_chunk)
                    if match:
                        enc_match = RE_DECLARATION_ENCODING.search(
                            match.group(0))
                        if enc_match:
                            charset = enc_match.group(1)

        if not charset:
            if 'Content-Type' in self.headers:
                pos = self.headers['Content-Type'].find('charset=')
                if pos > -1:
                    charset = self.headers['Content-Type'][(pos + 8):]

        if charset:
            charset = charset.lower()
            if not isinstance(charset, str):
                # Convert to unicode (py2.x) or string (py3.x)
                charset = charset.decode('utf-8')
            # Check that python knows such charset
            try:
                codecs.lookup(charset)
            except LookupError:
                logger.debug('Unknown charset found: %s.'
                             ' Using utf-8 istead.', charset)
                self.charset = 'utf-8'
            else:
                self.charset = charset
1029842
def html(self, unicode=False):
        
        html = lxml.html.tostring(self.element, encoding=self.encoding)
        if unicode:
            html = html.decode(self.encoding)
        return html
546539
def guess_xml_encoding(self, content):
        
        """
        matchobj = self.__regex['xml_encoding'].match(content)
        return matchobj and matchobj.group(1).lower()
1250388
function encodeSpecialCharacters (str) {
  return encodeURI(str).replace(/[!'()* ]/g, function (char) {
    return '%' + char.charCodeAt(0).toString(16)
  })
}
496827
def content_encoding
      if content_type = headers["content-type"]
        mime_type, charset = content_type.split(SEMICOLON, 2)
        charset = charset[CHARSET_OFFSET] if charset
        charset ||= EMPTY

        if charset.empty? and mime_type.start_with?("text/")
          Encoding::UTF_8
        elsif charset.empty?
          Encoding::BINARY
        else
          ENCODINGS[charset] or raise StompParser::InvalidEncodingError, "invalid encoding #{charset.inspect}"
        end
      else
        Encoding::BINARY
      end
    end
215372
def _get_encoding(self):
        """"""
        enco = 'utf-8'

        #  Currently only cp1252 is allowed?
        if WIN:
            import ctypes
            codepage = to_text_string(ctypes.cdll.kernel32.GetACP())
            # import locale
            # locale.getpreferredencoding()  # Differences?
            enco = 'cp' + codepage
        return enco
19849
def ensure_str(text):
    """
    if isinstance(text, unicode):
        try:
            return text.encode(pyreadline_codepage, u"replace")
        except (LookupError, TypeError):
            return text.encode(u"ascii", u"replace")
    return text
284487
def UnicodeFromCodePage(string):
  """"""
  # get the current code page
  codepage = ctypes.windll.kernel32.GetOEMCP()
  try:
    return string.decode("cp%s" % codepage)
  except UnicodeError:
    try:
      return string.decode("utf16", "ignore")
    except UnicodeError:
      # Fall back on utf8 but ignore errors
      return string.decode("utf8", "ignore")
448294
def unicode_safe(text, encoding=UrlEncoding, errors='ignore'):
    """"""
    try:
        text_type = unicode
    except NameError:
        text_type = str
    if isinstance(text, text_type):
        return text
    return text.decode(encoding, errors)
717694
def _ensure_decoded(s):
    
    if isinstance(s, np.bytes_):
        s = s.decode('UTF-8')
    return s
1060042
public static void checkEncoding(String encoding) throws IOException {
	try {
	    URLEncoder.encode("", encoding);
	}
	catch (UnsupportedEncodingException e) {
	    throw new IOException("invalid encoding [" + encoding + "]");
	}
    }