aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2022-10-28 13:38:46 -0400
committerJussi Pakkanen <jpakkane@gmail.com>2022-11-06 17:22:00 +0200
commita6db624aada29e0e11e7e0297d6310413dd1f8f9 (patch)
treec7f6d67a5160ff89b9c5a4ee07c818900104a102
parent8abbcf78870c350cd9ff43f65d89e7e998d50448 (diff)
downloadmeson-a6db624aada29e0e11e7e0297d6310413dd1f8f9.zip
meson-a6db624aada29e0e11e7e0297d6310413dd1f8f9.tar.gz
meson-a6db624aada29e0e11e7e0297d6310413dd1f8f9.tar.bz2
Implement `in` operator on string
-rw-r--r--docs/markdown/snippets/str_in.md11
-rw-r--r--mesonbuild/interpreter/primitives/string.py12
-rw-r--r--test cases/common/16 comparison/meson.build3
3 files changed, 26 insertions, 0 deletions
diff --git a/docs/markdown/snippets/str_in.md b/docs/markdown/snippets/str_in.md
new file mode 100644
index 0000000..abcb8bd
--- /dev/null
+++ b/docs/markdown/snippets/str_in.md
@@ -0,0 +1,11 @@
+## `in` operator for strings
+
+`in` and `not in` operators now works on strings, in addition to arrays and
+dictionaries.
+
+```
+fs = import('fs')
+if 'something' in fs.read('somefile')
+ # True
+endif
+```
diff --git a/mesonbuild/interpreter/primitives/string.py b/mesonbuild/interpreter/primitives/string.py
index 0c635a2..d8133c1 100644
--- a/mesonbuild/interpreter/primitives/string.py
+++ b/mesonbuild/interpreter/primitives/string.py
@@ -64,6 +64,8 @@ class StringHolder(ObjectHolder[str]):
self.operators.update({
MesonOperator.DIV: self.op_div,
MesonOperator.INDEX: self.op_index,
+ MesonOperator.IN: self.op_in,
+ MesonOperator.NOT_IN: self.op_notin,
})
def display_name(self) -> str:
@@ -173,6 +175,16 @@ class StringHolder(ObjectHolder[str]):
except IndexError:
raise InvalidArguments(f'Index {other} out of bounds of string of size {len(self.held_object)}.')
+ @FeatureNew('"in" string operator', '0.64.0')
+ @typed_operator(MesonOperator.IN, str)
+ def op_in(self, other: str) -> bool:
+ return other in self.held_object
+
+ @FeatureNew('"not in" string operator', '0.64.0')
+ @typed_operator(MesonOperator.NOT_IN, str)
+ def op_notin(self, other: str) -> bool:
+ return other not in self.held_object
+
class MesonVersionString(str):
pass
diff --git a/test cases/common/16 comparison/meson.build b/test cases/common/16 comparison/meson.build
index 1d250f9..97cf2ca 100644
--- a/test cases/common/16 comparison/meson.build
+++ b/test cases/common/16 comparison/meson.build
@@ -141,3 +141,6 @@ assert(exe3 not in [exe1, exe2], ''''exe3 shouldn't be in [exe1, exe2]''')
assert('a' in {'a': 'b'}, '''1 should be in {'a': 'b'}''')
assert('b' not in {'a': 'b'}, '''1 should be in {'a': 'b'}''')
+
+assert('a' in 'abc')
+assert('b' not in 'def')