MCPcopy Index your code
hub / github.com/RustPython/RustPython / detect_encoding

Function detect_encoding

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

Source from the content-addressed store, hash-verified

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

Callers 1

loadsFunction · 0.70

Calls 1

lenFunction · 0.85

Tested by

no test coverage detected