diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2017-08-14 05:08:15 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek@centricular.com> | 2017-09-12 14:07:29 +0530 |
commit | 276e9c15cc63d266c9f81301229128d02b229a6f (patch) | |
tree | c443124a2601e1a531b4270594367c228f89cc7b | |
parent | e1fc17ef2a532d539678b9a9378bca59eda38a91 (diff) | |
download | meson-276e9c15cc63d266c9f81301229128d02b229a6f.tar.gz |
str.split() can now take a positional argument
All the specified characters in the specified argument will be
stripped. If unspecified, the old behaviour is used.
Includes tests.
-rw-r--r-- | mesonbuild/interpreterbase.py | 27 | ||||
-rw-r--r-- | test cases/common/42 string operations/meson.build | 7 |
2 files changed, 26 insertions, 8 deletions
diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index d2e2ab3bb..1dd2f0212 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -450,9 +450,25 @@ class InterpreterBase: else: raise InterpreterException('Unknown method "%s" for an integer.' % method_name) + @staticmethod + def _get_one_string_posarg(posargs, method_name): + if len(posargs) > 1: + m = '{}() must have zero or one arguments' + raise InterpreterException(m.format(method_name)) + elif len(posargs) == 1: + s = posargs[0] + if not isinstance(s, str): + m = '{}() argument must be a string' + raise InterpreterException(m.format(method_name)) + return s + return None + def string_method_call(self, obj, method_name, args): (posargs, _) = self.reduce_arguments(args) if method_name == 'strip': + s = self._get_one_string_posarg(posargs, 'strip') + if s is not None: + return obj.strip(s) return obj.strip() elif method_name == 'format': return self.format_string(obj, args) @@ -463,15 +479,10 @@ class InterpreterBase: 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.') - elif len(posargs) == 1: - s = posargs[0] - if not isinstance(s, str): - raise InterpreterException('Split() argument must be a string') + s = self._get_one_string_posarg(posargs, 'split') + if s is not None: return obj.split(s) - else: - return obj.split() + return obj.split() elif method_name == 'startswith' or method_name == 'contains' or method_name == 'endswith': s = posargs[0] if not isinstance(s, str): diff --git a/test cases/common/42 string operations/meson.build b/test cases/common/42 string operations/meson.build index 5d7a73d00..d9f813004 100644 --- a/test cases/common/42 string operations/meson.build +++ b/test cases/common/42 string operations/meson.build @@ -67,3 +67,10 @@ assert(not version_number.version_compare('!=1.2.8'), 'Version_compare neq broke assert(version_number.version_compare('<2.0'), 'Version_compare major less broken') assert(version_number.version_compare('>0.9'), 'Version_compare major greater broken') + +assert(' spaces tabs '.strip() == 'spaces tabs', 'Spaces and tabs badly stripped') +assert(''' +multiline string '''.strip() == '''multiline string''', 'Newlines badly stripped') +assert('"1.1.20"'.strip('"') == '1.1.20', '" badly stripped') +assert('"1.1.20"'.strip('".') == '1.1.20', '". badly stripped') +assert('"1.1.20" '.strip('" ') == '1.1.20', '". badly stripped') |