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

Function PyUnicode_EncodeUTF16

Lib/_pycodecs.py:1312–1356  ·  view source on GitHub ↗
(s, size, errors, byteorder="little")

Source from the content-addressed store, hash-verified

1310
1311
1312def PyUnicode_EncodeUTF16(s, size, errors, byteorder="little"):
1313 # /* Offsets from p for storing byte pairs in the right order. */
1314
1315 p = []
1316 bom = sys.byteorder
1317 if byteorder == "native":
1318 bom = sys.byteorder
1319 p += STORECHAR(0xFEFF, bom)
1320
1321 if byteorder == "little":
1322 bom = "little"
1323 elif byteorder == "big":
1324 bom = "big"
1325
1326 pos = 0
1327 while pos < len(s):
1328 ch = ord(s[pos])
1329 if 0xD800 <= ch <= 0xDFFF:
1330 if errors == "surrogatepass":
1331 p += STORECHAR(ch, bom)
1332 pos += 1
1333 else:
1334 res, pos = unicode_call_errorhandler(
1335 errors, "utf-16", "surrogates not allowed", s, pos, pos + 1, False
1336 )
1337 for c in res:
1338 cp = ord(c)
1339 cp2 = 0
1340 if cp >= 0x10000:
1341 cp2 = 0xDC00 | ((cp - 0x10000) & 0x3FF)
1342 cp = 0xD800 | ((cp - 0x10000) >> 10)
1343 p += STORECHAR(cp, bom)
1344 if cp2:
1345 p += STORECHAR(cp2, bom)
1346 else:
1347 ch2 = 0
1348 if ch >= 0x10000:
1349 ch2 = 0xDC00 | ((ch - 0x10000) & 0x3FF)
1350 ch = 0xD800 | ((ch - 0x10000) >> 10)
1351 p += STORECHAR(ch, bom)
1352 if ch2:
1353 p += STORECHAR(ch2, bom)
1354 pos += 1
1355
1356 return p
1357
1358
1359def PyUnicode_DecodeMBCS(s, size, errors):

Callers 3

utf_16_encodeFunction · 0.85
utf_16_le_encodeFunction · 0.85
utf_16_be_encodeFunction · 0.85

Calls 4

STORECHARFunction · 0.85
lenFunction · 0.85
ordFunction · 0.85

Tested by

no test coverage detected