From 1d0c2cf95378118e9aa0a42af040b1a1a3318f92 Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Fri, 29 Nov 2024 17:31:36 +0100 Subject: [PATCH] fix(specctra): less (possible) panic!s in read_string --- src/specctra/read.rs | 45 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/specctra/read.rs b/src/specctra/read.rs index 067439e..585066f 100644 --- a/src/specctra/read.rs +++ b/src/specctra/read.rs @@ -13,6 +13,8 @@ pub enum ParseError { Expected(&'static str), #[error("expected ({0}")] ExpectedStartOfList(&'static str), + #[error("found a space inside a quoted string, but file didn't declare this possibility")] + UnexpectedSpaceInQuotedStr, } impl ParseError { @@ -241,9 +243,24 @@ impl ListTokenizer { } fn read_string(&mut self) -> Result { - if let Some(chr) = self.quote_char { - if chr == self.peek_char()? { - return self.read_quoted(); + if let Some(quote_chr) = self.quote_char { + if quote_chr == self.peek_char()? { + let mut string = String::new(); + assert_eq!(self.next_char().unwrap(), quote_chr); + + loop { + let ctx = self.context(); + let chr = self.next_char()?; + if chr == ' ' && !self.space_in_quoted { + return Err(ParseError::UnexpectedSpaceInQuotedStr.add_context(ctx)); + } else if chr == quote_chr { + break; + } else { + string.push(chr); + } + } + + return Ok(string); } } self.read_unquoted() @@ -267,28 +284,6 @@ impl ListTokenizer { } } - fn read_quoted(&mut self) -> Result { - let mut string = String::new(); - - if self.next_char().unwrap() != self.quote_char.unwrap() { - panic!(); - } - - loop { - let chr = self.peek_char()?; - if !self.space_in_quoted && chr == ' ' { - panic!("found a space inside a quoted string, but file didn't declare this possibility"); - } - if chr == self.quote_char.unwrap() { - self.next_char().unwrap(); - break; - } - string.push(self.next_char().unwrap()); - } - - Ok(string) - } - // the following two methods effectively allow 1 token of lookahead // returns next token, either a cached one returned earlier or a newly read one