Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 483:38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 17 Dec 2021 14:14:36 +0100 |
| parents | 8b8ffb74d452 |
| children | 0259bd521729 |
comparison
equal
deleted
inserted
replaced
| 482:8b8ffb74d452 | 483:38e4be1882e3 |
|---|---|
| 235 _QUOTE = u(b'%') | 235 _QUOTE = u(b'%') |
| 236 _QUOTE_x = u(b'x') | 236 _QUOTE_x = u(b'x') |
| 237 _QUOTE_u = u(b'u') | 237 _QUOTE_u = u(b'u') |
| 238 _QUOTE_U = u(b'U') | 238 _QUOTE_U = u(b'U') |
| 239 _COMMENT = u(b'#') | 239 _COMMENT = u(b'#') |
| 240 _QUOTE_MAP = { | |
| 241 0x25: u(b'%x25'), # _QUOTE | |
| 242 0x2e: u(b'%x2e'), # _DOT | |
| 243 0x3a: u(b'%x3a'), # _NS_SEPARATOR | |
| 244 0x23: u(b'%x23'), # _COMMENT / anchor | |
| 245 0x7c: u(b'%x7c'), # _FILTER_SEPARATOR | |
| 246 0x22: u(b'%x22'), | |
| 247 0x27: u(b'%x27'), | |
| 248 0x7b: u(b'%x7b'), | |
| 249 0x7d: u(b'%x7d'), | |
| 250 0x5b: u(b'%x5b'), | |
| 251 0x5d: u(b'%x5d'), | |
| 252 } | |
| 240 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz' | 253 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz' |
| 241 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | 254 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 242 b'0123456789' | 255 b'0123456789' |
| 243 b'-_@!§$&/()=?*+~;,<>´`^') | 256 b'-_@!$&/\\()=?*+~;,<>^') |
| 244 """Mostly used configuration key characters that do not need any quoting | 257 """Mostly used configuration key characters that do not need any quoting |
| 245 | 258 |
| 246 """ | 259 """ |
| 247 | 260 |
| 248 is_jail = False | 261 is_jail = False |
| 705 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. | 718 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. |
| 706 | 719 |
| 707 See also the :ref:`quoting` section. | 720 See also the :ref:`quoting` section. |
| 708 | 721 |
| 709 """ | 722 """ |
| 710 # Quick check whether all of the chars is in _QUOTE_SAFE | 723 # Quick check whether all of the chars are in _QUOTE_SAFE |
| 711 if not s.rstrip(klass._QUOTE_SAFE): | 724 if not s.rstrip(klass._QUOTE_SAFE): |
| 712 return s | 725 return s |
| 726 | |
| 713 # Slow path | 727 # Slow path |
| 714 qc = klass._QUOTE | 728 re_encode = False |
| 715 s = s.replace(qc, qc + "x25") | 729 if PY2: |
| 716 s = s.replace(klass._DOT, qc + "x2e") | 730 # Use the Unicode translation variant in PY2 |
| 717 s = s.replace(klass._NS_SEPARATOR, qc + "x3a") | 731 if isinstance(s, str): |
| 718 s = s.replace(klass._COMMENT, qc + "x23") | 732 s = s.decode("latin1") |
| 719 s = s.replace(klass._FILTER_SEPARATOR, qc + "x7c") | 733 re_encode = True |
| 720 s = s.replace('"', qc + "x22") | 734 s = s.translate(klass._QUOTE_MAP) |
| 721 s = s.replace("'", qc + "x27") | 735 if re_encode: |
| 722 s = s.replace('{', qc + "x7b") | 736 return s.encode("latin1") |
| 723 s = s.replace('}', qc + "x7d") | 737 else: |
| 724 s = s.replace('[', qc + "x5b") | 738 return s |
| 725 return s.replace(']', qc + "x5d") | |
| 726 | 739 |
| 727 @classmethod | 740 @classmethod |
| 728 def unquote(klass, s): | 741 def unquote(klass, s): |
| 729 """Unquote the content of `s`: handle all patterns ``%xNN``, | 742 """Unquote the content of `s`: handle all patterns ``%xNN``, |
| 730 ``%uNNNN`` or ``%UNNNNNNNN``. | 743 ``%uNNNN`` or ``%UNNNNNNNN``. |
