From b52a9d0b6a2c15b1bebbd5ffa5481fdf25148556 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 9 Sep 2025 14:32:21 -0400 Subject: Add list parsing test, fix list parsing --- src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3e3c8d6..ffd8235 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ #![warn(clippy::redundant_closure_for_method_calls)] extern crate alloc; +use alloc::borrow::ToOwned; use alloc::boxed::Box; use alloc::string::String; use alloc::sync::Arc; @@ -271,10 +272,24 @@ fn parse_bool(location: &Location, string: &str) -> Result { fn parse_list(string: &str) -> Vec { let mut list = vec![]; let mut item = String::new(); + let mut push = |item: &mut String, push_empty: bool| { + let mut item = take(item); + while item.ends_with([' ', '\t']) { + item.pop(); + } + let leading_space_stripped = item.trim_start_matches([' ', '\t']); + if push_empty || !leading_space_stripped.is_empty() { + list.push(if leading_space_stripped.len() == item.len() { + item + } else { + leading_space_stripped.to_owned() + }); + } + }; let mut chars = string.chars(); while let Some(c) = chars.next() { if c == ',' { - list.push(item); + push(&mut item, true); item = String::new(); } else if c == '\\' { if let Some(next) = chars.next() { @@ -292,9 +307,7 @@ fn parse_list(string: &str) -> Vec { item.push(c); } } - if !item.is_empty() { - list.push(item); - } + push(&mut item, false); list } -- cgit v1.2.3