diff --git a/stdlib/src/collections/string/string_slice.mojo b/stdlib/src/collections/string/string_slice.mojo index e0c6170f20..ec62dd9661 100644 --- a/stdlib/src/collections/string/string_slice.mojo +++ b/stdlib/src/collections/string/string_slice.mojo @@ -56,7 +56,7 @@ fn _utf8_first_byte_sequence_length(b: Byte) -> Int: this does not work correctly if given a continuation byte.""" debug_assert( - (b & 0b1100_0000) != 0b1000_0000, + not _is_continuation_byte(b), "Function does not work correctly if given a continuation byte.", ) return Int(count_leading_zeros(~b)) + Int(b < 0b1000_0000) @@ -1154,7 +1154,7 @@ struct StringSlice[mut: Bool, //, origin: Origin[mut]]( # overall length in bytes. # For a visual explanation of how this UTF-8 codepoint counting works: # https://connorgray.com/ephemera/project-log#2025-01-13 - var continuation_count = _count_utf8_continuation_bytes(self) + var continuation_count = _count_utf8_continuation_bytes(self.as_bytes()) return self.byte_length() - continuation_count fn get_immutable( diff --git a/stdlib/test/collections/string/test_string_slice.mojo b/stdlib/test/collections/string/test_string_slice.mojo index 1ac338e12d..eb463eb087 100644 --- a/stdlib/test/collections/string/test_string_slice.mojo +++ b/stdlib/test/collections/string/test_string_slice.mojo @@ -552,8 +552,7 @@ def test_count_utf8_continuation_bytes(): def _test(amnt: Int, items: List[UInt8]): var p = items.unsafe_ptr() var span = Span[Byte, StaticConstantOrigin](ptr=p, length=len(items)) - var str_slice = StringSlice(unsafe_from_utf8=span) - assert_equal(amnt, _count_utf8_continuation_bytes(str_slice)) + assert_equal(amnt, _count_utf8_continuation_bytes(span)) _test(5, List[UInt8](c, c, c, c, c)) _test(2, List[UInt8](b2, c, b2, c, b1))