From 04d81a06c344120d41a838aa1865e405cda2ac5c Mon Sep 17 00:00:00 2001 From: "Tom D." <15268361+anastygnome@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:58:50 +0100 Subject: [PATCH 1/7] perf: dispatch date parser prefixes Choose the date parser from the first character instead of trying every form. --- src/items/date.rs | 79 +++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/items/date.rs b/src/items/date.rs index 69cbf382..73f98d3d 100644 --- a/src/items/date.rs +++ b/src/items/date.rs @@ -28,15 +28,15 @@ use winnow::{ ascii::alpha1, - combinator::{alt, eof, opt, preceded, terminated}, + combinator::{alt, dispatch, eof, fail, opt, peek, preceded, terminated}, error::ErrMode, stream::AsChar, - token::take_while, + token::{any, take_while}, ModalResult, Parser, }; use super::{ - primitive::{ctx_err, dec_uint, multispace1, s}, + primitive::{ctx_err, dec_uint, dec_uint_str, multispace1, s}, year::{year_from_str, year_str}, }; @@ -130,13 +130,30 @@ impl TryFrom for jiff::civil::Date { /// Parse a date that is not in ISO format. ISO dates are parsed by [`iso1`] /// and [`iso2`], which the caller tries first. pub(super) fn parse_non_iso(input: &mut &str) -> ModalResult { - alt((dotted, us, literal1, literal2, literal3)).parse_next(input) + dispatch! { s(peek(any)); + '0'..='9' => numeric, + 'a'..='z' => literal_month_first, + _ => fail, + } + .parse_next(input) } -/// Parse the European `DD.MM.[YYYY]` spelling. -fn dotted(input: &mut &str) -> ModalResult { - let (day, _, month, _, year) = ( - s(dec_uint), +fn numeric(input: &mut &str) -> ModalResult { + let first = s(dec_uint_str).parse_next(input)?; + + dispatch! { s(peek(any)); + '.' => move |input: &mut &str| dotted_after(first, input), + '/' => move |input: &mut &str| us_after(first, input), + '-' | 'a'..='z' => move |input: &mut &str| literal1_after(first, input), + _ => fail, + } + .parse_next(input) +} + +/// Parse the European `DD.MM.[YYYY]` spelling after the shared numeric prefix. +fn dotted_after(first: &str, input: &mut &str) -> ModalResult { + let day = day_from_str(first)?; + let (_, month, _, year) = ( s('.'), s(dec_uint), s('.'), @@ -181,10 +198,8 @@ pub(super) fn iso2(input: &mut &str) -> ModalResult { .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } -/// Parse `[year]/[month]/[day]` or `[month]/[day]/[year]` or `[month]/[day]`. -fn us(input: &mut &str) -> ModalResult { - let (s1, _, n, s2) = ( - s(take_while(1.., AsChar::is_dec_digit)), +fn us_after(first: &str, input: &mut &str) -> ModalResult { + let (_, n, s2) = ( s('/'), s(dec_uint), opt(preceded(s('/'), s(take_while(1.., AsChar::is_dec_digit)))), @@ -192,26 +207,26 @@ fn us(input: &mut &str) -> ModalResult { .parse_next(input)?; match s2 { - Some(s2) if s1.len() >= 4 => { + Some(s2) if first.len() >= 4 => { // [year]/[month]/[day] // // GNU quirk: interpret as [year]/[month]/[day] if the first part is at // least 4 characters long. let day = day_from_str(s2)?; - (s1, n, day) + (first, n, day) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } Some(s2) => { // [month]/[day]/[year] - let month = month_from_str(s1)?; + let month = month_from_str(first)?; (s2, month, n) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) } None => { // [month]/[day] - let month = month_from_str(s1)?; + let month = month_from_str(first)?; (month, n) .try_into() .map_err(|e| ErrMode::Backtrack(ctx_err(e))) @@ -219,11 +234,9 @@ fn us(input: &mut &str) -> ModalResult { } } -/// Parse `14 November 2022`, `14 Nov 2022`, "14nov2022", "14-nov-2022", -/// "14-nov2022", "14nov-2022". -fn literal1(input: &mut &str) -> ModalResult { - let (day, _, month, year) = ( - s(dec_uint), +fn literal1_after(first: &str, input: &mut &str) -> ModalResult { + let day = day_from_str(first)?; + let (_, month, year) = ( opt(s('-')), s(literal_month), opt(terminated( @@ -244,10 +257,19 @@ fn literal1(input: &mut &str) -> ModalResult { } } -/// Parse `November 14, 2022`, `Nov 14, 2022`, and `Nov 14 2022`. -fn literal2(input: &mut &str) -> ModalResult { - let (month, day, year) = ( - s(literal_month), +fn literal_month_first(input: &mut &str) -> ModalResult { + let month = s(literal_month).parse_next(input)?; + + dispatch! { s(peek(any)); + '-' => move |input: &mut &str| literal3_after(month, input), + '0'..='9' => move |input: &mut &str| literal2_after(month, input), + _ => fail, + } + .parse_next(input) +} + +fn literal2_after(month: u8, input: &mut &str) -> ModalResult { + let (day, year) = ( s(dec_uint), opt(terminated( preceded( @@ -273,11 +295,8 @@ fn literal2(input: &mut &str) -> ModalResult { } } -/// Parse `November-14-2022` and `Nov-14-2022`. Unlike `literal2`, the year is mandatory -/// to match the behavior of GNU `date`. -fn literal3(input: &mut &str) -> ModalResult { - let (month, _, day, _, year) = - (s(literal_month), s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?; +fn literal3_after(month: u8, input: &mut &str) -> ModalResult { + let (_, day, _, year) = (s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?; // Map err to Backtrack instead of Cut to avoid early termination of parsing (year, month, day) From ac21feac651cdc8230e0e49de4a323b42b3d3afb Mon Sep 17 00:00:00 2001 From: "Tom D." <15268361+anastygnome@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:59:30 +0100 Subject: [PATCH 2/7] perf: factor shared parser prefixes Parse shared time and offset prefixes once to reduce backtracking in common ISO inputs. --- src/items/offset.rs | 149 ++++++++++++++++++++++++------------------ src/items/time.rs | 52 +++++++++------ src/items/timezone.rs | 14 ++-- 3 files changed, 125 insertions(+), 90 deletions(-) diff --git a/src/items/offset.rs b/src/items/offset.rs index ad2b3597..a240db44 100644 --- a/src/items/offset.rs +++ b/src/items/offset.rs @@ -28,10 +28,10 @@ use std::fmt::Display; use winnow::{ - combinator::{alt, peek}, + combinator::{alt, dispatch, eof, opt, peek, repeat}, error::{ContextError, ErrMode}, stream::{AsChar, Stream}, - token::take_while, + token::{take, take_while}, ModalResult, Parser, }; @@ -196,7 +196,7 @@ pub(super) fn timezone_offset(input: &mut &str) -> ModalResult { // Like GNU, read "+8 years" as a relative item, not as "+8" and "years". // The costlier relative check only runs once an offset has parsed. let start = input.checkpoint(); - let result = alt((timezone_offset_colon, timezone_offset_colonless)).parse_next(input); + let result = timezone_offset_parts.parse_next(input); if matches!(result, Err(ErrMode::Backtrack(_))) { return result; } @@ -242,57 +242,67 @@ fn timezone_name_offset(input: &mut &str) -> ModalResult { Ok(tz) } -/// Parse a timezone offset with a colon separating hours and minutes, e.g., -/// `+08:00`, `+8:00`, `+8:0`. -fn timezone_offset_colon(input: &mut &str) -> ModalResult { - (plus_or_minus, s(dec_uint), s(colon), s(dec_uint)) - .parse_next(input) - .and_then(|(sign, hours, _, minutes)| { - (sign == '-', hours, minutes) - .try_into() - .map_err(|e| ErrMode::Cut(ctx_err(e))) - }) +fn timezone_offset_parts(input: &mut &str) -> ModalResult { + let (sign, digits) = (plus_or_minus, s(dec_uint_str)).parse_next(input)?; + + dispatch! { peek(opt(colon)); + Some(()) => move |input: &mut &str| timezone_offset_colon_after(sign, digits, input), + None => move |_: &mut &str| timezone_offset_colonless_after(sign, digits), + } + .parse_next(input) } -/// Parse a timezone offset without colon, e.g., `+0800`, `+800`, `+08`, `+8`. -fn timezone_offset_colonless(input: &mut &str) -> ModalResult { - (plus_or_minus, s(dec_uint_str)) - .verify_map(|(sign, s)| { - // GNU date accepts numeric offset strings with leading zeroes. For - // example, `+000000110` is valid. In such cases, the string is - // truncated to the last four characters. Thus, `+000000110` becomes - // `+0110` (note that one leading zero is kept). - let s = if s.len() > 4 && s.trim_start_matches('0').len() <= 4 { - &s[s.len() - 4..] - } else { - s - }; - - // Hour and minute values are dependent on the length of the string. - // For example: - // - // - "5" -> 05:00 - // - "05" -> 05:00 - // - "530" -> 05:30 (the minute is the last two characters here) - // - "0530"-> 05:30 - // - "0000530" -> 05:30 - let (h_str, m_str) = match s.len() { - 1 | 2 => (s, "0"), - 3 => s.split_at(1), - 4 => s.split_at(2), - _ => return None, - }; - - let hours = h_str.parse::().ok()?; - let minutes = m_str.parse::().ok()?; - Some((sign, hours, minutes)) - }) - .parse_next(input) - .and_then(|(sign, hours, minutes)| { - (sign == '-', hours, minutes) - .try_into() - .map_err(|e| ErrMode::Cut(ctx_err(e))) - }) +fn timezone_offset_colon_after(sign: char, digits: &str, input: &mut &str) -> ModalResult { + let (_, hours) = dec_uint:: + .parse_peek(digits) + .map_err(|_| ErrMode::Backtrack(ctx_err("timezone hour must be a valid u8 number")))?; + let (_, minutes) = (s(colon), s(dec_uint)).parse_next(input)?; + (sign == '-', hours, minutes) + .try_into() + .map_err(|e| ErrMode::Cut(ctx_err(e))) +} + +fn timezone_offset_colonless_after(sign: char, digits: &str) -> ModalResult { + // GNU date reads one or two digits as hours, three digits as HMM, and four + // digits as HHMM. Extra digits are accepted only when they are leading + // zeroes; the final four digits then provide the HHMM value. + let (_, (hours, minutes)) = alt(( + // One or two digits: hours only, with zero minutes. + ( + take_while(1..=2, AsChar::is_dec_digit).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, _)| (hours, 0)), + // Three digits: one hour digit followed by two minute digits. + ( + take(1usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, minutes, _)| (hours, minutes)), + // Four digits: two hour digits followed by two minute digits. + ( + take(2usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(hours, minutes, _)| (hours, minutes)), + // Longer values: consume leading zeroes while four digits remain, + // then split those final four digits into hours and minutes. + ( + repeat::<_, _, (), _, _>(0.., ('0', peek(take(4usize)))).void(), + take(2usize).parse_to::(), + take(2usize).parse_to::(), + eof::<&str, ContextError>, + ) + .map(|(_, hours, minutes, _)| (hours, minutes)), + )) + .parse_peek(digits) + .map_err(|_| ErrMode::Backtrack(ctx_err("timezone offset must be 1-4 digits")))?; + + (sign == '-', hours, minutes) + .try_into() + .map_err(|e| ErrMode::Cut(ctx_err(e))) } /// Named timezone list. @@ -439,17 +449,19 @@ mod tests { #[test] fn timezone_offset_without_colon() { for (input, expected) in [ - ("+0000", off(false, 0, 0)), // UTC - ("-0000", off(true, 0, 0)), // UTC - ("+0100", off(false, 1, 0)), // positive offset - ("-0600", off(true, 6, 0)), // negative offset - ("+0530", off(false, 5, 30)), // positive offset with non-zero minutes - ("-0330", off(true, 3, 30)), // negative offset with non-zero minutes - ("- 0330", off(true, 3, 30)), // space after sign - ("+530", off(false, 5, 30)), // single-digit hours - ("+05", off(false, 5, 0)), // double-digit hours and no minutes - ("+5", off(false, 5, 0)), // single-digit hours and no minutes - ("+00000530", off(false, 5, 30)), // leading zeroes + ("+0000", off(false, 0, 0)), // UTC + ("-0000", off(true, 0, 0)), // UTC + ("+0100", off(false, 1, 0)), // positive offset + ("-0600", off(true, 6, 0)), // negative offset + ("+0530", off(false, 5, 30)), // positive offset with non-zero minutes + ("-0330", off(true, 3, 30)), // negative offset with non-zero minutes + ("- 0330", off(true, 3, 30)), // space after sign + ("+530", off(false, 5, 30)), // single-digit hours + ("+05", off(false, 5, 0)), // double-digit hours and no minutes + ("+5", off(false, 5, 0)), // single-digit hours and no minutes + ("+00000530", off(false, 5, 30)), // leading zeroes + ("+000000110", off(false, 1, 10)), // leading zeroes before four digits + ("+00000", off(false, 0, 0)), // leading zeroes before zero offset ("+0000abc", off(false, 0, 0)), // space separator can be ignored if immediately followed by alphas (GNU date behavior) ] { let mut s = input; @@ -460,6 +472,7 @@ mod tests { "+2500", // invalid: hours > 24 "-2361", // invalid: minutes > 60 "+2401", // invalid: minutes > 0 when hours == 24 + "+12345", // invalid: too many non-zero digits "+23 days", // invalid: ambiguous with relative time parsing ] { let mut s = input; @@ -467,6 +480,16 @@ mod tests { } } + #[test] + fn timezone_offset_reports_invalid_minutes() { + let mut input = "+0061"; + let error = timezone_offset(&mut input).unwrap_err(); + + assert!(error + .to_string() + .contains("timezone minute must be between 0 and 60")); + } + #[test] fn timezone_name_without_offset() { for (input, expected) in [ diff --git a/src/items/time.rs b/src/items/time.rs index 777ca5e9..c4078344 100644 --- a/src/items/time.rs +++ b/src/items/time.rs @@ -38,7 +38,7 @@ //! > Either ‘am’/‘pm’ or a time zone correction may be specified, but not both. use winnow::{ - combinator::{alt, opt, preceded}, + combinator::{alt, dispatch, fail, opt, peek, preceded}, error::ErrMode, ModalResult, Parser, }; @@ -46,7 +46,7 @@ use winnow::{ use super::{ epoch::sec_and_nsec, offset::{timezone_offset, Offset}, - primitive::{colon, ctx_err, dec_uint, s}, + primitive::{colon, ctx_err, dec_uint, s, space}, }; #[derive(PartialEq, Clone, Debug, Default)] @@ -86,30 +86,42 @@ pub(crate) fn parse(input: &mut &str) -> ModalResult