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

Function PyUnicode_EncodeUTF7

Lib/_pycodecs.py:994–1076  ·  view source on GitHub ↗
(s, size, encodeSetO, encodeWhiteSpace, errors)

Source from the content-addressed store, hash-verified

992
993
994def PyUnicode_EncodeUTF7(s, size, encodeSetO, encodeWhiteSpace, errors):
995 inShift = False
996 base64bits = 0
997 base64buffer = 0
998 out = []
999
1000 for i, ch in enumerate(s):
1001 ch_ord = ord(ch)
1002 if inShift:
1003 if _ENCODE_DIRECT(ch, encodeSetO, encodeWhiteSpace):
1004 # shifting out
1005 if base64bits:
1006 out.append(B64(base64buffer << (6 - base64bits)))
1007 base64buffer = 0
1008 base64bits = 0
1009 inShift = False
1010 if B64CHAR(ch) or ch == "-":
1011 out.append(b"-")
1012 out.append(bytes([ch_ord]))
1013 else:
1014 # encode character in base64
1015 if ch_ord >= 0x10000:
1016 # split into surrogate pair
1017 hi = 0xD800 | ((ch_ord - 0x10000) >> 10)
1018 lo = 0xDC00 | ((ch_ord - 0x10000) & 0x3FF)
1019 base64bits += 16
1020 base64buffer = (base64buffer << 16) | hi
1021 while base64bits >= 6:
1022 out.append(B64(base64buffer >> (base64bits - 6)))
1023 base64bits -= 6
1024 base64buffer &= (1 << base64bits) - 1 if base64bits else 0
1025 ch_ord = lo
1026
1027 base64bits += 16
1028 base64buffer = (base64buffer << 16) | ch_ord
1029 while base64bits >= 6:
1030 out.append(B64(base64buffer >> (base64bits - 6)))
1031 base64bits -= 6
1032 base64buffer &= (1 << base64bits) - 1 if base64bits else 0
1033 else:
1034 if ch == "+":
1035 out.append(b"+-")
1036 elif _ENCODE_DIRECT(ch, encodeSetO, encodeWhiteSpace):
1037 out.append(bytes([ch_ord]))
1038 else:
1039 out.append(b"+")
1040 inShift = True
1041 # encode character in base64
1042 if ch_ord >= 0x10000:
1043 hi = 0xD800 | ((ch_ord - 0x10000) >> 10)
1044 lo = 0xDC00 | ((ch_ord - 0x10000) & 0x3FF)
1045 base64bits += 16
1046 base64buffer = (base64buffer << 16) | hi
1047 while base64bits >= 6:
1048 out.append(B64(base64buffer >> (base64bits - 6)))
1049 base64bits -= 6
1050 base64buffer &= (1 << base64bits) - 1 if base64bits else 0
1051 ch_ord = lo

Callers 1

utf_7_encodeFunction · 0.85

Calls 6

enumerateFunction · 0.85
ordFunction · 0.85
_ENCODE_DIRECTFunction · 0.85
B64Function · 0.85
B64CHARFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected