From 74104f76ad6d655b7d4db8c5de64150c71f36919 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 1 Apr 2023 13:10:59 -0700 Subject: [PATCH] wcstod() to skip leading whitespace This matches the C implementation. --- fish-rust/src/wutil/wcstod.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/fish-rust/src/wutil/wcstod.rs b/fish-rust/src/wutil/wcstod.rs index 82268453c..f178a34eb 100644 --- a/fish-rust/src/wutil/wcstod.rs +++ b/fish-rust/src/wutil/wcstod.rs @@ -21,10 +21,22 @@ pub fn wcstod(input: Chars, decimal_sep: char, consumed: &mut usize) -> R where Chars: IntoCharIter, { - let chars = input.chars(); - if chars.clone().next().is_none() { - *consumed = 0; - return Err(Error::Empty); + let mut chars = input.chars(); + let mut whitespace_skipped = 0; + + // Skip leading whitespace. + loop { + match chars.clone().next() { + Some(c) if c.is_whitespace() => { + whitespace_skipped += 1; + chars.next(); + } + None => { + *consumed = 0; + return Err(Error::Empty); + } + _ => break, + } } let ret = parse_partial_iter(chars.clone().fuse(), decimal_sep); @@ -47,7 +59,7 @@ where } } } - *consumed = n; + *consumed = n + whitespace_skipped; Ok(val) } @@ -61,7 +73,10 @@ mod test { #[test] #[allow(clippy::all)] pub fn tests() { - test("12.345", Ok(12.345)); + test_consumed("12.345", Ok(12.345), 6); + test_consumed(" 12.345", Ok(12.345), 8); + test_consumed(" 12.345 ", Ok(12.345), 8); + test_consumed("12.345 ", Ok(12.345), 6); test("12.345e19", Ok(12.345e19)); test("-.1e+9", Ok(-0.1e+9)); test(".125", Ok(0.125));