From e869a09bc2e0fbd988fdc19014b8b0ad7e4b97c9 Mon Sep 17 00:00:00 2001 From: Martin Dørum Date: Mon, 19 Jun 2023 16:55:16 +0200 Subject: add str.splitlines method The new splitlines method on str is intended to replace usage of fs.read('whatever').strip().split('\n'). The problem with the .strip().split() approach is that it doesn't have a way to represent empty lists (an empty string becomes a list with one empty string, not an empty list), and it doesn't handle Windows-style line endings. --- mesonbuild/interpreter/primitives/string.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'mesonbuild/interpreter/primitives/string.py') diff --git a/mesonbuild/interpreter/primitives/string.py b/mesonbuild/interpreter/primitives/string.py index d9f6a0664..d4daab9f0 100644 --- a/mesonbuild/interpreter/primitives/string.py +++ b/mesonbuild/interpreter/primitives/string.py @@ -38,6 +38,7 @@ class StringHolder(ObjectHolder[str]): 'join': self.join_method, 'replace': self.replace_method, 'split': self.split_method, + 'splitlines': self.splitlines_method, 'strip': self.strip_method, 'substring': self.substring_method, 'to_int': self.to_int_method, @@ -104,6 +105,12 @@ class StringHolder(ObjectHolder[str]): return re.sub(r'@(\d+)@', arg_replace, self.held_object) + @noKwargs + @noPosargs + @FeatureNew('str.splitlines', '1.2.0') + def splitlines_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> T.List[str]: + return self.held_object.splitlines() + @noKwargs @typed_pos_args('str.join', varargs=str) def join_method(self, args: T.Tuple[T.List[str]], kwargs: TYPE_kwargs) -> str: -- cgit v1.2.3