MCPcopy Index your code
hub / github.com/python/cpython / detect_encoding

Function detect_encoding

Lib/json/__init__.py:247–274  ·  view source on GitHub ↗
(b)

Source from the content-addressed store, hash-verified

245
246
247def detect_encoding(b):
248 bstartswith = b.startswith
249 if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
250 return 'utf-32'
251 if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
252 return 'utf-16'
253 if bstartswith(codecs.BOM_UTF8):
254 return 'utf-8-sig'
255
256 if len(b) >= 4:
257 if not b[0]:
258 # 00 00 -- -- - utf-32-be
259 # 00 XX -- -- - utf-16-be
260 return 'utf-16-be' if b[1] else 'utf-32-be'
261 if not b[1]:
262 # XX 00 00 00 - utf-32-le
263 # XX 00 00 XX - utf-16-le
264 # XX 00 XX -- - utf-16-le
265 return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
266 elif len(b) == 2:
267 if not b[0]:
268 # 00 XX - utf-16-be
269 return 'utf-16-be'
270 if not b[1]:
271 # XX 00 - utf-16-le
272 return 'utf-16-le'
273 # default
274 return 'utf-8'
275
276
277def load(fp, *, cls=None, object_hook=None, parse_float=None,

Callers 1

loadsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…