aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2016-03-12 16:42:40 +0200
committerJussi Pakkanen <jpakkane@gmail.com>2016-03-12 16:42:40 +0200
commit2afc1dd497e78478faace68f14471ed9b3942d1f (patch)
tree3474088b8690d4dc8b8eb95a85a0f8babe207c38
parent49418cfe8ab1f42fa28ca9f408f60050bf56c5dc (diff)
parent3c8468cd4d90f5072934e849078298e70dd1ddfc (diff)
downloadmeson-2afc1dd497e78478faace68f14471ed9b3942d1f.zip
meson-2afc1dd497e78478faace68f14471ed9b3942d1f.tar.gz
meson-2afc1dd497e78478faace68f14471ed9b3942d1f.tar.bz2
Merge pull request #443 from tp-m/more-string-funcs
Add more string funcs: to_upper(), to_lower(), contains() and underscorify()
-rw-r--r--authors.txt1
-rw-r--r--mesonbuild/interpreter.py10
-rw-r--r--test cases/common/42 string formatting/meson.build37
3 files changed, 47 insertions, 1 deletions
diff --git a/authors.txt b/authors.txt
index 5f79881..d8b0abc 100644
--- a/authors.txt
+++ b/authors.txt
@@ -32,3 +32,4 @@ Nirbheek Chauhan
Nicolas Schneider
Luke Adams
Rogiel Sulzbach
+Tim-Philipp Müller
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 62b7fc0..14f20e7 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -2033,6 +2033,12 @@ class Interpreter():
return obj.strip()
elif method_name == 'format':
return self.format_string(obj, args)
+ elif method_name == 'to_upper':
+ 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.')
@@ -2043,12 +2049,14 @@ class Interpreter():
return obj.split(s)
else:
return obj.split()
- elif method_name == 'startswith' or method_name == 'endswith':
+ elif method_name == 'startswith' or method_name == 'contains' or method_name == 'endswith':
s = posargs[0]
if not isinstance(s, str):
raise InterpreterException('Argument must be a string.')
if method_name == 'startswith':
return obj.startswith(s)
+ elif method_name == 'contains':
+ return obj.find(s) >= 0
return obj.endswith(s)
elif method_name == 'to_int':
try:
diff --git a/test cases/common/42 string formatting/meson.build b/test cases/common/42 string formatting/meson.build
index 39737f7..56cbcb2 100644
--- a/test cases/common/42 string formatting/meson.build
+++ b/test cases/common/42 string formatting/meson.build
@@ -41,4 +41,41 @@ if long.endswith(prefix)
error('Not suffix.')
endif
+if not long.contains(prefix)
+ error('Does not contain prefix')
+endif
+
+if not long.contains(suffix)
+ error('Does not contain suffix')
+endif
+
+if not long.contains('bcd')
+ error('Does not contain middle part')
+endif
+
+if long.contains('dc')
+ error('Broken contains')
+endif
+
+if long.to_upper() != 'ABCDE'
+ error('Broken to_upper')
+endif
+
+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 <foo/bar.h>'.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.')