From df979ad422709bc61d9bb05d49e0803b6f745efd Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Thu, 15 Oct 2015 18:40:00 +0300 Subject: String startswith and endswith methods. --- interpreter.py | 9 ++++++++- test cases/common/42 string formatting/meson.build | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/interpreter.py b/interpreter.py index 82fdb741b..b9f91c4b8 100644 --- a/interpreter.py +++ b/interpreter.py @@ -1832,12 +1832,12 @@ class Interpreter(): def string_method_call(self, obj, method_name, args): obj = self.to_native(obj) + (posargs, _) = self.reduce_arguments(args) if method_name == 'strip': return obj.strip() elif method_name == 'format': return self.format_string(obj, args) elif method_name == 'split': - (posargs, _) = self.reduce_arguments(args) if len(posargs) > 1: raise InterpreterException('Split() must have at most one argument.') elif len(posargs) == 1: @@ -1847,6 +1847,13 @@ class Interpreter(): return obj.split(s) else: return obj.split() + elif method_name == 'startswith' 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) + return obj.endswith(s) raise InterpreterException('Unknown method "%s" for a string.' % method_name) def to_native(self, arg): diff --git a/test cases/common/42 string formatting/meson.build b/test cases/common/42 string formatting/meson.build index 4e7cc1aa2..81f52683b 100644 --- a/test cases/common/42 string formatting/meson.build +++ b/test cases/common/42 string formatting/meson.build @@ -20,3 +20,23 @@ subs2 = '42' if templ2.format(subs2) != '42' error('String formatting with variables is broken.') endif + +long = 'abcde' +prefix = 'abc' +suffix = 'cde' + +if not long.startswith(prefix) + error('Prefix.') +endif + +if long.startswith(suffix) + error('Not prefix.') +endif + +if not long.endswith(suffix) + error('Suffix.') +endif + +if long.endswith(prefix) + error('Not suffix.') +endif -- cgit v1.2.3