fmt: massively refactor duration parsing

This introduces a new internal helper type, `DurationUnits`, that is
used to parse all duration types and formats. It centralizes the logic
and trims away a lot of fat. This should reduce code size (although I
haven't checked) and also improve perf. Currently, it does significantly
improve perf for parsing longer durations and especially for `Span`. It
does however slightly regress perf for parsing shorter `SignedDuration`
values.

However, I think there are a lot of perf improvement opportunities. I'll
put those in a subsequent commit.

(We still haven't implemented `std::time::Duration` yet. But
`DurationUnits` is clearly designed with it in mind. This is one helluva
yak shave!)

This also fixes a long-standing bug where we couldn't parse
`abs(i64::MIN) secs ago`. The `DurationUnits` design was specifically
motivated (in part) by this.
This commit is contained in:
Andrew Gallant 2025-11-01 14:35:21 -04:00
parent cf26d14aaa
commit 7744244e3f
No known key found for this signature in database
GPG key ID: B2E3A4923F8B0D44
10 changed files with 833 additions and 566 deletions

View file

@ -2,17 +2,10 @@ use crate::{
error::{err, ErrorContext},
fmt::{
friendly::parser_label,
util::{
fractional_time_to_duration, fractional_time_to_span,
parse_temporal_fraction, set_duration_unit_value,
set_span_unit_value,
},
util::{parse_temporal_fraction, DurationUnits},
Parsed,
},
util::{
escape,
t::{self, C},
},
util::{c::Sign, escape},
Error, SignedDuration, Span, Unit,
};
@ -283,18 +276,20 @@ impl SpanParser {
optional sign, but no integer was found",
));
};
let Parsed { value: span, input } =
self.parse_units_to_span(input, first_unit_value)?;
let Parsed { value: mut builder, input } =
self.parse_duration_units(input, first_unit_value)?;
// As with the prefix sign parsing, guard it to avoid calling the
// function.
let (sign, input) = if !input.first().map_or(false, is_whitespace) {
(sign.unwrap_or(t::Sign::N::<1>()), input)
(sign.unwrap_or(Sign::Positive), input)
} else {
let parsed = self.parse_suffix_sign(sign, input)?;
(parsed.value, parsed.input)
};
Ok(Parsed { value: span * i64::from(sign.get()), input })
builder.set_sign(sign);
let span = builder.to_span()?;
Ok(Parsed { value: span, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
@ -324,191 +319,42 @@ impl SpanParser {
optional sign, but no integer was found",
));
};
let Parsed { value: mut sdur, input } =
self.parse_units_to_duration(input, first_unit_value)?;
let Parsed { value: mut builder, input } =
self.parse_duration_units(input, first_unit_value)?;
// As with the prefix sign parsing, guard it to avoid calling the
// function.
let (sign, input) = if !input.first().map_or(false, is_whitespace) {
(sign.unwrap_or(t::Sign::N::<1>()), input)
(sign.unwrap_or(Sign::Positive), input)
} else {
let parsed = self.parse_suffix_sign(sign, input)?;
(parsed.value, parsed.input)
};
if sign < C(0) {
sdur = -sdur;
}
builder.set_sign(sign);
let sdur = builder.to_duration()?;
Ok(Parsed { value: sdur, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_units_to_span<'i>(
fn parse_duration_units<'i>(
&self,
mut input: &'i [u8],
first_unit_value: i64,
) -> Result<Parsed<'i, Span>, Error> {
first_unit_value: u64,
) -> Result<Parsed<'i, DurationUnits>, Error> {
let mut parsed_any_after_comma = true;
let mut prev_unit: Option<Unit> = None;
let mut value = first_unit_value;
let mut span = Span::new();
let mut builder = DurationUnits::default();
loop {
let parsed = self.parse_hms_maybe(input, value)?;
input = parsed.input;
if let Some(hms) = parsed.value {
if let Some(prev_unit) = prev_unit {
if prev_unit <= Unit::Hour {
return Err(err!(
"found 'HH:MM:SS' after unit {prev_unit}, \
but 'HH:MM:SS' can only appear after \
years, months, weeks or days",
prev_unit = prev_unit.singular(),
));
}
}
span = set_span_unit_value(Unit::Hour, hms.hour, span)?;
span = set_span_unit_value(Unit::Minute, hms.minute, span)?;
span = if let Some(fraction) = hms.fraction {
fractional_time_to_span(
Unit::Second,
hms.second,
fraction,
span,
)?
} else {
set_span_unit_value(Unit::Second, hms.second, span)?
};
break;
}
let fraction =
if input.first().map_or(false, |&b| b == b'.' || b == b',') {
let parsed = parse_temporal_fraction(input)?;
input = parsed.input;
parsed.value
} else {
None
};
// Eat any optional whitespace between the unit value and label.
input = self.parse_optional_whitespace(input).input;
// Parse the actual unit label/designator.
let parsed = self.parse_unit_designator(input)?;
input = parsed.input;
let unit = parsed.value;
// A comma is allowed to immediately follow the designator.
// Since this is a rarer case, we guard it with a check to see
// if the comma is there and only then call the function (which is
// marked unlineable to try and keep the hot path tighter).
if input.first().map_or(false, |&b| b == b',') {
input = self.parse_optional_comma(input)?.input;
parsed_any_after_comma = false;
}
if let Some(prev_unit) = prev_unit {
if prev_unit <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = prev_unit.singular(),
));
}
}
prev_unit = Some(unit);
if let Some(fraction) = fraction {
span = fractional_time_to_span(unit, value, fraction, span)?;
// Once we see a fraction, we are done. We don't permit parsing
// any more units. That is, a fraction can only occur on the
// lowest unit of time.
break;
} else {
span = set_span_unit_value(unit, value, span)?;
}
// Eat any optional whitespace after the designator (or comma) and
// before the next unit value. But if we don't see a unit value,
// we don't eat the whitespace.
let after_whitespace = self.parse_optional_whitespace(input).input;
let parsed = self.parse_unit_value(after_whitespace)?;
value = match parsed.value {
None => break,
Some(value) => value,
};
input = parsed.input;
parsed_any_after_comma = true;
}
if !parsed_any_after_comma {
return Err(err!(
"found comma at the end of duration, \
but a comma indicates at least one more \
unit follows and none were found after \
{prev_unit}",
// OK because parsed_any_after_comma can only
// be false when prev_unit is set.
prev_unit = prev_unit.unwrap().plural(),
));
}
Ok(Parsed { value: span, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_units_to_duration<'i>(
&self,
mut input: &'i [u8],
first_unit_value: i64,
) -> Result<Parsed<'i, SignedDuration>, Error> {
let mut parsed_any_after_comma = true;
let mut prev_unit: Option<Unit> = None;
let mut value = first_unit_value;
let mut sdur = SignedDuration::ZERO;
loop {
let parsed = self.parse_hms_maybe(input, value)?;
input = parsed.input;
if let Some(hms) = parsed.value {
if let Some(prev_unit) = prev_unit {
if prev_unit <= Unit::Hour {
return Err(err!(
"found 'HH:MM:SS' after unit {prev_unit}, \
but 'HH:MM:SS' can only appear after \
years, months, weeks or days",
prev_unit = prev_unit.singular(),
));
}
}
sdur = set_duration_unit_value(
Unit::Hour,
builder.set_hms(
hms.hour,
sdur,
false,
)?;
sdur = set_duration_unit_value(
Unit::Minute,
hms.minute,
sdur,
false,
hms.second,
hms.fraction,
)?;
sdur = if let Some(fraction) = hms.fraction {
fractional_time_to_duration(
Unit::Second,
hms.second,
fraction,
sdur,
false,
)?
} else {
set_duration_unit_value(
Unit::Second,
hms.second,
sdur,
false,
)?
};
break;
}
@ -538,30 +384,13 @@ impl SpanParser {
parsed_any_after_comma = false;
}
if let Some(prev_unit) = prev_unit {
if prev_unit <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = prev_unit.singular(),
));
}
}
prev_unit = Some(unit);
builder.set_unit_value(unit, value)?;
if let Some(fraction) = fraction {
sdur = fractional_time_to_duration(
unit, value, fraction, sdur, false,
)?;
builder.set_fraction(fraction)?;
// Once we see a fraction, we are done. We don't permit parsing
// any more units. That is, a fraction can only occur on the
// lowest unit of time.
break;
} else {
sdur = set_duration_unit_value(unit, value, sdur, false)?;
}
// Eat any optional whitespace after the designator (or comma) and
@ -580,14 +409,10 @@ impl SpanParser {
return Err(err!(
"found comma at the end of duration, \
but a comma indicates at least one more \
unit follows and none were found after \
{prev_unit}",
// OK because parsed_any_after_comma can only
// be false when prev_unit is set.
prev_unit = prev_unit.unwrap().plural(),
unit follows",
));
}
Ok(Parsed { value: sdur, input })
Ok(Parsed { value: builder, input })
}
/// This possibly parses a `HH:MM:SS[.fraction]`.
@ -599,7 +424,7 @@ impl SpanParser {
fn parse_hms_maybe<'i>(
&self,
input: &'i [u8],
hour: i64,
hour: u64,
) -> Result<Parsed<'i, Option<HMS>>, Error> {
if !input.first().map_or(false, |&b| b == b':') {
return Ok(Parsed { input, value: None });
@ -621,7 +446,7 @@ impl SpanParser {
fn parse_hms<'i>(
&self,
input: &'i [u8],
hour: i64,
hour: u64,
) -> Result<Parsed<'i, HMS>, Error> {
let Parsed { input, value } = self.parse_unit_value(input)?;
let Some(minute) = value else {
@ -663,55 +488,50 @@ impl SpanParser {
///
/// Note that this is safe to call on untrusted input. It will not attempt
/// to consume more input than could possibly fit into a parsed integer.
///
/// Since this returns a `u64`, it is possible that an integer that cannot
/// fit into an `i64` is returned. Callers should handle this. (Indeed,
/// `DurationUnits` handles this case.)
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_unit_value<'i>(
&self,
mut input: &'i [u8],
) -> Result<Parsed<'i, Option<i64>>, Error> {
) -> Result<Parsed<'i, Option<u64>>, Error> {
// Discovered via `i64::MAX.to_string().len()`.
const MAX_I64_DIGITS: usize = 19;
let mut digit_count = 0;
let mut n: i64 = 0;
while digit_count <= MAX_I64_DIGITS
&& input.get(digit_count).map_or(false, u8::is_ascii_digit)
{
let byte = input[digit_count];
digit_count += 1;
// This is mostly manually inlined from `util::parse::i64`.
// Namely, `parse::i64` requires knowing all of the
// digits up front. But we don't really know that here.
// So as we parse the digits, we also accumulate them
// into an integer. This avoids a second pass. (I guess
// `util::parse::i64` could be better designed? Meh.)
//
// Note though that we parse into a `u64` since that's
// what our duration components want.
// This part is manually inlined from `util::parse::i64`.
// Namely, `parse::i64` requires knowing all of the
// digits up front. But we don't really know that here.
// So as we parse the digits, we also accumulate them
// into an integer. This avoids a second pass. (I guess
// `util::parse::i64` could be better designed? Meh.)
let digit = match byte.checked_sub(b'0') {
None => {
return Err(err!(
"invalid digit, expected 0-9 but got {}",
escape::Byte(byte),
));
}
Some(digit) if digit > 9 => {
return Err(err!(
"invalid digit, expected 0-9 but got {}",
escape::Byte(byte),
))
}
Some(digit) => {
debug_assert!((0..=9).contains(&digit));
i64::from(digit)
}
};
let mut digit_count = 0;
let mut n: u64 = 0;
while digit_count <= MAX_I64_DIGITS {
let Some(&byte) = input.get(digit_count) else { break };
if !byte.is_ascii_digit() {
break;
}
digit_count += 1;
// OK because we confirmed `byte` is an ASCII digit.
let digit = u64::from(byte - b'0');
n = n
.checked_mul(10)
.and_then(|n| n.checked_add(digit))
.ok_or_else(|| {
err!(
"number '{}' too big to parse into 64-bit integer",
escape::Bytes(&input[..digit_count]),
)
})?;
.ok_or_else(
#[inline(never)]
|| {
err!(
"number `{}` too big to parse into 64-bit integer",
escape::Bytes(&input[..digit_count]),
)
},
)?;
}
if digit_count == 0 {
return Ok(Parsed { value: None, input });
@ -759,14 +579,14 @@ impl SpanParser {
fn parse_prefix_sign<'i>(
&self,
input: &'i [u8],
) -> Parsed<'i, Option<t::Sign>> {
) -> Parsed<'i, Option<Sign>> {
let Some(sign) = input.first().copied() else {
return Parsed { value: None, input };
};
let sign = if sign == b'+' {
t::Sign::N::<1>()
Sign::Positive
} else if sign == b'-' {
t::Sign::N::<-1>()
Sign::Negative
} else {
return Parsed { value: None, input };
};
@ -789,17 +609,17 @@ impl SpanParser {
#[inline(never)]
fn parse_suffix_sign<'i>(
&self,
prefix_sign: Option<t::Sign>,
prefix_sign: Option<Sign>,
mut input: &'i [u8],
) -> Result<Parsed<'i, t::Sign>, Error> {
) -> Result<Parsed<'i, Sign>, Error> {
if !input.first().map_or(false, is_whitespace) {
let sign = prefix_sign.unwrap_or(t::Sign::N::<1>());
let sign = prefix_sign.unwrap_or(Sign::Positive);
return Ok(Parsed { value: sign, input });
}
// Eat any additional whitespace we find before looking for 'ago'.
input = self.parse_optional_whitespace(&input[1..]).input;
let (suffix_sign, input) = if input.starts_with(b"ago") {
(Some(t::Sign::N::<-1>()), &input[3..])
(Some(Sign::Negative), &input[3..])
} else {
(None, input)
};
@ -812,7 +632,7 @@ impl SpanParser {
}
(Some(sign), None) => sign,
(None, Some(sign)) => sign,
(None, None) => t::Sign::N::<1>(),
(None, None) => Sign::Positive,
};
Ok(Parsed { value: sign, input })
}
@ -865,10 +685,10 @@ impl SpanParser {
/// A type that represents the parsed components of `HH:MM:SS[.fraction]`.
#[derive(Debug)]
struct HMS {
hour: i64,
minute: i64,
second: i64,
fraction: Option<i32>,
hour: u64,
minute: u64,
second: u64,
fraction: Option<u32>,
}
/// Returns true if the byte is ASCII whitespace.
@ -992,7 +812,7 @@ mod tests {
);
insta::assert_snapshot!(
p("2 months, "),
@r###"failed to parse "2 months, " in the "friendly" format: found comma at the end of duration, but a comma indicates at least one more unit follows and none were found after months"###,
@r#"failed to parse "2 months, " in the "friendly" format: found comma at the end of duration, but a comma indicates at least one more unit follows"#,
);
insta::assert_snapshot!(
p("2 months ,"),
@ -1064,7 +884,7 @@ mod tests {
);
insta::assert_snapshot!(
p("19999 years ago"),
@r###"failed to parse "19999 years ago" in the "friendly" format: failed to set value 19999 as year unit on span: parameter 'years' with value 19999 is not in the required range of -19998..=19998"###,
@r#"failed to parse "19999 years ago" in the "friendly" format: failed to set value -19999 as year unit on span: parameter 'years' with value -19999 is not in the required range of -19998..=19998"#,
);
insta::assert_snapshot!(
@ -1073,7 +893,7 @@ mod tests {
);
insta::assert_snapshot!(
p("239977 months ago"),
@r###"failed to parse "239977 months ago" in the "friendly" format: failed to set value 239977 as month unit on span: parameter 'months' with value 239977 is not in the required range of -239976..=239976"###,
@r#"failed to parse "239977 months ago" in the "friendly" format: failed to set value -239977 as month unit on span: parameter 'months' with value -239977 is not in the required range of -239976..=239976"#,
);
insta::assert_snapshot!(
@ -1082,7 +902,7 @@ mod tests {
);
insta::assert_snapshot!(
p("1043498 weeks ago"),
@r###"failed to parse "1043498 weeks ago" in the "friendly" format: failed to set value 1043498 as week unit on span: parameter 'weeks' with value 1043498 is not in the required range of -1043497..=1043497"###,
@r#"failed to parse "1043498 weeks ago" in the "friendly" format: failed to set value -1043498 as week unit on span: parameter 'weeks' with value -1043498 is not in the required range of -1043497..=1043497"#,
);
insta::assert_snapshot!(
@ -1091,16 +911,16 @@ mod tests {
);
insta::assert_snapshot!(
p("7304485 days ago"),
@r###"failed to parse "7304485 days ago" in the "friendly" format: failed to set value 7304485 as day unit on span: parameter 'days' with value 7304485 is not in the required range of -7304484..=7304484"###,
@r#"failed to parse "7304485 days ago" in the "friendly" format: failed to set value -7304485 as day unit on span: parameter 'days' with value -7304485 is not in the required range of -7304484..=7304484"#,
);
insta::assert_snapshot!(
p("9223372036854775808 nanoseconds"),
@r###"failed to parse "9223372036854775808 nanoseconds" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
@r#"failed to parse "9223372036854775808 nanoseconds" in the "friendly" format: `9223372036854775808` nanoseconds is too big (or small) to fit into a signed 64-bit integer"#,
);
insta::assert_snapshot!(
p("9223372036854775808 nanoseconds ago"),
@r###"failed to parse "9223372036854775808 nanoseconds ago" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
@r#"failed to parse "9223372036854775808 nanoseconds ago" in the "friendly" format: fractional nanosecond units are not allowed"#,
);
}
@ -1110,11 +930,11 @@ mod tests {
insta::assert_snapshot!(
p("1.5 years"),
@r###"failed to parse "1.5 years" in the "friendly" format: fractional year units are not allowed"###,
@r#"failed to parse "1.5 years" in the "friendly" format: fractional years are not supported"#,
);
insta::assert_snapshot!(
p("1.5 nanos"),
@r###"failed to parse "1.5 nanos" in the "friendly" format: fractional nanosecond units are not allowed"###,
@r#"failed to parse "1.5 nanos" in the "friendly" format: fractional nanoseconds are not supported"#,
);
}
@ -1136,7 +956,7 @@ mod tests {
);
insta::assert_snapshot!(
p("2 hours, 05:06:07"),
@r###"failed to parse "2 hours, 05:06:07" in the "friendly" format: found 'HH:MM:SS' after unit hour, but 'HH:MM:SS' can only appear after years, months, weeks or days"###,
@r#"failed to parse "2 hours, 05:06:07" in the "friendly" format: found `HH:MM:SS` after unit hour, but `HH:MM:SS` can only appear after years, months, weeks or days"#,
);
}
@ -1160,19 +980,11 @@ mod tests {
);
insta::assert_snapshot!(
perr("9223372036854775808s"),
@r###"failed to parse "9223372036854775808s" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
@r#"failed to parse "9223372036854775808s" in the "friendly" format: `9223372036854775808` seconds is too big (or small) to fit into a signed 64-bit integer"#,
);
// This is kinda bush league, since -9223372036854775808 is the
// minimum i64 value. But we fail to parse it because its absolute
// value does not fit into an i64. Normally this would be bad juju
// because it could imply that `SignedDuration::MIN` could serialize
// successfully but then fail to deserialize. But the friendly printer
// will try to use larger units before going to smaller units. So
// `-9223372036854775808s` will never actually be emitted by the
// friendly printer.
insta::assert_snapshot!(
perr("-9223372036854775808s"),
@r###"failed to parse "-9223372036854775808s" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
p("-9223372036854775808s"),
@"-PT2562047788015215H30M8S",
);
}
@ -1232,25 +1044,25 @@ mod tests {
insta::assert_snapshot!(p("-2562047788015215hours"), @"-PT2562047788015215H");
insta::assert_snapshot!(
pe("2562047788015216hrs"),
@r###"failed to parse "2562047788015216hrs" in the "friendly" format: converting 2562047788015216 hours to seconds overflows i64"###,
@r#"failed to parse "2562047788015216hrs" in the "friendly" format: accumulated `SignedDuration` of `0s` overflowed when adding 2562047788015216 of unit hour"#,
);
insta::assert_snapshot!(p("153722867280912930minutes"), @"PT2562047788015215H30M");
insta::assert_snapshot!(p("153722867280912930minutes ago"), @"-PT2562047788015215H30M");
insta::assert_snapshot!(
pe("153722867280912931mins"),
@r#"failed to parse "153722867280912931mins" in the "friendly" format: converting 153722867280912931 minutes to seconds overflows i64"#,
@r#"failed to parse "153722867280912931mins" in the "friendly" format: accumulated `SignedDuration` of `0s` overflowed when adding 153722867280912931 of unit minute"#,
);
insta::assert_snapshot!(p("9223372036854775807seconds"), @"PT2562047788015215H30M7S");
insta::assert_snapshot!(p("-9223372036854775807seconds"), @"-PT2562047788015215H30M7S");
insta::assert_snapshot!(
pe("9223372036854775808s"),
@r###"failed to parse "9223372036854775808s" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
@r#"failed to parse "9223372036854775808s" in the "friendly" format: `9223372036854775808` seconds is too big (or small) to fit into a signed 64-bit integer"#,
);
insta::assert_snapshot!(
pe("-9223372036854775808s"),
@r###"failed to parse "-9223372036854775808s" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
p("-9223372036854775808s"),
@"-PT2562047788015215H30M8S",
);
}
@ -1288,7 +1100,7 @@ mod tests {
);
insta::assert_snapshot!(
p("2 minutes, "),
@r###"failed to parse "2 minutes, " in the "friendly" format: found comma at the end of duration, but a comma indicates at least one more unit follows and none were found after minutes"###,
@r#"failed to parse "2 minutes, " in the "friendly" format: found comma at the end of duration, but a comma indicates at least one more unit follows"#,
);
insta::assert_snapshot!(
p("2 minutes ,"),
@ -1327,7 +1139,7 @@ mod tests {
// Unlike `Span`, this just overflows because it can't be parsed
// as a 64-bit integer.
pe("9223372036854775808 micros"),
@r###"failed to parse "9223372036854775808 micros" in the "friendly" format: number '9223372036854775808' too big to parse into 64-bit integer"###,
@r#"failed to parse "9223372036854775808 micros" in the "friendly" format: `9223372036854775808` microseconds is too big (or small) to fit into a signed 64-bit integer"#,
);
// one fewer is okay
insta::assert_snapshot!(
@ -1342,7 +1154,7 @@ mod tests {
insta::assert_snapshot!(
p("1.5 nanos"),
@r###"failed to parse "1.5 nanos" in the "friendly" format: fractional nanosecond units are not allowed"###,
@r#"failed to parse "1.5 nanos" in the "friendly" format: fractional nanoseconds are not supported"#,
);
}
@ -1364,7 +1176,7 @@ mod tests {
);
insta::assert_snapshot!(
p("2 hours, 05:06:07"),
@r###"failed to parse "2 hours, 05:06:07" in the "friendly" format: found 'HH:MM:SS' after unit hour, but 'HH:MM:SS' can only appear after years, months, weeks or days"###,
@r#"failed to parse "2 hours, 05:06:07" in the "friendly" format: found `HH:MM:SS` after unit hour, but `HH:MM:SS` can only appear after years, months, weeks or days"#,
);
}
}

View file

@ -593,7 +593,7 @@ impl<'f, 'i, 't> Parser<'f, 'i, 't> {
// I believe this error can never happen, since we know we have no more
// than 9 ASCII digits. Any sequence of 9 ASCII digits can be parsed
// into an `i64`.
let nanoseconds = parse::fraction(digits, 9).map_err(|err| {
let nanoseconds = parse::fraction(digits).map_err(|err| {
err!(
"failed to parse {digits:?} as fractional second component \
(up to 9 digits, nanosecond precision): {err}",

View file

@ -5,11 +5,7 @@ use crate::{
offset::{self, ParsedOffset},
rfc9557::{self, ParsedAnnotations},
temporal::Pieces,
util::{
fractional_time_to_duration, fractional_time_to_span,
parse_temporal_fraction, set_duration_unit_value,
set_span_unit_value,
},
util::{parse_temporal_fraction, DurationUnits},
Parsed,
},
span::Span,
@ -18,6 +14,7 @@ use crate::{
TimeZoneDatabase,
},
util::{
c::Sign,
escape, parse,
t::{self, C},
},
@ -807,6 +804,10 @@ impl DateTimeParser {
&self,
input: &'i [u8],
) -> Result<Parsed<'i, t::Year>, Error> {
// TODO: We could probably decrease the codegen for this function,
// or at least make it tighter, by putting the code for signed years
// behind an unlineable function.
let Parsed { value: sign, input } = self.parse_year_sign(input);
if let Some(sign) = sign {
let (year, input) = parse::split(input, 6).ok_or_else(|| {
@ -823,13 +824,13 @@ impl DateTimeParser {
})?;
let year =
t::Year::try_new("year", year).context("year is not valid")?;
if year == C(0) && sign < C(0) {
if year == C(0) && sign.is_negative() {
return Err(err!(
"year zero must be written without a sign or a \
positive sign, but not a negative sign",
));
}
Ok(Parsed { value: year * sign, input })
Ok(Parsed { value: year * sign.as_ranged_integer(), input })
} else {
let (year, input) = parse::split(input, 4).ok_or_else(|| {
err!(
@ -1094,14 +1095,14 @@ impl DateTimeParser {
fn parse_year_sign<'i>(
&self,
mut input: &'i [u8],
) -> Parsed<'i, Option<t::Sign>> {
) -> Parsed<'i, Option<Sign>> {
let Some(sign) = input.get(0).copied() else {
return Parsed { value: None, input };
};
let sign = if sign == b'+' {
t::Sign::N::<1>()
Sign::Positive
} else if sign == b'-' {
t::Sign::N::<-1>()
Sign::Negative
} else {
return Parsed { value: None, input };
};
@ -1155,70 +1156,79 @@ impl SpanParser {
let original = escape::Bytes(input);
let Parsed { value: sign, input } = self.parse_sign(input);
let Parsed { input, .. } = self.parse_duration_designator(input)?;
let Parsed { value: (mut span, parsed_any_date), input } =
self.parse_date_units(input, Span::new())?;
let mut builder = DurationUnits::default();
let Parsed { input, .. } =
self.parse_duration_date_units(input, &mut builder)?;
let Parsed { value: has_time, mut input } =
self.parse_time_designator(input);
if has_time {
let parsed = self.parse_time_units(input, span)?;
let parsed =
self.parse_duration_time_units(input, &mut builder)?;
input = parsed.input;
let (time_span, parsed_any_time) = parsed.value;
if !parsed_any_time {
if builder.get_min().map_or(true, |min| min > Unit::Hour) {
return Err(err!(
"found a time designator (T or t) in an ISO 8601 \
duration string in {original:?}, but did not find \
any time units",
));
}
span = time_span;
} else if !parsed_any_date {
return Err(err!(
"found the start of a ISO 8601 duration string \
in {original:?}, but did not find any units",
));
}
if sign < C(0) {
span = span.negate();
}
builder.set_sign(sign);
let span = builder.to_span()?;
Ok(Parsed { value: span, input })
}
// BREADCRUMBS: Use `DurationUnits` in `parse_duration`.
//
// Then I think we can start cleaning up code (like parsing as `u64`)
// and making the `i64::MIN` test cases work.
//
// Then I think we can have a little fun optimizing.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_duration<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, SignedDuration>, Error> {
let original = escape::Bytes(input);
let Parsed { value: sign, input } = self.parse_sign(input);
let Parsed { input, .. } = self.parse_duration_designator(input)?;
let Parsed { value: has_time, input } =
self.parse_time_designator(input);
if !has_time {
return Err(err!(
"parsing ISO 8601 duration into SignedDuration requires \
"parsing ISO 8601 duration into a `SignedDuration` requires \
that the duration contain a time component and no \
components of days or greater",
));
}
let Parsed { value: dur, input } =
self.parse_time_units_duration(input, sign == C(-1))?;
Ok(Parsed { value: dur, input })
let mut builder = DurationUnits::default();
let Parsed { value: (), input } =
self.parse_duration_time_units(input, &mut builder)?;
if builder.get_min().map_or(true, |min| min > Unit::Hour) {
return Err(err!(
"found a time designator (T or t) in an ISO 8601 \
duration string in {original:?}, but did not find \
any time units",
));
}
builder.set_sign(sign);
let sdur = builder.to_duration()?;
Ok(Parsed { value: sdur, input })
}
/// Parses consecutive date units from an ISO 8601 duration string into the
/// span given.
///
/// If 1 or more units were found, then `true` is also returned. Otherwise,
/// `false` indicates that no units were parsed. (Which the caller may want
/// to treat as an error.)
/// Parses consecutive units from an ISO 8601 duration string into the
/// `DurationUnits` given.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_date_units<'i>(
fn parse_duration_date_units<'i>(
&self,
mut input: &'i [u8],
mut span: Span,
) -> Result<Parsed<'i, (Span, bool)>, Error> {
let mut parsed_any = false;
let mut prev_unit: Option<Unit> = None;
builder: &mut DurationUnits,
) -> Result<Parsed<'i, ()>, Error> {
loop {
let parsed = self.parse_unit_value(input)?;
input = parsed.input;
@ -1228,39 +1238,19 @@ impl SpanParser {
input = parsed.input;
let unit = parsed.value;
if let Some(prev_unit) = prev_unit {
if prev_unit <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = prev_unit.singular(),
));
}
}
prev_unit = Some(unit);
span = set_span_unit_value(unit, value, span)?;
parsed_any = true;
builder.set_unit_value(unit, value as u64)?;
}
Ok(Parsed { value: (span, parsed_any), input })
Ok(Parsed { value: (), input })
}
/// Parses consecutive time units from an ISO 8601 duration string into the
/// span given.
///
/// If 1 or more units were found, then `true` is also returned. Otherwise,
/// `false` indicates that no units were parsed. (Which the caller may want
/// to treat as an error.)
/// `DurationUnits` given.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_time_units<'i>(
fn parse_duration_time_units<'i>(
&self,
mut input: &'i [u8],
mut span: Span,
) -> Result<Parsed<'i, (Span, bool)>, Error> {
let mut parsed_any = false;
let mut prev_unit: Option<Unit> = None;
builder: &mut DurationUnits,
) -> Result<Parsed<'i, ()>, Error> {
loop {
let parsed = self.parse_unit_value(input)?;
input = parsed.input;
@ -1274,96 +1264,16 @@ impl SpanParser {
input = parsed.input;
let unit = parsed.value;
if let Some(prev_unit) = prev_unit {
if prev_unit <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = prev_unit.singular(),
));
}
}
prev_unit = Some(unit);
parsed_any = true;
builder.set_unit_value(unit, value as u64)?;
if let Some(fraction) = fraction {
span = fractional_time_to_span(unit, value, fraction, span)?;
builder.set_fraction(fraction)?;
// Once we see a fraction, we are done. We don't permit parsing
// any more units. That is, a fraction can only occur on the
// lowest unit of time.
break;
} else {
span = set_span_unit_value(unit, value, span)?;
}
}
Ok(Parsed { value: (span, parsed_any), input })
}
/// Parses consecutive time units from an ISO 8601 duration string into
/// a Jiff signed duration.
///
/// If no time units are found, then this returns an error.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_time_units_duration<'i>(
&self,
mut input: &'i [u8],
negative: bool,
) -> Result<Parsed<'i, SignedDuration>, Error> {
let mut parsed_any = false;
let mut prev_unit: Option<Unit> = None;
let mut sdur = SignedDuration::ZERO;
loop {
let parsed = self.parse_unit_value(input)?;
input = parsed.input;
let Some(value) = parsed.value else { break };
let parsed = parse_temporal_fraction(input)?;
input = parsed.input;
let fraction = parsed.value;
let parsed = self.parse_unit_time_designator(input)?;
input = parsed.input;
let unit = parsed.value;
if let Some(prev_unit) = prev_unit {
if prev_unit <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = prev_unit.singular(),
));
}
}
prev_unit = Some(unit);
parsed_any = true;
if let Some(fraction) = fraction {
sdur = fractional_time_to_duration(
unit, value, fraction, sdur, negative,
)?;
// Once we see a fraction, we are done. We don't permit parsing
// any more units. That is, a fraction can only occur on the
// lowest unit of time.
break;
} else {
sdur = set_duration_unit_value(unit, value, sdur, negative)?;
}
}
if !parsed_any {
return Err(err!(
"expected at least one unit of time (hours, minutes or \
seconds) in ISO 8601 duration when parsing into a \
`SignedDuration`",
));
}
Ok(Parsed { value: sdur, input })
Ok(Parsed { value: (), input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
@ -1486,16 +1396,16 @@ impl SpanParser {
// NOTE: Like with other things with signs, we don't support the Unicode
// <MINUS> sign. Just ASCII.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_sign<'i>(&self, input: &'i [u8]) -> Parsed<'i, t::Sign> {
fn parse_sign<'i>(&self, input: &'i [u8]) -> Parsed<'i, Sign> {
let Some(sign) = input.get(0).copied() else {
return Parsed { value: t::Sign::N::<1>(), input };
return Parsed { value: Sign::Positive, input };
};
let sign = if sign == b'+' {
t::Sign::N::<1>()
Sign::Positive
} else if sign == b'-' {
t::Sign::N::<-1>()
Sign::Negative
} else {
return Parsed { value: t::Sign::N::<1>(), input };
return Parsed { value: Sign::Positive, input };
};
Parsed { value: sign, input: &input[1..] }
}
@ -1604,7 +1514,7 @@ mod tests {
insta::assert_snapshot!(
p(b"P0d"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into SignedDuration requires that the duration contain a time component and no components of days or greater",
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into a `SignedDuration` requires that the duration contain a time component and no components of days or greater",
);
insta::assert_snapshot!(
p(b"PT0d"),
@ -1612,7 +1522,7 @@ mod tests {
);
insta::assert_snapshot!(
p(b"P0dT1s"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into SignedDuration requires that the duration contain a time component and no components of days or greater",
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into a `SignedDuration` requires that the duration contain a time component and no components of days or greater",
);
insta::assert_snapshot!(
@ -1621,15 +1531,15 @@ mod tests {
);
insta::assert_snapshot!(
p(b"P"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into SignedDuration requires that the duration contain a time component and no components of days or greater",
@"failed to parse ISO 8601 duration string into `SignedDuration`: parsing ISO 8601 duration into a `SignedDuration` requires that the duration contain a time component and no components of days or greater",
);
insta::assert_snapshot!(
p(b"PT"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: expected at least one unit of time (hours, minutes or seconds) in ISO 8601 duration when parsing into a `SignedDuration`",
@r#"failed to parse ISO 8601 duration string into `SignedDuration`: found a time designator (T or t) in an ISO 8601 duration string in "PT", but did not find any time units"#,
);
insta::assert_snapshot!(
p(b"PTs"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: expected at least one unit of time (hours, minutes or seconds) in ISO 8601 duration when parsing into a `SignedDuration`",
@r#"failed to parse ISO 8601 duration string into `SignedDuration`: found a time designator (T or t) in an ISO 8601 duration string in "PTs", but did not find any time units"#,
);
insta::assert_snapshot!(
@ -1665,7 +1575,7 @@ mod tests {
);
insta::assert_snapshot!(
p(b"PT2562047788015215.6h"),
@"failed to parse ISO 8601 duration string into `SignedDuration`: accumulated `SignedDuration` of `2562047788015215h` overflowed when adding `36m` (from fractional hour units)",
@"failed to parse ISO 8601 duration string into `SignedDuration`: accumulated `SignedDuration` of `2562047788015215h` overflowed when adding 0.600000000 of unit hour",
);
}

View file

@ -1,7 +1,7 @@
use crate::{
error::{err, ErrorContext},
fmt::Parsed,
util::{escape, parse, t},
util::{c::Sign, escape, parse, t},
Error, SignedDuration, Span, Unit,
};
@ -328,6 +328,419 @@ impl Fractional {
}
}
/// A container for holding a partially parsed duration.
///
/// This is used for parsing into `Span`, `SignedDuration` and (hopefully
/// soon) `std::time::Duration`. It's _also_ used for both the ISO 8601
/// duration and "friendly" format.
///
/// This replaced a significant chunk of code that was bespoke to each
/// combination of duration type _and_ format.
///
/// The idea behind it is that we parse each duration component as an unsigned
/// 64-bit integer and keep track of the sign separately. This is a critical
/// aspect that was motivated by being able to roundtrip all legal values of
/// a 96-bit signed integer number of nanoseconds (i.e., `SignedDuration`).
/// In particular, if we used `i64` to represent each component, then it
/// makes it much more difficult to parse, e.g., `9223372036854775808
/// seconds ago`. Namely, `9223372036854775808` is not a valid `i64` but
/// `-9223372036854775808` is. Notably, the sign is indicated by a suffix,
/// so we don't know it's negative when parsing the integer itself. So we
/// represent all components as their unsigned absolute value and apply the
/// sign at the end.
///
/// This also centralizes a lot of thorny duration math and opens up the
/// opportunity for tighter optimization.
#[derive(Debug, Default)]
pub(crate) struct DurationUnits {
/// The parsed unit values in descending order. That is, nanoseconds are
/// at index 0 while years are at index 9.
values: [u64; 10],
/// Any fractional component parsed. The fraction is necessarily a fraction
/// of the minimum unit if present.
///
/// TODO: Make this use a `u32` since we want the caller to pass in
/// a value in the range `0..=999_999_999`.
fraction: Option<u32>,
/// The sign of the duration. This may be set at any time.
sign: Option<Sign>,
/// The smallest unit value that was explicitly set.
min: Option<Unit>,
/// The largest unit value that was explicitly set.
max: Option<Unit>,
}
impl DurationUnits {
/// Set the duration component value for the given unit.
///
/// The value here is always unsigned. To deal with negative values, set
/// the sign independently. It will be accounted for when using one of this
/// type's methods for converting to a concrete duration type.
///
/// # Panics
///
/// When this is called after `set_fraction`.
///
/// # Errors
///
/// Since this is meant to be used in service of duration parsing and all
/// duration parsing proceeds from largest to smallest units, this will
/// return an error if the given unit is bigger than or equal to any
/// previously set unit.
pub(crate) fn set_unit_value(
&mut self,
unit: Unit,
value: u64,
) -> Result<(), Error> {
assert!(self.fraction.is_none());
if let Some(min) = self.min {
if min <= unit {
return Err(err!(
"found value {value:?} with unit {unit} \
after unit {prev_unit}, but units must be \
written from largest to smallest \
(and they can't be repeated)",
unit = unit.singular(),
prev_unit = min.singular(),
));
}
}
// Given the above check, the given unit must be smaller than any we
// have seen so far.
self.min = Some(unit);
// The maximum unit is always the first unit set, since we can never
// see a unit bigger than it without an error occurring.
if self.max.is_none() {
self.max = Some(unit);
}
self.values[unit.as_usize()] = value;
Ok(())
}
/// A convenience routine for setting values parsed from an `HH:MM:SS`
/// format (including the fraction).
///
/// # Errors
///
/// This forwards errors from `DurationUnits::set_unit_value`. It will also
/// return an error is the minimum parsed unit (so far) is smaller than
/// days. (Since `HH:MM:SS` can only appear after units of years, months,
/// weeks or days.)
pub(crate) fn set_hms(
&mut self,
hours: u64,
minutes: u64,
seconds: u64,
fraction: Option<u32>,
) -> Result<(), Error> {
if let Some(min) = self.min {
if min <= Unit::Hour {
return Err(err!(
"found `HH:MM:SS` after unit {min}, \
but `HH:MM:SS` can only appear after \
years, months, weeks or days",
min = min.singular(),
));
}
}
self.set_unit_value(Unit::Hour, hours)?;
self.set_unit_value(Unit::Minute, minutes)?;
self.set_unit_value(Unit::Second, seconds)?;
if let Some(fraction) = fraction {
self.set_fraction(fraction)?;
}
Ok(())
}
/// Set the fractional value.
///
/// This is always interpreted as a fraction of the minimal unit.
///
/// Callers must ensure this is called after the last call to
/// `DurationUnits::set_unit_value`.
///
/// # Panics
///
/// When `fraction` is not in the range `0..=999_999_999`. Callers are
/// expected to uphold this invariant.
///
/// # Errors
///
/// This will return an error if the minimum unit is `Unit::Nanosecond`.
/// (Because fractional nanoseconds are not supported.) This will also
/// return an error if the minimum unit is bigger than `Unit::Hour`.
pub(crate) fn set_fraction(&mut self, fraction: u32) -> Result<(), Error> {
assert!(fraction <= 999_999_999);
if self.min == Some(Unit::Nanosecond) {
return Err(err!("fractional nanoseconds are not supported"));
}
if let Some(min) = self.min {
if min > Unit::Hour {
return Err(err!(
"fractional {plural} are not supported",
plural = min.plural()
));
}
}
self.fraction = Some(fraction);
Ok(())
}
/// Set the sign associated with the components.
///
/// The sign applies to the entire duration. There is no support for
/// having some components signed and some unsigned.
///
/// If no sign is set, then it is assumed to be positive.
pub(crate) fn set_sign(&mut self, sign: Sign) {
self.sign = Some(sign);
}
/// Convert these duration components to a `Span`.
///
/// # Errors
///
/// If any individual unit exceeds the limits of a `Span`, or if the units
/// combine to exceed what can be represented by a `Span`, then this
/// returns an error.
///
/// This also returns an error if no units were set.
pub(crate) fn to_span(&self) -> Result<Span, Error> {
let (min, _) = self.get_min_max_units()?;
let mut span = Span::new();
if self.values[Unit::Year.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Year)?;
span = set_span_value(Unit::Year, value, span)?;
}
if self.values[Unit::Month.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Month)?;
span = set_span_value(Unit::Month, value, span)?;
}
if self.values[Unit::Week.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Week)?;
span = set_span_value(Unit::Week, value, span)?;
}
if self.values[Unit::Day.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Day)?;
span = set_span_value(Unit::Day, value, span)?;
}
if self.values[Unit::Hour.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Hour)?;
span = set_span_value_fallback(Unit::Hour, value, span)?;
}
if self.values[Unit::Minute.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Minute)?;
span = set_span_value_fallback(Unit::Minute, value, span)?;
}
if self.values[Unit::Second.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Second)?;
span = set_span_value_fallback(Unit::Second, value, span)?;
}
if self.values[Unit::Millisecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Millisecond)?;
span = set_span_value_fallback(Unit::Millisecond, value, span)?;
}
if self.values[Unit::Microsecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Microsecond)?;
span = set_span_value_fallback(Unit::Microsecond, value, span)?;
}
if self.values[Unit::Nanosecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Nanosecond)?;
span = set_span_value_fallback(Unit::Nanosecond, value, span)?;
}
if let Some(fraction) = self.get_fraction()? {
let value = self.get_unit_value(min)?;
span = fractional_time_to_span(min, value, fraction, span)?;
}
Ok(span)
}
/// Convert these duration components to a `SignedDuration`.
///
/// # Errors
///
/// If the total number of nanoseconds represented by all units combined
/// exceeds what can bit in a 96-bit signed integer, then an error is
/// returned.
///
/// An error is also returned if any calendar units (days or greater) were
/// set or if no units were set.
pub(crate) fn to_duration(&self) -> Result<SignedDuration, Error> {
let (min, max) = self.get_min_max_units()?;
if max > Unit::Hour {
return Err(err!(
"parsing {unit} units into a `SignedDuration` is not supported \
(perhaps try parsing into a `Span` instead)",
unit = max.singular(),
));
}
let mut sdur = SignedDuration::ZERO;
if self.values[Unit::Hour.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Hour)?;
sdur = SignedDuration::try_from_hours(value)
.and_then(|nanos| sdur.checked_add(nanos))
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Hour.singular(),
)
})?;
}
if self.values[Unit::Minute.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Minute)?;
sdur = SignedDuration::try_from_mins(value)
.and_then(|nanos| sdur.checked_add(nanos))
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Minute.singular(),
)
})?;
}
if self.values[Unit::Second.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Second)?;
sdur = SignedDuration::from_secs(value)
.checked_add(sdur)
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Second.singular(),
)
})?;
}
if self.values[Unit::Millisecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Millisecond)?;
sdur = SignedDuration::from_millis(value)
.checked_add(sdur)
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Millisecond.singular(),
)
})?;
}
if self.values[Unit::Microsecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Microsecond)?;
sdur = SignedDuration::from_micros(value)
.checked_add(sdur)
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Microsecond.singular(),
)
})?;
}
if self.values[Unit::Nanosecond.as_usize()] != 0 {
let value = self.get_unit_value(Unit::Nanosecond)?;
sdur = SignedDuration::from_nanos(value)
.checked_add(sdur)
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding {value} of unit {unit}",
unit = Unit::Nanosecond.singular(),
)
})?;
}
if let Some(fraction) = self.get_fraction()? {
sdur = sdur
.checked_add(fractional_duration(min, fraction)?)
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` \
overflowed when adding 0.{fraction} of unit {unit}",
unit = min.singular(),
)
})?;
}
Ok(sdur)
}
/// Returns the minimum unit set.
///
/// This only returns `None` when no units have been set.
pub(crate) fn get_min(&self) -> Option<Unit> {
self.min
}
/// Returns the minimum and maximum units set.
///
/// This returns an error if no units were set. (Since this means there
/// were no parsed duration components.)
fn get_min_max_units(&self) -> Result<(Unit, Unit), Error> {
let (Some(min), Some(max)) = (self.min, self.max) else {
return Err(err!("no parsed duration components"));
};
Ok((min, max))
}
/// Returns the corresponding unit value using the set signed-ness.
fn get_unit_value(&self, unit: Unit) -> Result<i64, Error> {
const I64_MIN_ABS: u64 = i64::MIN.unsigned_abs();
let sign = self.get_sign();
let value = self.values[unit.as_usize()];
// As a weird special case, when we need to represent i64::MIN,
// we'll have a unit value of `|i64::MIN|` as a `u64`. We can't
// convert that to a positive `i64` first, since it will overflow.
if sign.is_negative() && value == I64_MIN_ABS {
return Ok(i64::MIN);
}
// Otherwise, if a conversion to `i64` fails, then that failure
// is correct.
let mut value = i64::try_from(value).map_err(|_| {
err!(
"`{sign}{value}` {unit} is too big (or small) \
to fit into a signed 64-bit integer",
unit = unit.plural()
)
})?;
if sign.is_negative() {
value = value.checked_neg().ok_or_else(|| {
err!(
"`{sign}{value}` {unit} is too big (or small) \
to fit into a signed 64-bit integer",
unit = unit.plural()
)
})?;
}
Ok(value)
}
/// Returns the fraction using the set signed-ness.
///
/// This returns `None` when no fraction has been set.
fn get_fraction(&self) -> Result<Option<i32>, Error> {
let Some(fraction) = self.fraction else {
return Ok(None);
};
// OK because `set_fraction` guarantees `0..=999_999_999`.
let mut fraction = fraction as i32;
if self.get_sign().is_negative() {
// OK because `set_fraction` guarantees `0..=999_999_999`.
fraction = -fraction;
}
Ok(Some(fraction))
}
/// Returns the sign that should be applied to each individual unit.
fn get_sign(&self) -> Sign {
self.sign.unwrap_or(Sign::Positive)
}
}
/// Parses an optional fractional number from the start of `input`.
///
/// If `input` does not begin with a `.` (or a `,`), then this returns `None`
@ -346,7 +759,7 @@ impl Fractional {
#[cfg_attr(feature = "perf-inline", inline(always))]
pub(crate) fn parse_temporal_fraction<'i>(
input: &'i [u8],
) -> Result<Parsed<'i, Option<i32>>, Error> {
) -> Result<Parsed<'i, Option<u32>>, Error> {
// TimeFraction :::
// TemporalDecimalFraction
//
@ -377,7 +790,7 @@ pub(crate) fn parse_temporal_fraction<'i>(
// 0 1 2 3 4 5 6 7 8 9
#[inline(never)]
fn imp<'i>(mut input: &'i [u8]) -> Result<Parsed<'i, Option<i32>>, Error> {
fn imp<'i>(mut input: &'i [u8]) -> Result<Parsed<'i, Option<u32>>, Error> {
let mkdigits = parse::slicer(input);
while mkdigits(input).len() <= 8
&& input.first().map_or(false, u8::is_ascii_digit)
@ -394,16 +807,17 @@ pub(crate) fn parse_temporal_fraction<'i>(
// I believe this error can never happen, since we know we have no more
// than 9 ASCII digits. Any sequence of 9 ASCII digits can be parsed
// into an `i64`.
let nanoseconds = parse::fraction(digits, 9).map_err(|err| {
let nanoseconds = parse::fraction(digits).map_err(|err| {
err!(
"failed to parse {digits:?} as fractional component \
(up to 9 digits, nanosecond precision): {err}",
digits = escape::Bytes(digits),
)
})?;
// OK because `999_999_999` is the maximum possible parsed value, which
// fits into an `i32`.
let nanoseconds = i32::try_from(nanoseconds).unwrap();
// OK because parsing is forcefully limited to 9 digits,
// which can never be greater than `999_999_99`,
// which is less than `u32::MAX`.
let nanoseconds = nanoseconds as u32;
Ok(Parsed { value: Some(nanoseconds), input })
}
@ -413,8 +827,8 @@ pub(crate) fn parse_temporal_fraction<'i>(
imp(&input[1..])
}
/// This routine returns a span based on the given with fractional time applied
/// to it.
/// This routine returns a span based on the given unit and value with
/// fractional time applied to it.
///
/// For example, given a span like `P1dT1.5h`, the `unit` would be
/// `Unit::Hour`, the `value` would be `1` and the `fraction` would be
@ -432,7 +846,7 @@ pub(crate) fn parse_temporal_fraction<'i>(
/// a `span`. This also errors if `unit` is not `Hour`, `Minute`, `Second`,
/// `Millisecond` or `Microsecond`.
#[inline(never)]
pub(crate) fn fractional_time_to_span(
fn fractional_time_to_span(
unit: Unit,
value: i64,
fraction: i32,
@ -445,6 +859,13 @@ pub(crate) fn fractional_time_to_span(
t::SpanMilliseconds::MAX_SELF.get_unchecked() as i128;
const MAX_MICROS: i128 =
t::SpanMicroseconds::MAX_SELF.get_unchecked() as i128;
const MIN_HOURS: i64 = t::SpanHours::MIN_SELF.get_unchecked() as i64;
const MIN_MINS: i64 = t::SpanMinutes::MIN_SELF.get_unchecked() as i64;
const MIN_SECS: i64 = t::SpanSeconds::MIN_SELF.get_unchecked() as i64;
const MIN_MILLIS: i128 =
t::SpanMilliseconds::MIN_SELF.get_unchecked() as i128;
const MIN_MICROS: i128 =
t::SpanMicroseconds::MIN_SELF.get_unchecked() as i128;
// We switch everything over to nanoseconds and then divy that up as
// appropriate. In general, we always create a balanced span, but there
@ -462,13 +883,7 @@ pub(crate) fn fractional_time_to_span(
// out anything over the limit and carry it over to the lesser units. If
// our value is truly too big, then the final call to set nanoseconds will
// fail.
let mut sdur = fractional_time_to_duration(
unit,
value,
fraction,
SignedDuration::ZERO,
false,
)?;
let mut sdur = fractional_time_to_duration(unit, value, fraction)?;
if unit >= Unit::Hour && !sdur.is_zero() {
let (mut hours, rem) = sdur.as_hours_with_remainder();
@ -476,6 +891,9 @@ pub(crate) fn fractional_time_to_span(
if hours > MAX_HOURS {
sdur += SignedDuration::from_hours(hours - MAX_HOURS);
hours = MAX_HOURS;
} else if hours < MIN_HOURS {
sdur += SignedDuration::from_hours(hours - MIN_HOURS);
hours = MIN_HOURS;
}
// OK because we just checked that our units are in range.
span = span.hours(hours);
@ -486,6 +904,9 @@ pub(crate) fn fractional_time_to_span(
if mins > MAX_MINS {
sdur += SignedDuration::from_mins(mins - MAX_MINS);
mins = MAX_MINS;
} else if mins < MIN_MINS {
sdur += SignedDuration::from_mins(mins - MIN_MINS);
mins = MIN_MINS;
}
// OK because we just checked that our units are in range.
span = span.minutes(mins);
@ -496,6 +917,9 @@ pub(crate) fn fractional_time_to_span(
if secs > MAX_SECS {
sdur += SignedDuration::from_secs(secs - MAX_SECS);
secs = MAX_SECS;
} else if secs < MIN_SECS {
sdur += SignedDuration::from_secs(secs - MIN_SECS);
secs = MIN_SECS;
}
// OK because we just checked that our units are in range.
span = span.seconds(secs);
@ -506,6 +930,9 @@ pub(crate) fn fractional_time_to_span(
if millis > MAX_MILLIS {
sdur += SignedDuration::from_millis_i128(millis - MAX_MILLIS);
millis = MAX_MILLIS;
} else if millis < MIN_MILLIS {
sdur += SignedDuration::from_millis_i128(millis - MIN_MILLIS);
millis = MIN_MILLIS;
}
// OK because we just checked that our units are in range.
span = span.milliseconds(i64::try_from(millis).unwrap());
@ -516,6 +943,9 @@ pub(crate) fn fractional_time_to_span(
if micros > MAX_MICROS {
sdur += SignedDuration::from_micros_i128(micros - MAX_MICROS);
micros = MAX_MICROS;
} else if micros < MIN_MICROS {
sdur += SignedDuration::from_micros_i128(micros - MIN_MICROS);
micros = MIN_MICROS;
}
// OK because we just checked that our units are in range.
span = span.microseconds(i64::try_from(micros).unwrap());
@ -541,22 +971,28 @@ pub(crate) fn fractional_time_to_span(
/// Set the given unit to the given value on the given span.
///
/// When the given unit is hours or smaller, then if the value exceeds the
/// limits for that unit on `Span`, then an unbalanced span may be created to
/// handle the overage (if it can fit into smaller units).
///
/// # Errors
///
/// If the value is outside the legal boundaries for the given unit, then an
/// error is returned.
#[cfg_attr(feature = "perf-inline", inline(always))]
pub(crate) fn set_span_unit_value(
fn set_span_value_fallback(
unit: Unit,
value: i64,
span: Span,
) -> Result<Span, Error> {
let result = span.try_units(unit, value).with_context(|| {
err!(
"failed to set value {value:?} \
as {unit} unit on span",
unit = unit.singular(),
)
});
result.or_else(|err| {
#[cold]
#[inline(never)]
fn fractional_fallback(
err: Error,
unit: Unit,
value: i64,
span: Span,
) -> Result<Span, Error> {
if unit > Unit::Hour {
Err(err)
} else {
@ -568,6 +1004,23 @@ pub(crate) fn set_span_unit_value(
// the limits.
fractional_time_to_span(unit, value, 0, span)
}
}
set_span_value(unit, value, span)
.or_else(|err| fractional_fallback(err, unit, value, span))
}
/// Set the given calendar unit to the given value on the given span.
///
/// If the value is outside the legal boundaries for the given unit, then an
/// error is returned.
#[cfg_attr(feature = "perf-inline", inline(always))]
fn set_span_value(unit: Unit, value: i64, span: Span) -> Result<Span, Error> {
span.try_units(unit, value).with_context(|| {
err!(
"failed to set value {value:?} as {unit} unit on span",
unit = unit.singular(),
)
})
}
@ -588,35 +1041,14 @@ pub(crate) fn set_span_unit_value(
/// This returns an error if `unit` is not `Hour`, `Minute`, `Second`,
/// `Millisecond` or `Microsecond`.
#[inline(never)]
pub(crate) fn fractional_time_to_duration(
fn fractional_time_to_duration(
unit: Unit,
value: i64,
fraction: i32,
sdur: SignedDuration,
negative: bool,
) -> Result<SignedDuration, Error> {
let fraction = i64::from(fraction);
let nanos = match unit {
Unit::Hour => fraction.wrapping_mul(t::SECONDS_PER_HOUR.value()),
Unit::Minute => fraction.wrapping_mul(t::SECONDS_PER_MINUTE.value()),
Unit::Second => fraction,
Unit::Millisecond => fraction.wrapping_div(t::NANOS_PER_MICRO.value()),
Unit::Microsecond => fraction.wrapping_div(t::NANOS_PER_MILLI.value()),
unit => {
return Err(err!(
"fractional {unit} units are not allowed",
unit = unit.singular(),
))
}
};
let sdur = set_duration_unit_value(unit, value, sdur, negative)?;
let fraction_dur = SignedDuration::from_nanos(nanos);
if negative {
sdur.checked_sub(fraction_dur)
} else {
sdur.checked_add(fraction_dur)
}
.ok_or_else(|| {
let sdur = duration_unit_value(unit, value)?;
let fraction_dur = fractional_duration(unit, fraction)?;
sdur.checked_add(fraction_dur).ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` overflowed \
when adding `{fraction_dur:?}` (from fractional {unit} units)",
@ -625,31 +1057,40 @@ pub(crate) fn fractional_time_to_duration(
})
}
/// Set the given unit to the given value on the given duration.
/// Converts the fraction of the given unit to a signed duration.
///
/// If the value is outside the legal boundaries for the given unit, then an
/// error is returned. Moreover, if adding `value` (in terms of `unit`) to
/// the given signed duration would overflow, then an error is returned.
#[cfg_attr(feature = "perf-inline", inline(always))]
pub(crate) fn set_duration_unit_value(
/// Since a signed duration doesn't keep track of individual units, there is
/// no loss of fidelity between it and ISO 8601 durations like there is for
/// `Span`. Thus, we can do something far less complicated.
///
/// # Panics
///
/// When `fraction` isn't in the range `-999_999_999..=999_999_999`.
///
/// # Errors
///
/// This returns an error if `unit` is not `Hour`, `Minute`, `Second`,
/// `Millisecond` or `Microsecond`.
#[inline(never)]
fn fractional_duration(
unit: Unit,
value: i64,
sdur: SignedDuration,
negative: bool,
fraction: i32,
) -> Result<SignedDuration, Error> {
let value_dur = duration_unit_value(unit, value)?;
if negative {
sdur.checked_sub(value_dur)
} else {
sdur.checked_add(value_dur)
}
.ok_or_else(|| {
err!(
"accumulated `SignedDuration` of `{sdur:?}` overflowed when \
adding {value} of unit {unit}",
unit = unit.singular(),
)
})
let fraction = i64::from(fraction);
let nanos = match unit {
Unit::Hour => fraction * t::SECONDS_PER_HOUR.value(),
Unit::Minute => fraction * t::SECONDS_PER_MINUTE.value(),
Unit::Second => fraction,
Unit::Millisecond => fraction / t::NANOS_PER_MICRO.value(),
Unit::Microsecond => fraction / t::NANOS_PER_MILLI.value(),
unit => {
return Err(err!(
"fractional {unit} units are not allowed",
unit = unit.singular(),
))
}
};
Ok(SignedDuration::from_nanos(nanos))
}
/// Returns the given parsed value, interpreted as the given unit, as a

View file

@ -66,7 +66,7 @@ const MINS_PER_HOUR: i64 = 60;
/// assert_eq!(
/// "P1d".parse::<SignedDuration>().unwrap_err().to_string(),
/// "failed to parse ISO 8601 duration string into `SignedDuration`: \
/// parsing ISO 8601 duration into SignedDuration requires that the \
/// parsing ISO 8601 duration into a `SignedDuration` requires that the \
/// duration contain a time component and no components of days or \
/// greater",
/// );
@ -745,19 +745,15 @@ impl SignedDuration {
/// ```
#[inline]
pub const fn from_hours(hours: i64) -> SignedDuration {
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
const MIN_HOUR: i64 = i64::MIN / (SECS_PER_MINUTE * MINS_PER_HOUR);
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
const MAX_HOUR: i64 = i64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR);
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
if hours < MIN_HOUR {
panic!("hours overflowed minimum number of SignedDuration seconds")
match SignedDuration::try_from_hours(hours) {
Some(sdur) => sdur,
None => {
panic!(
"hours overflowed an `i64` number of seconds \
in `SignedDuration::from_hours`",
)
}
}
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
if hours > MAX_HOUR {
panic!("hours overflowed maximum number of SignedDuration seconds")
}
SignedDuration::from_secs(hours * MINS_PER_HOUR * SECS_PER_MINUTE)
}
/// Creates a new `SignedDuration` from the given number of minutes. Every
@ -782,24 +778,16 @@ impl SignedDuration {
/// assert_eq!(duration.subsec_nanos(), 0);
/// ```
#[inline]
pub const fn from_mins(minutes: i64) -> SignedDuration {
// OK because SECS_PER_MINUTE!={-1,0}.
const MIN_MINUTE: i64 = i64::MIN / SECS_PER_MINUTE;
// OK because SECS_PER_MINUTE!={-1,0}.
const MAX_MINUTE: i64 = i64::MAX / SECS_PER_MINUTE;
// OK because SECS_PER_MINUTE!={-1,0}.
if minutes < MIN_MINUTE {
panic!(
"minutes overflowed minimum number of SignedDuration seconds"
)
pub const fn from_mins(mins: i64) -> SignedDuration {
match SignedDuration::try_from_mins(mins) {
Some(sdur) => sdur,
None => {
panic!(
"minutes overflowed an `i64` number of seconds \
in `SignedDuration::from_mins`",
)
}
}
// OK because SECS_PER_MINUTE!={-1,0}.
if minutes > MAX_MINUTE {
panic!(
"minutes overflowed maximum number of SignedDuration seconds"
)
}
SignedDuration::from_secs(minutes * SECS_PER_MINUTE)
}
/// Converts the given timestamp into a signed duration.
@ -2211,6 +2199,44 @@ impl SignedDuration {
/// or a `Result`. For now, these return an `Option` so that they are `const`
/// and can aide code reuse. But I suspect these ought to be a `Result`.
impl SignedDuration {
/// Fallibly creates a new `SignedDuration` from a 64-bit integer number
/// of hours.
///
/// If the number of hours is less than [`SignedDuration::MIN`] or
/// more than [`SignedDuration::MAX`], then this returns `None`.
#[inline]
pub const fn try_from_hours(hours: i64) -> Option<SignedDuration> {
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
const MIN_HOUR: i64 = i64::MIN / (SECS_PER_MINUTE * MINS_PER_HOUR);
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
const MAX_HOUR: i64 = i64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR);
// OK because (SECS_PER_MINUTE*MINS_PER_HOUR)!={-1,0}.
if !(MIN_HOUR <= hours && hours <= MAX_HOUR) {
return None;
}
Some(SignedDuration::from_secs(
hours * MINS_PER_HOUR * SECS_PER_MINUTE,
))
}
/// Fallibly creates a new `SignedDuration` from a 64-bit integer number
/// of minutes.
///
/// If the number of minutes is less than [`SignedDuration::MIN`] or
/// more than [`SignedDuration::MAX`], then this returns `None`.
#[inline]
pub const fn try_from_mins(mins: i64) -> Option<SignedDuration> {
// OK because SECS_PER_MINUTE!={-1,0}.
const MIN_MINUTE: i64 = i64::MIN / SECS_PER_MINUTE;
// OK because SECS_PER_MINUTE!={-1,0}.
const MAX_MINUTE: i64 = i64::MAX / SECS_PER_MINUTE;
// OK because SECS_PER_MINUTE!={-1,0}.
if !(MIN_MINUTE <= mins && mins <= MAX_MINUTE) {
return None;
}
Some(SignedDuration::from_secs(mins * SECS_PER_MINUTE))
}
/// Fallibly creates a new `SignedDuration` from a 128-bit integer number
/// of milliseconds.
///

View file

@ -4172,6 +4172,24 @@ impl Unit {
}
}
/*
/// Returns the next smallest unit, if one exists.
pub(crate) fn prev(&self) -> Option<Unit> {
match *self {
Unit::Year => Some(Unit::Month),
Unit::Month => Some(Unit::Week),
Unit::Week => Some(Unit::Day),
Unit::Day => Some(Unit::Hour),
Unit::Hour => Some(Unit::Minute),
Unit::Minute => Some(Unit::Second),
Unit::Second => Some(Unit::Millisecond),
Unit::Millisecond => Some(Unit::Microsecond),
Unit::Microsecond => Some(Unit::Nanosecond),
Unit::Nanosecond => None,
}
}
*/
/// Returns the number of nanoseconds in this unit as a 128-bit integer.
///
/// # Panics
@ -4250,6 +4268,13 @@ impl Unit {
}
}
/// Return this unit as a `usize`.
///
/// This is use `unit as usize`.
pub(crate) fn as_usize(&self) -> usize {
*self as usize
}
/// The inverse of `unit as usize`.
fn from_usize(n: usize) -> Option<Unit> {
match n {

View file

@ -1310,8 +1310,8 @@ fn parse_span(span: &str) -> Result<Span, Error> {
but found {rest:?} instead"
));
}
let nanoseconds = parse::fraction(nanosecond_digits.as_bytes(), 9)
.map_err(|e| {
let nanoseconds =
parse::fraction(nanosecond_digits.as_bytes()).map_err(|e| {
e.context("failed to parse nanoseconds in time duration")
})?;
let nanoseconds_ranged = t::FractionalNanosecond::new(nanoseconds)

57
src/util/c.rs Normal file
View file

@ -0,0 +1,57 @@
/*!
A module for constants and various base utilities.
This module is a work-in-progress that may lead to helping us move off of
ranged integers. I'm not quite sure where this will go.
*/
use crate::util::t;
/// A representation of a numeric sign.
///
/// Its `Display` impl emits the ASCII minus sign, `-` when this
/// is negative. It emits the empty string in all other cases.
#[derive(
Clone, Copy, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord,
)]
#[repr(i8)]
pub(crate) enum Sign {
#[default]
Zero = 0,
Positive = 1,
Negative = -1,
}
impl Sign {
/*
pub(crate) fn is_zero(&self) -> bool {
matches!(*self, Sign::Zero)
}
pub(crate) fn is_positive(&self) -> bool {
matches!(*self, Sign::Positive)
}
*/
pub(crate) fn is_negative(&self) -> bool {
matches!(*self, Sign::Negative)
}
pub(crate) fn as_ranged_integer(&self) -> t::Sign {
match *self {
Sign::Zero => t::Sign::N::<0>(),
Sign::Positive => t::Sign::N::<1>(),
Sign::Negative => t::Sign::N::<-1>(),
}
}
}
impl core::fmt::Display for Sign {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self.is_negative() {
write!(f, "-")
} else {
Ok(())
}
}
}

View file

@ -1,5 +1,6 @@
pub(crate) mod array_str;
pub(crate) mod borrow;
pub(crate) mod c;
#[cfg(any(
feature = "tz-system",
feature = "tzdb-zoneinfo",

View file

@ -49,33 +49,28 @@ pub(crate) fn i64(bytes: &[u8]) -> Result<i64, Error> {
Ok(n)
}
/// Parses an `i64` fractional number from the beginning to the end of the
/// given slice of ASCII digit characters.
/// Parses a `u32` fractional number from the beginning to the end of the given
/// slice of ASCII digit characters.
///
/// The fraction's maximum precision must be provided. The returned integer
/// will always be in units of `10^{max_precision}`. For example, to parse a
/// fractional amount of seconds with a maximum precision of nanoseconds, then
/// use `max_precision=9`.
/// The fraction's maximum precision is always 9 digits. The returned integer
/// will always be in units of `10^{max_precision}`. For example, this
/// will parse a fractional amount of seconds with a maximum precision of
/// nanoseconds.
///
/// If any byte in the given slice is not `[0-9]`, then this returns an error.
/// Similarly, if the fraction parsed does not fit into a `i64`, then this
/// returns an error. Notably, this routine does not permit parsing a negative
/// integer. (We use `i64` because everything in this crate uses signed
/// integers, and because a higher level routine might want to parse the sign
/// and then apply it to the result of this routine.)
pub(crate) fn fraction(
bytes: &[u8],
max_precision: usize,
) -> Result<i64, Error> {
/// Notably, this routine does not permit parsing a negative integer.
pub(crate) fn fraction(bytes: &[u8]) -> Result<u32, Error> {
const MAX_PRECISION: usize = 9;
if bytes.is_empty() {
return Err(err!("invalid fraction, no digits found"));
} else if bytes.len() > max_precision {
} else if bytes.len() > MAX_PRECISION {
return Err(err!(
"invalid fraction, too many digits \
(at most {max_precision} are allowed"
(at most {MAX_PRECISION} are allowed"
));
}
let mut n: i64 = 0;
let mut n: u32 = 0;
for &byte in bytes {
let digit = match byte.checked_sub(b'0') {
None => {
@ -92,7 +87,7 @@ pub(crate) fn fraction(
}
Some(digit) => {
debug_assert!((0..=9).contains(&digit));
i64::from(digit)
u32::from(digit)
}
};
n = n.checked_mul(10).and_then(|n| n.checked_add(digit)).ok_or_else(
@ -104,7 +99,7 @@ pub(crate) fn fraction(
},
)?;
}
for _ in bytes.len()..max_precision {
for _ in bytes.len()..MAX_PRECISION {
n = n.checked_mul(10).ok_or_else(|| {
err!(
"fractional '{}' too big to parse into 64-bit integer \