Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing preparations #1479

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ where
Minute => (2, false, Parsed::set_minute),
Second => (2, false, Parsed::set_second),
Nanosecond => (9, false, Parsed::set_nanosecond),
Timestamp => (usize::MAX, false, Parsed::set_timestamp),
Timestamp => (usize::MAX, true, Parsed::set_timestamp),

// for the future expansion
Internal(ref int) => match int._dummy {},
Expand All @@ -366,8 +366,7 @@ where
s = s.trim_start();
let v = if signed {
if s.starts_with('-') {
let v = try_consume!(scan::number(&s[1..], 1, usize::MAX));
0i64.checked_sub(v).ok_or(OUT_OF_RANGE)?
try_consume!(scan::negative_number(&s[1..], 1, usize::MAX))
} else if s.starts_with('+') {
try_consume!(scan::number(&s[1..], 1, usize::MAX))
} else {
Expand Down Expand Up @@ -765,6 +764,7 @@ mod tests {
check(" + 42", &[Space(" "), num(Year)], Err(INVALID));
check("-", &[num(Year)], Err(TOO_SHORT));
check("+", &[num(Year)], Err(TOO_SHORT));
check("-9223372036854775808", &[num(Timestamp)], parsed!(timestamp: i64::MIN));

// unsigned numeric
check("345", &[num(Ordinal)], parsed!(ordinal: 345));
Expand Down
26 changes: 24 additions & 2 deletions src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ use crate::Weekday;
/// Any number that does not fit in `i64` is an error.
#[inline]
pub(super) fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)> {
let (s, n) = unsigned_number(s, min, max)?;
Ok((s, n.try_into().map_err(|_| OUT_OF_RANGE)?))
}

/// Tries to parse the negative number from `min` to `max` digits.
///
/// The absence of digits at all is an unconditional error.
/// More than `max` digits are consumed up to the first `max` digits.
/// Any number that does not fit in `i64` is an error.
#[inline]
pub(super) fn negative_number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to inline this in the caller (more like before), since there aren't any other users, anyway.

Is there a way to more directly express the precondition for negating the number? This feels kind of roundabout -- even the previous 0i64.checked_sub(v).ok_or() feels clearer.

let (s, n) = unsigned_number(s, min, max)?;
let signed_neg = (n as i64).wrapping_neg();
if !signed_neg.is_negative() {
return Err(OUT_OF_RANGE);
}
Ok((s, signed_neg))
}

/// Tries to parse a number from `min` to `max` digits as an unsigned integer.
#[inline]
pub(super) fn unsigned_number(s: &str, min: usize, max: usize) -> ParseResult<(&str, u64)> {
assert!(min <= max);

// We are only interested in ascii numbers, so we can work with the `str` as bytes. We stop on
Expand All @@ -25,7 +47,7 @@ pub(super) fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)
return Err(TOO_SHORT);
}

let mut n = 0i64;
let mut n = 0u64;
for (i, c) in bytes.iter().take(max).cloned().enumerate() {
// cloned() = copied()
if !c.is_ascii_digit() {
Expand All @@ -36,7 +58,7 @@ pub(super) fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)
}
}

n = match n.checked_mul(10).and_then(|n| n.checked_add((c - b'0') as i64)) {
n = match n.checked_mul(10).and_then(|n| n.checked_add((c - b'0') as u64)) {
Some(n) => n,
None => return Err(OUT_OF_RANGE),
};
Expand Down