shatter

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

commit 7ab1d6432e51c50d90174cc1e5a3d97bff97cfc0
parent 821cf87c434012a2f590c0dc533456298ac9f470
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  6 Jul 2023 22:09:53 -0700

parser: add parse_digits

This will be used by the index mention parser

Diffstat:
Msrc/parser.rs | 40++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+), 0 deletions(-)

diff --git a/src/parser.rs b/src/parser.rs @@ -88,6 +88,35 @@ impl<'a> Parser<'a> { self.skip(|c| c.is_ascii_whitespace()) } + pub fn parse_digits(&mut self) -> Result<u16> { + let mut i = self.pos(); + let mut digits = 0u16; + let mut number = 0u16; + + while i < self.len() { + let byte = self.data()[i]; + + // if it's a utf8 char this is not a digit + if (byte & 0x80) == 0x80 || !(byte as char).is_ascii_digit() || digits == 5 { + break; + } + + let digit = (byte - b'0') as u16; + + number = number.saturating_mul(10).saturating_add(digit); + + digits += 1; + i += 1; + } + + if digits == 0 || digits > 5 { + return Err(Error::NotFound); + } + + self.set_pos(i); + Ok(number) + } + pub fn peek_char(&mut self) -> Result<char> { let peek = true; self.pull_or_peek_char(peek) @@ -249,6 +278,17 @@ mod test { } #[test] + fn test_parse_digits() { + let s = "[1315]"; + let mut parser = Parser::from_str(s); + let r1 = parser.parse_char('['); + assert_eq!(r1, Ok(())); + let r2 = parser.parse_digits(); + assert_eq!(r2, Ok(1315)); + assert_eq!(parser.pos(), 5); + } + + #[test] fn test_peek_prev_char() { let s = ".👽."; let mut parser = Parser::from_str(s);