diff --git a/docs/changelog.md b/docs/changelog.md index d7ef52f96f..0f64391810 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -217,29 +217,11 @@ what we publish. - `b16encode()` - `b16decode()` -- Added new `String.chars()` and `String.char_slices()` iterator methods, and - deprecated the existing `String.__iter__()` method. +- Added new `String.chars()` and `String.char_slices()` iterator methods. Different use-cases may prefer iterating over the `Char`s encoded in a string, - or iterating over subslices containing single characters. Neither iteration - semantics is an obvious default, so the existing `__iter__()` method has been - deprecated in favor of writing explicit iteration methods for the time being. - - Code of the form: - - ```mojo - var s: String = ... - for c in s: - # ... - ``` - - can be migrated to using the `.char_slices()` method: - - ```mojo - var s: String = ... - for c in s.char_slices(): - # ... - ``` + or iterating over subslices containing single characters. The existing + `__iter__()` method defaults to `String.char_slices()`. - The `String.__len__()` and `StringSlice.__len__()` methods now return the length of the string in bytes. diff --git a/stdlib/test/collections/string/test_string.mojo b/stdlib/test/collections/string/test_string.mojo index 0d39db2fa5..9c7a0f141a 100644 --- a/stdlib/test/collections/string/test_string.mojo +++ b/stdlib/test/collections/string/test_string.mojo @@ -1152,7 +1152,7 @@ def test_string_char_slices_iter(): # Borrow immutably fn conc(vs: String) -> String: var c = String("") - for v in vs.char_slices(): + for v in vs: c += v return c @@ -1163,11 +1163,11 @@ def test_string_char_slices_iter(): concat += v assert_equal(321, atol(concat)) - for v in vs.char_slices(): + for v in vs: v.unsafe_ptr().origin_cast[mut=True]()[] = ord("1") # Borrow immutably - for v in vs.char_slices(): + for v in vs: concat += v assert_equal(321111, atol(concat)) @@ -1224,7 +1224,7 @@ def test_string_char_slices_iter(): var ptr = item.unsafe_ptr() var amnt_characters = 0 var byte_idx = 0 - for v in item.char_slices(): + for v in item: var byte_len = v.byte_length() for i in range(byte_len): assert_equal(ptr[byte_idx + i], v.unsafe_ptr()[i])