Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Unified Diff: tools/js2c.py

Issue 2249873004: [js2c] Fix ordering issue of TextMacro expansion (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/js2c.py
diff --git a/tools/js2c.py b/tools/js2c.py
index d91513311429a87b3fe5731d82feac6ddecfb493..b676d662e773e281830f6471b04284ec62cefe1c 100755
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -145,10 +145,12 @@ class TextMacro:
self.args = args
self.body = body
def expand(self, mapping):
- result = self.body
- for key, value in mapping.items():
- result = result.replace(key, value)
- return result
+ # Keys could be substrings of earlier values. To avoid unintended
+ # clobbering, apply all replacements simultaneously.
+ any_key_pattern = "|".join(re.escape(k) for k in mapping.iterkeys())
+ def replace(match):
+ return mapping[match.group(0)]
+ return re.sub(any_key_pattern, replace, self.body)
class PythonMacro:
def __init__(self, args, fun):
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698