Struct simple_tokenizer::Tokens
source · pub struct Tokens<'s> { /* private fields */ }Expand description
Tokens instance.
Implementations§
source§impl<'s> Tokens<'s>
impl<'s> Tokens<'s>
sourcepub fn set_offset(&mut self, offset: Offset) -> bool
pub fn set_offset(&mut self, offset: Offset) -> bool
Sets the offset if it is valid, updating position and span.
Returns true if the offset is valid, false otherwise.
sourcepub fn is_at_start(&self) -> bool
pub fn is_at_start(&self) -> bool
Returns true if the current position is the start of input.
sourcepub fn consume_all(&mut self) -> &str
pub fn consume_all(&mut self) -> &str
Consumes the rest of input.
Example
use simple_tokenizer::*;
let mut tokens = "tokens".as_tokens();
assert_eq!(tokens.consume_all(), "tokens");
assert!(tokens.remainder().is_empty());
sourcepub fn token(&mut self, token: impl AsRef<str>) -> bool
pub fn token(&mut self, token: impl AsRef<str>) -> bool
Consume the next substring equal to token or nothing.
Basically a shortcut for bytes_if(token.len(), |s| s == token).is_some().
Example
use simple_tokenizer::*;
let mut tokens = "tok1 tok2".as_tokens();
assert!(tokens.token("tok1"));
assert_eq!(tokens.remainder(), " tok2");
assert!(!tokens.token(" tok3"));
assert_eq!(tokens.remainder(), " tok2");
sourcepub fn tokens(
&mut self,
tokens: impl IntoIterator<Item = impl AsRef<str>>
) -> Option<&str>
pub fn tokens( &mut self, tokens: impl IntoIterator<Item = impl AsRef<str>> ) -> Option<&str>
Try to consume a substring equal to one of the provided tokens. Returns the first successful substring.
Example
use simple_tokenizer::*;
let mut tokens = "tok1 tok2".as_tokens();
assert_eq!(tokens.tokens(&["tok", "tok1"]), Some("tok"));
assert_eq!(tokens.remainder(), "1 tok2");
assert_eq!(tokens.tokens(&["1 tok3", "2 tok2"]), None);
assert_eq!(tokens.remainder(), "1 tok2");
sourcepub fn char(&mut self) -> Option<char>
pub fn char(&mut self) -> Option<char>
Consume the next character.
Example
use simple_tokenizer::*;
let mut tokens = "tokens".as_tokens();
assert_eq!(tokens.char(), Some('t'));
assert_eq!(tokens.remainder(), "okens");
sourcepub fn char_if(&mut self, f: impl FnOnce(char) -> bool) -> Option<char>
pub fn char_if(&mut self, f: impl FnOnce(char) -> bool) -> Option<char>
Consume the next character if it matches a predicate.
Example
use simple_tokenizer::*;
let mut tokens = "tokens".as_tokens();
assert_eq!(tokens.char_if(char::is_alphabetic), Some('t'));
assert_eq!(tokens.remainder(), "okens");
assert_eq!(tokens.char_if(char::is_numeric), None);
assert_eq!(tokens.remainder(), "okens");
sourcepub fn bytes(&mut self, n: usize) -> Option<&str>
pub fn bytes(&mut self, n: usize) -> Option<&str>
Consume the next n bytes.
Example
use simple_tokenizer::*;
let mut tokens = "tokens123".as_tokens();
assert_eq!(tokens.bytes(6), Some("tokens"));
assert_eq!(tokens.remainder(), "123");
assert_eq!(tokens.bytes(5), None);
assert_eq!(tokens.remainder(), "123");
sourcepub fn bytes_if(
&mut self,
n: usize,
f: impl FnOnce(&str) -> bool
) -> Option<&str>
pub fn bytes_if( &mut self, n: usize, f: impl FnOnce(&str) -> bool ) -> Option<&str>
Consume the next n bytes if they match a predicate.
Example
use simple_tokenizer::*;
let mut tokens = "1231234".as_tokens();
assert_eq!(tokens.bytes_if(3, |s| s.chars().all(char::is_numeric)), Some("123"));
assert_eq!(tokens.remainder(), "1234");
assert_eq!(tokens.bytes_if(5, |s| s.chars().all(char::is_numeric)), None);
assert_eq!(tokens.remainder(), "1234");
sourcepub fn chars(&mut self, n: usize) -> Option<&str>
pub fn chars(&mut self, n: usize) -> Option<&str>
Consume the next n characters.
Doesn’t advance if there aren’t enough characters left.
Example
use simple_tokenizer::*;
let mut tokens = "tokens123".as_tokens();
assert_eq!(tokens.chars(6), Some("tokens"));
assert_eq!(tokens.remainder(), "123");
assert_eq!(tokens.chars(5), None);
assert_eq!(tokens.remainder(), "123");
sourcepub fn chars_if(
&mut self,
n: usize,
f: impl FnOnce(&str) -> bool
) -> Option<&str>
pub fn chars_if( &mut self, n: usize, f: impl FnOnce(&str) -> bool ) -> Option<&str>
Consume the next n characters if they match a predicate.
Doesn’t advance if there aren’t enough characters left.
Example
use simple_tokenizer::*;
let mut tokens = "1231234".as_tokens();
assert_eq!(tokens.chars_if(3, |s| s.chars().all(char::is_numeric)), Some("123"));
assert_eq!(tokens.remainder(), "1234");
assert_eq!(tokens.chars_if(5, |s| s.chars().all(char::is_numeric)), None);
assert_eq!(tokens.remainder(), "1234");
sourcepub fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &str
pub fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &str
Consume characters while f returns true.
Returns the consumed substring.
Example
use simple_tokenizer::*;
let mut tokens = "12345word".as_tokens();
assert_eq!(tokens.take_while(char::is_numeric), "12345");
assert_eq!(tokens.remainder(), "word");