MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / detect_encoding

Function detect_encoding

tools/python-3.11.9-amd64/Lib/json/__init__.py:244–271  ·  view source on GitHub ↗
(b)

Source from the content-addressed store, hash-verified

242
243
244def detect_encoding(b):
245 bstartswith = b.startswith
246 if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
247 return 'utf-32'
248 if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
249 return 'utf-16'
250 if bstartswith(codecs.BOM_UTF8):
251 return 'utf-8-sig'
252
253 if len(b) >= 4:
254 if not b[0]:
255 # 00 00 -- -- - utf-32-be
256 # 00 XX -- -- - utf-16-be
257 return 'utf-16-be' if b[1] else 'utf-32-be'
258 if not b[1]:
259 # XX 00 00 00 - utf-32-le
260 # XX 00 00 XX - utf-16-le
261 # XX 00 XX -- - utf-16-le
262 return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
263 elif len(b) == 2:
264 if not b[0]:
265 # 00 XX - utf-16-be
266 return 'utf-16-be'
267 if not b[1]:
268 # XX 00 - utf-16-le
269 return 'utf-16-le'
270 # default
271 return 'utf-8'
272
273
274def 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