From 82b09f09a2e92399edf33fa390ca99752cc98bb8 Mon Sep 17 00:00:00 2001 From: Nabil Wadih Date: Sun, 5 May 2024 08:46:43 -0700 Subject: [PATCH] Replace uses of `unsafe` with safe alternatives (#1714) * Replace unsafe blocks with safe alternatives * Replace unsafe blocks with safe alternatives * update to fix tests * use split_at(0) for consistensy * Update src/lib.rs --------- Co-authored-by: Nabil Wadih Co-authored-by: Geoffroy Couprie --- src/traits.rs | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index 953a747f..d35212ed 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -419,8 +419,11 @@ impl<'a> Input for &'a str { P: Fn(Self::Item) -> bool, { match self.find(predicate) { - // find() returns a byte index that is already in the slice at a char boundary - Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, + // The position i is returned from str::find() which means it is within the bounds of the string + Some(i) => { + let (str1, str2) = self.split_at(i); + Ok((str2, str1)) + } None => Err(Err::Incomplete(Needed::new(1))), } } @@ -436,8 +439,11 @@ impl<'a> Input for &'a str { { match self.find(predicate) { Some(0) => Err(Err::Error(E::from_error_kind(self, e))), - // find() returns a byte index that is already in the slice at a char boundary - Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, + // The position i is returned from str::find() which means it is within the bounds of the string + Some(i) => { + let (str1, str2) = self.split_at(i); + Ok((str2, str1)) + } None => Err(Err::Incomplete(Needed::new(1))), } } @@ -451,15 +457,12 @@ impl<'a> Input for &'a str { P: Fn(Self::Item) -> bool, { match self.find(predicate) { - // find() returns a byte index that is already in the slice at a char boundary - Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, - // the end of slice is a char boundary - None => unsafe { - Ok(( - self.get_unchecked(self.len()..), - self.get_unchecked(..self.len()), - )) - }, + // The position i is returned from str::find() which means it is within the bounds of the string + Some(i) => { + let (str1, str2) = self.split_at(i); + Ok((str2, str1)) + } + None => Ok(self.split_at(0)), } } @@ -474,19 +477,18 @@ impl<'a> Input for &'a str { { match self.find(predicate) { Some(0) => Err(Err::Error(E::from_error_kind(self, e))), - // find() returns a byte index that is already in the slice at a char boundary - Some(i) => unsafe { Ok((self.get_unchecked(i..), self.get_unchecked(..i))) }, + // The position i is returned from str::find() which means it is within the bounds of the string + Some(i) => { + let (str1, str2) = self.split_at(i); + Ok((str2, str1)) + } None => { if self.is_empty() { Err(Err::Error(E::from_error_kind(self, e))) } else { // the end of slice is a char boundary - unsafe { - Ok(( - self.get_unchecked(self.len()..), - self.get_unchecked(..self.len()), - )) - } + let (str1, str2) = self.split_at(self.len()); + Ok((str2, str1)) } } }