Skip to content

Commit

Permalink
remove unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
Easyoakland committed Jun 22, 2024
1 parent 238c96f commit fe4da21
Showing 1 changed file with 3 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,9 @@ impl<'a> Tokens for StrTokens<'a> {
type Location = StrTokensLocation;

fn next(&mut self) -> Option<Self::Item> {
if self.cursor == self.str.len() {
return None;
}

// Cursor should always start at a valid char boundary.
// So, we just find the next char boundary and return the
// char between those two.
let mut next_char_boundary = self.cursor + 1;
while !self.str.is_char_boundary(next_char_boundary) {
next_char_boundary += 1;
}

// We have to go to &str and then char. Unchecked because we know
// that we are on a valid boundary. There's probably a quicker way..
// To check that bounds detection works even on exotic characters, there's a test included
// at the end of the file.
let next_char = unsafe { self.str.get_unchecked(self.cursor..next_char_boundary) }
.chars()
.next()
.unwrap();

self.cursor = next_char_boundary;
// Cursor should always start at a valid char boundary so this will never panic.
let next_char = self.str[self.cursor..].chars().next()?;
self.cursor += next_char.len_utf8();
Some(next_char)
}

Expand Down

0 comments on commit fe4da21

Please sign in to comment.