-
Notifications
You must be signed in to change notification settings - Fork 546
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing preparations #1479
Open
pitdicker
wants to merge
6
commits into
chronotope:main
Choose a base branch
from
pitdicker:parsing_preps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Parsing preparations #1479
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3af5d5b
Use `format::parse` in `DateTime<FixedOffset>::from_str`
pitdicker 94fcec0
Move `FromStr` impl to `datetime` module
pitdicker 1dc14e0
Use `format::parse` in `FixedOffset::from_str`
pitdicker c2d3345
Simplify digit parsing in `timezone_offset`
pitdicker a799ecf
Support parsing negative timestamps
pitdicker 3b7fa22
Remove duplicate checks
pitdicker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,28 @@ | |
/// 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)> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
|
@@ -25,7 +47,7 @@ | |
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() { | ||
|
@@ -36,7 +58,7 @@ | |
} | ||
} | ||
|
||
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), | ||
}; | ||
|
@@ -181,7 +203,7 @@ | |
} | ||
|
||
/// Consumes any number (including zero) of colon or spaces. | ||
pub(crate) fn colon_or_space(s: &str) -> ParseResult<&str> { | ||
pub(super) fn colon_or_space(s: &str) -> ParseResult<&str> { | ||
Ok(s.trim_start_matches(|c: char| c == ':' || c.is_whitespace())) | ||
} | ||
|
||
|
@@ -199,7 +221,7 @@ | |
/// This is part of [RFC 3339 & ISO 8601]. | ||
/// | ||
/// [RFC 3339 & ISO 8601]: https://en.wikipedia.org/w/index.php?title=ISO_8601&oldid=1114309368#Time_offsets_from_UTC | ||
pub(crate) fn timezone_offset<F>( | ||
pub(super) fn timezone_offset<F>( | ||
mut s: &str, | ||
mut consume_colon: F, | ||
allow_zulu: bool, | ||
|
@@ -215,67 +237,51 @@ | |
} | ||
} | ||
|
||
const fn digits(s: &str) -> ParseResult<(u8, u8)> { | ||
fn digits(s: &str) -> ParseResult<u8> { | ||
let b = s.as_bytes(); | ||
if b.len() < 2 { | ||
Err(TOO_SHORT) | ||
} else { | ||
Ok((b[0], b[1])) | ||
return Err(TOO_SHORT); | ||
} | ||
match (b[0], b[1]) { | ||
(h1 @ b'0'..=b'9', h2 @ b'0'..=b'9') => Ok((h1 - b'0') * 10 + (h2 - b'0')), | ||
_ => Err(INVALID), | ||
} | ||
} | ||
let negative = match s.chars().next() { | ||
Some('+') => { | ||
// PLUS SIGN (U+2B) | ||
s = &s['+'.len_utf8()..]; | ||
|
||
false | ||
} | ||
Some('-') => { | ||
// HYPHEN-MINUS (U+2D) | ||
s = &s['-'.len_utf8()..]; | ||
|
||
true | ||
} | ||
Some('−') => { | ||
Some('−') if allow_tz_minus_sign => { | ||
// MINUS SIGN (U+2212) | ||
if !allow_tz_minus_sign { | ||
return Err(INVALID); | ||
} | ||
s = &s['−'.len_utf8()..]; | ||
|
||
true | ||
} | ||
Some(_) => return Err(INVALID), | ||
None => return Err(TOO_SHORT), | ||
}; | ||
|
||
// hours (00--99) | ||
let hours = match digits(s)? { | ||
(h1 @ b'0'..=b'9', h2 @ b'0'..=b'9') => i32::from((h1 - b'0') * 10 + (h2 - b'0')), | ||
_ => return Err(INVALID), | ||
}; | ||
let hours = digits(s)? as i32; | ||
s = &s[2..]; | ||
|
||
// colons (and possibly other separators) | ||
s = consume_colon(s)?; | ||
|
||
// minutes (00--59) | ||
// if the next two items are digits then we have to add minutes | ||
let minutes = if let Ok(ds) = digits(s) { | ||
match ds { | ||
(m1 @ b'0'..=b'5', m2 @ b'0'..=b'9') => i32::from((m1 - b'0') * 10 + (m2 - b'0')), | ||
(b'6'..=b'9', b'0'..=b'9') => return Err(OUT_OF_RANGE), | ||
_ => return Err(INVALID), | ||
let minutes = match digits(s) { | ||
Ok(m) if m >= 60 => return Err(OUT_OF_RANGE), | ||
Ok(m) => { | ||
s = &s[2..]; | ||
m as i32 | ||
} | ||
} else if allow_missing_minutes { | ||
0 | ||
} else { | ||
return Err(TOO_SHORT); | ||
}; | ||
s = match s.len() { | ||
len if len >= 2 => &s[2..], | ||
0 => s, | ||
_ => return Err(TOO_SHORT), | ||
Err(_) if allow_missing_minutes => 0, | ||
Err(e) => return Err(e), | ||
}; | ||
|
||
let seconds = hours * 3600 + minutes * 60; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain in the commit message why they're duplicate (that is, how these invariants are upheld)? It's not clear in the diff.