diff options
author | Joergen Ibsen <ji@ibse.dk> | 2017-11-19 15:08:58 +0100 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2017-12-03 22:51:58 +0200 |
commit | 312bc2ca810e333e7d8dd42e55ae9bd348757a07 (patch) | |
tree | 6485efe90ff2f60fbbca786c72104da77e57d23f | |
parent | cf76ffad145eb83a0bbfce89e05b7610637ff293 (diff) | |
download | meson-312bc2ca810e333e7d8dd42e55ae9bd348757a07.zip meson-312bc2ca810e333e7d8dd42e55ae9bd348757a07.tar.gz meson-312bc2ca810e333e7d8dd42e55ae9bd348757a07.tar.bz2 |
Fix string format recursive replace
Also error on placeholder index out of range.
-rw-r--r-- | mesonbuild/interpreter.py | 13 | ||||
-rw-r--r-- | test cases/common/42 string operations/meson.build | 2 |
2 files changed, 12 insertions, 3 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 3e89305..f112d7b 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2999,12 +2999,19 @@ different subdirectory. def format_string(self, templ, args): if isinstance(args, mparser.ArgumentNode): args = args.arguments - for (i, arg) in enumerate(args): + arg_strings = [] + for arg in args: arg = self.evaluate_statement(arg) if isinstance(arg, bool): # Python boolean is upper case. arg = str(arg).lower() - templ = templ.replace('@{}@'.format(i), str(arg)) - return templ + arg_strings.append(str(arg)) + + def arg_replace(match): + idx = int(match.group(1)) + if idx >= len(arg_strings): + raise InterpreterException('Format placeholder @{}@ out of range.'.format(idx)) + return arg_strings[idx] + return re.sub(r'@(\d+)@', arg_replace, templ) # Only permit object extraction from the same subproject def validate_extraction(self, buildtarget): diff --git a/test cases/common/42 string operations/meson.build b/test cases/common/42 string operations/meson.build index e60006a..a43de70 100644 --- a/test cases/common/42 string operations/meson.build +++ b/test cases/common/42 string operations/meson.build @@ -13,6 +13,8 @@ subs2 = '42' assert(templ2.format(subs2) == '42', 'String formatting with variables is broken.') +assert('@@0@@ @@1@@'.format(1, 2) == '@1@ @2@', 'String format is recursive.') + long = 'abcde' prefix = 'abc' suffix = 'cde' |