diff --git a/edn/src/edn_token.rs b/edn/src/edn_token.rs index 723614c7..3343ef98 100644 --- a/edn/src/edn_token.rs +++ b/edn/src/edn_token.rs @@ -10,11 +10,12 @@ pub enum Token<'a> { } impl<'a> Token<'a> { - pub fn chomp (source: &'a str) -> Result<(&'a str, Self), ParseError> { + pub const fn chomp (source: &'a str) -> Result<(&'a str, Self), ParseError> { use Token::*; use konst::string::{split_at, char_indices}; let mut state = Self::Nil; - for (index, c) in source.char_indices() { + let mut chars = char_indices(source); + while let Some(((index, c), next)) = chars.next() { state = match state { // must begin expression Nil => match c { @@ -64,19 +65,24 @@ impl<'a> Token<'a> { '(' => Exp(source, index, length + 1, depth + 1), _ => Exp(source, index, length + 1, depth) }, - } + }; + chars = next } Ok(("", state)) } - pub fn number (digits: &str) -> usize { + pub const fn number (digits: &str) -> usize { let mut value = 0; - for c in digits.chars() { value = 10 * value + Self::digit(c); } + let mut chars = konst::string::char_indices(digits); + while let Some(((_, c), next)) = chars.next() { + value = 10 * value + Self::digit(c); + chars = next + } value } pub const fn digit (c: char) -> usize { match c { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, - '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, _ => unreachable!() } + '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, _ => panic!("not a digit") } } }