Add more string functions: contains(), to_upper() and to_lower()

pull/443/head
Tim-Philipp Müller 9 years ago
parent 79537b54db
commit 02e84df010
  1. 1
      authors.txt
  2. 8
      mesonbuild/interpreter.py
  3. 24
      test cases/common/42 string formatting/meson.build

@ -32,3 +32,4 @@ Nirbheek Chauhan
Nicolas Schneider
Luke Adams
Rogiel Sulzbach
Tim-Philipp Müller

@ -2033,6 +2033,10 @@ 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 == 'split':
if len(posargs) > 1:
raise InterpreterException('Split() must have at most one argument.')
@ -2043,12 +2047,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:

@ -41,4 +41,28 @@ 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
assert('3'.to_int() == 3, 'String int conversion does not work.')

Loading…
Cancel
Save