r""" >>> import io >>> s = 'abcd\uabcd' >>> enc = s.encode('utf-8') >>> enc b'abcd\xea\xaf\x8d' >>> n = bytes([len(enc)]) # little-endian 1-byte length >>> t = read_unicodestring1(io.BytesIO(n + enc + b'junk')) >>> s == t True >>> read_unicodestring1(io.Byte
(f)
| 627 | |
| 628 | |
| 629 | def read_unicodestring1(f): |
| 630 | r""" |
| 631 | >>> import io |
| 632 | >>> s = 'abcd\uabcd' |
| 633 | >>> enc = s.encode('utf-8') |
| 634 | >>> enc |
| 635 | b'abcd\xea\xaf\x8d' |
| 636 | >>> n = bytes([len(enc)]) # little-endian 1-byte length |
| 637 | >>> t = read_unicodestring1(io.BytesIO(n + enc + b'junk')) |
| 638 | >>> s == t |
| 639 | True |
| 640 | |
| 641 | >>> read_unicodestring1(io.BytesIO(n + enc[:-1])) |
| 642 | Traceback (most recent call last): |
| 643 | ... |
| 644 | ValueError: expected 7 bytes in a unicodestring1, but only 6 remain |
| 645 | """ |
| 646 | |
| 647 | n = read_uint1(f) |
| 648 | assert n >= 0 |
| 649 | data = f.read(n) |
| 650 | if len(data) == n: |
| 651 | return str(data, 'utf-8', 'surrogatepass') |
| 652 | raise ValueError("expected %d bytes in a unicodestring1, but only %d " |
| 653 | "remain" % (n, len(data))) |
| 654 | |
| 655 | unicodestring1 = ArgumentDescriptor( |
| 656 | name="unicodestring1", |
nothing calls this directly
no test coverage detected
searching dependent graphs…