diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2012-12-17 18:38:03 +0100 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2012-12-17 18:38:03 +0100 |
| commit | 19be9c57a69bf9a2e62056e6c73ee2a569c86a9d (patch) | |
| tree | e29068799d9228f3e11e86912ffb0e443e1ab537 /kconfiglib.py | |
| parent | 637d66b241c82bf53c7058b1a160fff0f2ca63d5 (diff) | |
Add a fast path for string literal lexing.
Speeds up _tokenize() by ~20%.
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 619de23..97c0450 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -697,24 +697,36 @@ class Config(): elif c == '"' or c == "'": quote = c - value = "" i += 1 - while 1: - if i >= strlen: + + # Fast path: If the rest of the string contains no backslashes + # (almost always), we can simply look for the matching quote. + if s.find("\\", i) == -1: + end = s.find(quote, i) + if end == -1: _tokenization_error(s, strlen, filename, linenr) - c = s[i] - if c == quote: - break - if c == "\\": - if i + 1 >= strlen: + append(s[i:end]) + i = end + 1 + else: + # Slow path. This could probably be sped up, but it's a + # very unusual case anyway. + value = "" + while 1: + if i >= strlen: _tokenization_error(s, strlen, filename, linenr) - value += s[i + 1] - i += 2 - else: - value += c - i += 1 - i += 1 - append(value) + c = s[i] + if c == quote: + break + if c == "\\": + if i + 1 >= strlen: + _tokenization_error(s, strlen, filename, linenr) + value += s[i + 1] + i += 2 + else: + value += c + i += 1 + i += 1 + append(value) elif c == "&": if i + 1 >= strlen: |
