(string: bytes | bytearray | str)
| 793 | return bytes(_unquote_impl(string)) |
| 794 | |
| 795 | def _unquote_impl(string: bytes | bytearray | str) -> bytes | bytearray: |
| 796 | # Note: strings are encoded as UTF-8. This is only an issue if it contains |
| 797 | # unescaped non-ASCII characters, which URIs should not. |
| 798 | if not string: |
| 799 | # Is it a string-like object? |
| 800 | string.split |
| 801 | return b'' |
| 802 | if isinstance(string, str): |
| 803 | string = string.encode('utf-8') |
| 804 | bits = string.split(b'%') |
| 805 | if len(bits) == 1: |
| 806 | return string |
| 807 | res = bytearray(bits[0]) |
| 808 | append = res.extend |
| 809 | # Delay the initialization of the table to not waste memory |
| 810 | # if the function is never called |
| 811 | global _hextobyte |
| 812 | if _hextobyte is None: |
| 813 | _hextobyte = {(a + b).encode(): bytes.fromhex(a + b) |
| 814 | for a in _hexdig for b in _hexdig} |
| 815 | for item in bits[1:]: |
| 816 | try: |
| 817 | append(_hextobyte[item[:2]]) |
| 818 | append(item[2:]) |
| 819 | except KeyError: |
| 820 | append(b'%') |
| 821 | append(item) |
| 822 | return res |
| 823 | |
| 824 | _asciire = re.compile('([\x00-\x7f]+)') |
| 825 |
no test coverage detected
searching dependent graphs…