MCPcopy Create free account
hub / github.com/clips/pattern / _toUnicode

Method _toUnicode

pattern/web/soup/BeautifulSoup.py:1842–1865  ·  view source on GitHub ↗

Given a string and its encoding, decodes the string into Unicode. %encoding is a string recognized by encodings.aliases

(self, data, encoding)

Source from the content-addressed store, hash-verified

1840 return self.markup
1841
1842 def _toUnicode(self, data, encoding):
1843 '''Given a string and its encoding, decodes the string into Unicode.
1844 %encoding is a string recognized by encodings.aliases'''
1845
1846 # strip Byte Order Mark (if present)
1847 if (len(data) >= 4) and (data[:2] == '\xfe\xff') \
1848 and (data[2:4] != '\x00\x00'):
1849 encoding = 'utf-16be'
1850 data = data[2:]
1851 elif (len(data) >= 4) and (data[:2] == '\xff\xfe') \
1852 and (data[2:4] != '\x00\x00'):
1853 encoding = 'utf-16le'
1854 data = data[2:]
1855 elif data[:3] == '\xef\xbb\xbf':
1856 encoding = 'utf-8'
1857 data = data[3:]
1858 elif data[:4] == '\x00\x00\xfe\xff':
1859 encoding = 'utf-32be'
1860 data = data[4:]
1861 elif data[:4] == '\xff\xfe\x00\x00':
1862 encoding = 'utf-32le'
1863 data = data[4:]
1864 newdata = unicode(data, encoding)
1865 return newdata
1866
1867 def _detectEncoding(self, xml_data, isHTML=False):
1868 """Given a document, tries to detect its XML encoding."""

Callers 1

_convertFromMethod · 0.95

Calls 1

lenFunction · 0.85

Tested by

no test coverage detected