Skip to content

Commit

Permalink
remove unsafe helper
Browse files Browse the repository at this point in the history
  • Loading branch information
asmello committed Jul 1, 2024
1 parent 4190e2d commit 9581fe2
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{borrow::Borrow, cmp::Ordering, ops::Deref, slice, str::FromStr};
use core::{borrow::Borrow, cmp::Ordering, ops::Deref, str::FromStr};

/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Expand Down Expand Up @@ -171,14 +171,11 @@ impl Pointer {
return None;
}
self.0[1..]
.split_once('/')
.find('/')
.map_or_else(
|| (Token::from_encoded_unchecked(&self.0[1..]), Self::root()),
|(front, back)| {
// We want to include the delimiter character too!
// SAFETY: if split was successful, then the delimiter
// character exists before the start of the second `str`.
let back = unsafe { extend_one_before(back) };
|idx| {
let (front, back) = self.0[1..].split_at(idx);
(Token::from_encoded_unchecked(front), Self::new(back))
},
)
Expand Down Expand Up @@ -848,13 +845,6 @@ const fn validate(value: &str) -> Result<&str, ParseError> {
Ok(value)
}

unsafe fn extend_one_before(s: &str) -> &str {
let ptr = s.as_ptr().offset(-1);
let len = s.len() + 1;
let slice = slice::from_raw_parts(ptr, len);
core::str::from_utf8_unchecked(slice)
}

/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
Expand Down

0 comments on commit 9581fe2

Please sign in to comment.