diff options
author | Joergen Ibsen <ji@ibse.dk> | 2017-11-18 20:48:36 +0100 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2017-11-22 00:43:32 +0200 |
commit | fdb48a3a0f0425a6bd4a71a2e004479080955df8 (patch) | |
tree | 24b9972e9e32a7b947acd57b555f556ac911e31f | |
parent | d5a6ab31bfbcec9838805c598be75abb036aff87 (diff) | |
download | meson-fdb48a3a0f0425a6bd4a71a2e004479080955df8.zip meson-fdb48a3a0f0425a6bd4a71a2e004479080955df8.tar.gz meson-fdb48a3a0f0425a6bd4a71a2e004479080955df8.tar.bz2 |
Use re.sub instead of replace loop
This also prohibits recursive substitution.
-rw-r--r-- | mesonbuild/mesonlib.py | 11 | ||||
-rw-r--r-- | test cases/common/16 configure file/config5.h.in | 1 | ||||
-rw-r--r-- | test cases/common/16 configure file/meson.build | 11 | ||||
-rw-r--r-- | test cases/common/16 configure file/prog5.c | 6 |
4 files changed, 23 insertions, 6 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index f74c6c1..c8eea4d 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -394,9 +394,9 @@ def get_library_dirs(): def do_replacement(regex, line, confdata): - match = re.search(regex, line) missing_variables = set() - while match: + + def variable_replace(match): varname = match.group(1) if varname in confdata: (var, desc) = confdata.get(varname) @@ -409,9 +409,8 @@ def do_replacement(regex, line, confdata): else: missing_variables.add(varname) var = '' - line = line.replace('@' + varname + '@', var) - match = re.search(regex, line) - return line, missing_variables + return var + return re.sub(regex, variable_replace, line), missing_variables def do_mesondefine(line, confdata): arr = line.split() @@ -443,7 +442,7 @@ def do_conf_file(src, dst, confdata): raise MesonException('Could not read input file %s: %s' % (src, str(e))) # Only allow (a-z, A-Z, 0-9, _, -) as valid characters for a define # Also allow escaping '@' with '\@' - regex = re.compile(r'[^\\]?@([-a-zA-Z0-9_]+)@') + regex = re.compile(r'(?<!\\)@([-a-zA-Z0-9_]+)@') result = [] missing_variables = set() for line in data: diff --git a/test cases/common/16 configure file/config5.h.in b/test cases/common/16 configure file/config5.h.in new file mode 100644 index 0000000..323bec6 --- /dev/null +++ b/test cases/common/16 configure file/config5.h.in @@ -0,0 +1 @@ +#define MESSAGE "@var@" diff --git a/test cases/common/16 configure file/meson.build b/test cases/common/16 configure file/meson.build index 1e5a819..4264d07 100644 --- a/test cases/common/16 configure file/meson.build +++ b/test cases/common/16 configure file/meson.build @@ -109,3 +109,14 @@ configs = [ foreach c : configs test('@0@'.format(c), file_contains_py, args: [ c, test_string ]) endforeach + +# Test variable is substituted only once +conf5 = configuration_data() +conf5.set('var', '@var2@') +conf5.set('var2', 'error') +configure_file( + input : 'config5.h.in', + output : '@BASENAME@', + configuration : conf5 +) +test('test5', executable('prog5', 'prog5.c')) diff --git a/test cases/common/16 configure file/prog5.c b/test cases/common/16 configure file/prog5.c new file mode 100644 index 0000000..42c08f9 --- /dev/null +++ b/test cases/common/16 configure file/prog5.c @@ -0,0 +1,6 @@ +#include <string.h> +#include <config5.h> + +int main(int argc, char **argv) { + return strcmp(MESSAGE, "@var2@"); +} |