shatter

A fast, zero-copy nostr content parser in Rust
git clone git://jb55.com/shatter
Log | Files | Refs | README

commit d7892fdcdb2ed5003735d7c2ba218267a9ca65af
parent 9d365b681d299257ad9a490f7c98a896d773a691
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  6 Jul 2023 23:17:14 -0700

parse_until: optimize for the fast case

Diffstat:
Msrc/parser.rs | 16+++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/parser.rs b/src/parser.rs @@ -251,8 +251,17 @@ impl<'a> Parser<'a> { pub fn parse_until<F: Fn(char) -> bool>(&mut self, matches: F) -> Result<()> { let len = self.len(); while self.pos < len { + let byte = self.data[self.pos]; let prev = self.pos; - if matches(self.pull_char()?) { + + let chr = if is_utf8(byte) { + self.pull_char()? + } else { + self.pos += 1; + byte as char + }; + + if matches(chr) { self.pos = prev; return Ok(()); } @@ -325,3 +334,8 @@ mod test { Ok(()) } } + +#[inline(always)] +fn is_utf8(byte: u8) -> bool { + (byte & 0x80) == 0x80 +}