MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / extract_unicode_strings

Function extract_unicode_strings

src/textcode/strings2.py:50–69  ·  view source on GitHub ↗

Yield Unicode strings (UTF-16-LE encoded ASCII) from a buf string of binary data. :param buf: A bytestring. :type buf: str :param n: The minimum length of strings to extract. :type n: int :rtype: Sequence[string]

(buf, n=3)

Source from the content-addressed store, hash-verified

48
49
50def extract_unicode_strings(buf, n=3):
51 """
52 Yield Unicode strings (UTF-16-LE encoded ASCII) from a buf string of binary data.
53
54 :param buf: A bytestring.
55 :type buf: str
56 :param n: The minimum length of strings to extract.
57 :type n: int
58 :rtype: Sequence[string]
59 """
60 if not buf:
61 return
62
63 reg = b'((?:[%s]\x00){%d,})' % (ASCII_BYTE, n)
64 r = re.compile(reg)
65 for match in r.finditer(buf):
66 try:
67 yield match.group().decode('utf-16')
68 except UnicodeDecodeError:
69 pass
70
71
72def extract_strings(buf, n=3):

Callers 1

extract_stringsFunction · 0.85

Calls 2

groupMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected