diff --git a/tests/test_string.py b/tests/test_string.py index b13a8f8..4228ddb 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -28,6 +28,12 @@ [True, False, True, False], {"elem": "num"}, ), + ( + "str_len", + ["num", "1num", "", "*", " ", "sp ace", "ta b"], + [3, 4, 0, 1, 1, 6, 4], + {} + ) ], ) def test_methods_with_args( diff --git a/ulist/python/ulist/core.py b/ulist/python/ulist/core.py index dc2f14a..a3d53e8 100644 --- a/ulist/python/ulist/core.py +++ b/ulist/python/ulist/core.py @@ -446,6 +446,11 @@ def starts_with(self, elem: str) -> UltraFastList: assert isinstance(self._values, StringList) return UltraFastList(self._values.starts_with(elem)) + def str_len(self) -> "UltraFastList": + """Return each element's string length of self.""" + assert isinstance(self._values, StringList) + return UltraFastList(self._values.str_len()) + def sub(self, other: "UltraFastList") -> "UltraFastList": """Return self - other.""" assert not isinstance(self._values, (BooleanList, StringList)) diff --git a/ulist/python/ulist/ulist.pyi b/ulist/python/ulist/ulist.pyi index 3ca9637..6620702 100644 --- a/ulist/python/ulist/ulist.pyi +++ b/ulist/python/ulist/ulist.pyi @@ -264,6 +264,7 @@ class StringList: def size(self) -> int: ... def sort(self, ascending: bool) -> StringList: ... def starts_with(self, elem: str) -> BooleanList: ... + def str_len(self) -> IntegerList64: ... def to_list(self) -> List[str]: ... def union_all(self, other: LIST_RS) -> LIST_RS: ... def unique(self) -> StringList: ... diff --git a/ulist/src/string.rs b/ulist/src/string.rs index 0e456aa..c05021f 100644 --- a/ulist/src/string.rs +++ b/ulist/src/string.rs @@ -129,6 +129,11 @@ impl StringList { BooleanList::new(vec) } + pub fn str_len(&self) -> IntegerList64 { + let vec = self.values().iter().map(|x| x.len() as i64).collect(); + IntegerList64::new(vec) + } + pub fn to_list(&self) -> Vec { List::to_list(self) }