[js2c] Fix ordering issue of TextMacro expansion
If a key is a substring of an earlier value, then the earlier value will unintentionally be clobbered. For example with: macro SET_PRIVATE(obj, sym, val) = (obj[sym] = val); SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object); if the mapping is: {'val': 'object', 'obj': 'iterator', 'sym': 'arrayIteratorObjectSymbol'} then 'obj' -> 'iterator' will clobber 'val' -> 'object', resulting in 'val' -> 'iteratorect'. To fix this, replace all substitutions simultaneously. Patch from Zoe Clifford <zoeclifford@google.com> Review-Url: https://codereview.chromium.org/2249873004 Cr-Commit-Position: refs/heads/master@{#38665}
This commit is contained in:
parent
888c67e995
commit
95db63ab6b
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user