diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2018-01-22 19:20:13 +0100 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2018-01-22 20:04:41 +0100 |
| commit | 532b561288f6a9628073e68aaa51b87d9c20e838 (patch) | |
| tree | 05c3efd9d57cb0ddc9d0384e1590dcf639eaa4e3 /kconfiglib.py | |
| parent | e01cb49d35e5062afb7fe159c8777f74e02aeceb (diff) | |
Simplify escape()
The fancy regex isn't really justified. Much faster with replace() too,
though this is an unlikely hotspot.
Could have used a \g<0> backreference to refer to the entire match
instead of using a capturing group too. Hadn't discovered that.
Add some selftests for escape() and unescape() too.
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index f1a254d..550c347 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -3671,17 +3671,17 @@ def expr_str(expr): _REL_TO_STR[expr[0]], expr_str(expr[2])) -# escape()/unescape() helpers -_escape_re_sub = re.compile(r'(["\\])').sub -_unescape_re_sub = re.compile(r"\\(.)").sub - def escape(s): r""" Escapes the string 's' in the same fashion as is done for display in Kconfig format and when writing strings to a .config file. " and \ are replaced by \" and \\, respectively. """ - return _escape_re_sub(r"\\\1", s) + # \ must be escaped before " to avoid double escaping + return s.replace("\\", r"\\").replace('"', r'\"') + +# unescape() helper +_unescape_re_sub = re.compile(r"\\(.)").sub def unescape(s): r""" |
