MCPcopy Create free account
hub / github.com/charlierguo/gmail / encode

Function encode

gmail/utf.py:30–58  ·  view source on GitHub ↗

Encode a folder name using IMAP modified UTF-7 encoding. Despite the function's name, the output is still a unicode string.

(s)

Source from the content-addressed store, hash-verified

28PRINTABLE = set(range(0x20, 0x26)) | set(range(0x27, 0x7f))
29
30def encode(s):
31 """Encode a folder name using IMAP modified UTF-7 encoding.
32
33 Despite the function's name, the output is still a unicode string.
34 """
35 if not isinstance(s, text_type):
36 return s
37
38 r = []
39 _in = []
40
41 def extend_result_if_chars_buffered():
42 if _in:
43 r.extend(['&', modified_utf7(''.join(_in)), '-'])
44 del _in[:]
45
46 for c in s:
47 if ord(c) in PRINTABLE:
48 extend_result_if_chars_buffered()
49 r.append(c)
50 elif c == '&':
51 extend_result_if_chars_buffered()
52 r.append('&-')
53 else:
54 _in.append(c)
55
56 extend_result_if_chars_buffered()
57
58 return ''.join(r)
59
60def decode(s):
61 """Decode a folder name from IMAP modified UTF-7 encoding to unicode.

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…