From 43453a8ebf183912cff3448223e6202375560ffa Mon Sep 17 00:00:00 2001 From: ltdk Date: Wed, 7 Jun 2023 10:48:28 -0400 Subject: [PATCH 1/3] Don't panic in ceil_char_boundary --- library/core/src/str/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index ef05b25fdd06c..99219914fd2ec 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -271,14 +271,13 @@ impl str { /// Finds the closest `x` not below `index` where `is_char_boundary(x)` is `true`. /// + /// If `x` is greater than the length of the string, this returns the length of the string. + /// /// This method is the natural complement to [`floor_char_boundary`]. See that method /// for more details. /// /// [`floor_char_boundary`]: str::floor_char_boundary /// - /// # Panics - /// - /// Panics if `index > self.len()`. /// /// # Examples /// @@ -296,7 +295,7 @@ impl str { #[inline] pub fn ceil_char_boundary(&self, index: usize) -> usize { if index > self.len() { - slice_error_fail(self, index, index) + self.len() } else { let upper_bound = Ord::min(index + 4, self.len()); self.as_bytes()[index..upper_bound] From d47371de6975524e2dd3848d48c43b724f7aa8f3 Mon Sep 17 00:00:00 2001 From: ltdk Date: Thu, 8 Jun 2023 09:21:05 -0400 Subject: [PATCH 2/3] Fix test --- library/alloc/tests/str.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs index 0ba5d088f6174..e1f3692f5010e 100644 --- a/library/alloc/tests/str.rs +++ b/library/alloc/tests/str.rs @@ -2416,10 +2416,7 @@ fn ceil_char_boundary() { check_many("🇯🇵", 0..=0, 0); check_many("🇯🇵", 1..=4, 4); check_many("🇯🇵", 5..=8, 8); -} -#[test] -#[should_panic] -fn ceil_char_boundary_above_len_panic() { - let _ = "x".ceil_char_boundary(2); + // above len + check_many("hello", 5..=10, 5); } From 2f75dd4e193fc0626dab44b6d76bc2b377fd1f3d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 15 Aug 2023 15:11:55 +0200 Subject: [PATCH 3/3] Fix typo. Co-authored-by: Josh Stone --- library/core/src/str/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 99219914fd2ec..7ae2002d212d6 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -271,7 +271,7 @@ impl str { /// Finds the closest `x` not below `index` where `is_char_boundary(x)` is `true`. /// - /// If `x` is greater than the length of the string, this returns the length of the string. + /// If `index` is greater than the length of the string, this returns the length of the string. /// /// This method is the natural complement to [`floor_char_boundary`]. See that method /// for more details.