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.')