From 3ea0081e273b821ce6a5654998660345e0bd7d5d Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Mon, 3 Feb 2025 09:52:00 -0300 Subject: [PATCH] delete char_slices() Signed-off-by: martinvuyk --- docs/changelog.md | 5 +-- stdlib/src/collections/string/string.mojo | 35 +++---------------- .../src/collections/string/string_slice.mojo | 18 +++------- .../test/collections/string/test_string.mojo | 4 +-- 4 files changed, 15 insertions(+), 47 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index c5d90ec167..f7daa6a4cc 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -228,11 +228,12 @@ what we publish. - `b16encode()` - `b16decode()` -- Added new `String.chars()` and `String.char_slices()` iterator methods. +- Added new `String.chars()` iterator method. Different use-cases may prefer iterating over the `Char`s encoded in a string, or iterating over subslices containing single characters. The existing - `__iter__()` method defaults to `String.char_slices()`. + `__iter__()` method defaults to returning `StringSlice`s of the unicode + characters. - The `String.__len__()` and `StringSlice.__len__()` methods now return the length of the string in bytes. diff --git a/stdlib/src/collections/string/string.mojo b/stdlib/src/collections/string/string.mojo index c831ec44a1..33d5a2f3bd 100644 --- a/stdlib/src/collections/string/string.mojo +++ b/stdlib/src/collections/string/string.mojo @@ -1069,7 +1069,9 @@ struct String( Returns: An iterator of references to the string unicode characters. """ - return self.char_slices() + return _StringSliceIter[__origin_of(self)]( + ptr=self.unsafe_ptr(), length=self.byte_length() + ) fn __reversed__(self) -> _StringSliceIter[__origin_of(self), False]: """Iterate backwards over the string unicode characters. @@ -1274,33 +1276,6 @@ struct String( """ return self.as_string_slice().chars() - fn char_slices(self) -> _StringSliceIter[__origin_of(self)]: - """Returns an iterator over single-character slices of this string. - - Each returned slice points to a single Unicode codepoint encoded in the - underlying UTF-8 representation of this string. - - Returns: - An iterator of references to the string elements. - - # Examples - - Iterate over the character slices in a string: - - ```mojo - from testing import assert_equal, assert_true - - var s = String("abc") - var iter = s.char_slices() - assert_true(iter.__next__() == "a") - assert_true(iter.__next__() == "b") - assert_true(iter.__next__() == "c") - assert_equal(iter.__has_next__(), False) - ``` - . - """ - return self.as_string_slice().char_slices() - fn unsafe_ptr( ref self, ) -> UnsafePointer[ @@ -1543,7 +1518,7 @@ struct String( while lhs <= str_byte_len: # Python adds all "whitespace chars" as one separator # if no separator was specified - for s in self[lhs:].char_slices(): + for s in self[lhs:]: if not s.isspace(): break lhs += s.byte_length() @@ -1559,7 +1534,7 @@ struct String( rhs = lhs + num_bytes(self.unsafe_ptr()[lhs]) for s in self[ lhs + num_bytes(self.unsafe_ptr()[lhs]) : - ].char_slices(): + ]: if s.isspace(): break rhs += s.byte_length() diff --git a/stdlib/src/collections/string/string_slice.mojo b/stdlib/src/collections/string/string_slice.mojo index 2f3718055f..bc7131d948 100644 --- a/stdlib/src/collections/string/string_slice.mojo +++ b/stdlib/src/collections/string/string_slice.mojo @@ -446,7 +446,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( """ var result = String() var use_dquote = False - for s in self.char_slices(): + for s in self: use_dquote = use_dquote or (s == "'") if s == "\\": @@ -681,7 +681,9 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( Returns: An iterator of references to the string unicode characters. """ - return self.char_slices() + return _StringSliceIter[origin]( + ptr=self.unsafe_ptr(), length=self.byte_length() + ) fn __reversed__(self) -> _StringSliceIter[origin, False]: """Iterate backwards over the string unicode characters. @@ -953,16 +955,6 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( """ return CharsIter(self) - fn char_slices(self) -> _StringSliceIter[origin]: - """Iterate over the string, returning immutable references. - - Returns: - An iterator of references to the string elements. - """ - return _StringSliceIter[origin]( - ptr=self.unsafe_ptr(), length=self.byte_length() - ) - @always_inline fn as_bytes(self) -> Span[Byte, origin]: """Get the sequence of encoded bytes of the underlying string. @@ -1313,7 +1305,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( ) else: var offset = 0 - for s in self.char_slices(): + for s in self: var b_len = s.byte_length() if not _is_newline_char(ptr, offset, ptr[offset], b_len): return False diff --git a/stdlib/test/collections/string/test_string.mojo b/stdlib/test/collections/string/test_string.mojo index 9c7a0f141a..5705b66ebe 100644 --- a/stdlib/test/collections/string/test_string.mojo +++ b/stdlib/test/collections/string/test_string.mojo @@ -1141,7 +1141,7 @@ def test_string_chars_iter(): def test_string_char_slices_iter(): var s0 = String("abc") - var s0_iter = s0.char_slices() + var s0_iter = s0.__iter__() assert_true(s0_iter.__next__() == "a") assert_true(s0_iter.__next__() == "b") assert_true(s0_iter.__next__() == "c") @@ -1174,7 +1174,7 @@ def test_string_char_slices_iter(): var idx = -1 vs = String("mojo🔥") - var iterator = vs.char_slices() + var iterator = vs.__iter__() assert_equal(5, len(iterator)) var item = iterator.__next__() assert_equal(String("m"), String(item))