Replace %xx escapes by their single-character equivalent. By default, percent-encoded sequences are replaced by ASCII character or byte code, and invalid sequences are replaced by a placeholder character. unquote('abc%20def') -> 'abc def' unquote('abc%FFdef') -> 'abc\xffdef' unq
(string)
| 276 | |
| 277 | |
| 278 | def unquote_binary(string): |
| 279 | """Replace %xx escapes by their single-character equivalent. |
| 280 | By default, percent-encoded sequences are replaced by ASCII character or |
| 281 | byte code, and invalid sequences are replaced by a placeholder character. |
| 282 | |
| 283 | unquote('abc%20def') -> 'abc def' |
| 284 | unquote('abc%FFdef') -> 'abc\xffdef' |
| 285 | unquote('%no') -> '%no' |
| 286 | """ |
| 287 | bits = string.split(b"%") |
| 288 | if len(bits) == 1: |
| 289 | return bits[0] |
| 290 | |
| 291 | res = [bits[0]] |
| 292 | for item in bits[1:]: |
| 293 | res.append(_hextobyte.get(item[:2], b"%")) |
| 294 | res.append(item if res[-1] == b"%" else item[2:]) |
| 295 | return b"".join(res) |