Skip to content

Commit

Permalink
Replace uses of unsafe with safe alternatives (#1714)
Browse files Browse the repository at this point in the history
* 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 <nwadih@google.com>
Co-authored-by: Geoffroy Couprie <contact@geoffroycouprie.com>
  • Loading branch information
3 people committed May 5, 2024
1 parent cdd8baf commit 82b09f0
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
}
}
Expand All @@ -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))),
}
}
Expand All @@ -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)),
}
}

Expand All @@ -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))
}
}
}
Expand Down

0 comments on commit 82b09f0

Please sign in to comment.