From f777339af3eac0c0226417d3b63d50cbfd42eef2 Mon Sep 17 00:00:00 2001 From: Andrew Tribick Date: Fri, 21 Jul 2023 23:49:31 +0200 Subject: [PATCH] Clarify logic on bytes:code units ratio --- library/core/src/str/iter.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 133167a706793..cd16810c4dde7 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -1440,8 +1440,10 @@ impl<'a> Iterator for EncodeUtf16<'a> { #[inline] fn size_hint(&self) -> (usize, Option) { let len = self.chars.iter.len(); - // The highest bytes:code units ratio occurs for 3-byte sequences, so - // use this to determine the lower bound for the hint. The lowest + // The highest bytes:code units ratio occurs for 3-byte sequences, + // since a 4-byte sequence results in 2 code units. The lower bound + // is therefore determined by assuming the remaining bytes contain as + // many 3-byte sequences as possible. The highest bytes:code units // ratio is for 1-byte sequences, so use this for the upper bound. // `(len + 2)` can't overflow, because we know that the `slice::Iter` // belongs to a slice in memory which has a maximum length of