From 3c8468cd4d90f5072934e849078298e70dd1ddfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Sat, 12 Mar 2016 14:08:35 +0000 Subject: [PATCH] Add string underscorify() function So we can easily construct the defines for include headers and struct checks and such. --- mesonbuild/interpreter.py | 2 ++ test cases/common/42 string formatting/meson.build | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 37432a86a..14f20e7b0 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2037,6 +2037,8 @@ class Interpreter(): return obj.upper() elif method_name == 'to_lower': return obj.lower() + elif method_name == 'underscorify': + return re.sub(r'[^a-zA-Z0-9]', '_', obj) elif method_name == 'split': if len(posargs) > 1: raise InterpreterException('Split() must have at most one argument.') diff --git a/test cases/common/42 string formatting/meson.build b/test cases/common/42 string formatting/meson.build index ea785e80e..56cbcb2ae 100644 --- a/test cases/common/42 string formatting/meson.build +++ b/test cases/common/42 string formatting/meson.build @@ -65,4 +65,17 @@ if long.to_upper().to_lower() != long error('Broken to_lower') endif +if 'struct stat.st_foo'.underscorify() != 'struct_stat_st_foo' + error('Broken underscorify') +endif + +if '#include '.underscorify() != '_include__foo_bar_h_' + error('Broken underscorify') +endif + +# case should not change, space should be replaced, numbers are ok too +if 'Do SomeThing 09'.underscorify() != 'Do_SomeThing_09' + error('Broken underscorify') +endif + assert('3'.to_int() == 3, 'String int conversion does not work.')