Use percent-encoding to quote a string.
(string: str, safe: str)
| 480 | |
| 481 | |
| 482 | def percent_encoded(string: str, safe: str) -> str: |
| 483 | """ |
| 484 | Use percent-encoding to quote a string. |
| 485 | """ |
| 486 | NON_ESCAPED_CHARS = UNRESERVED_CHARACTERS + safe |
| 487 | |
| 488 | # Fast path for strings that don't need escaping. |
| 489 | if not string.rstrip(NON_ESCAPED_CHARS): |
| 490 | return string |
| 491 | |
| 492 | return "".join( |
| 493 | [char if char in NON_ESCAPED_CHARS else PERCENT(char) for char in string] |
| 494 | ) |
| 495 | |
| 496 | |
| 497 | def quote(string: str, safe: str) -> str: |