Implement `in` operator on string
This commit is contained in:
parent
8abbcf7887
commit
a6db624aad
|
@ -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
|
||||
```
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue