mirror of
https://github.com/bspeice/dtparse
synced 2024-11-13 17:38:09 -05:00
parent
73a7acad85
commit
41d8b99be9
17
src/lib.rs
17
src/lib.rs
@ -1218,6 +1218,21 @@ impl Parser {
|
||||
let len_l = tokens.len();
|
||||
let mut hms_idx = None;
|
||||
|
||||
// There's a super weird edge case that can happen
|
||||
// because Python safely handles negative array indices,
|
||||
// and Rust (because of usize) does not.
|
||||
let idx_minus_two = if idx == 1 && len_l > 0 {
|
||||
len_l - 1
|
||||
} else if idx == 0 && len_l > 1 {
|
||||
len_l - 2
|
||||
} else if idx > 1 {
|
||||
idx - 2
|
||||
} else if len_l == 0{
|
||||
panic!("Attempting to find_hms_index() wih no tokens.");
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if idx + 1 < len_l && info.get_hms(&tokens[idx + 1]).is_some() {
|
||||
hms_idx = Some(idx + 1)
|
||||
} else if allow_jump && idx + 2 < len_l && tokens[idx + 1] == " "
|
||||
@ -1227,7 +1242,7 @@ impl Parser {
|
||||
} else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() {
|
||||
hms_idx = Some(idx - 1)
|
||||
} else if len_l > 0 && idx > 0 && idx == len_l - 1 && tokens[idx - 1] == " "
|
||||
&& info.get_hms(&tokens[idx - 2]).is_some()
|
||||
&& info.get_hms(&tokens[idx_minus_two]).is_some()
|
||||
{
|
||||
hms_idx = Some(idx - 2)
|
||||
}
|
||||
|
11
src/tests.rs
11
src/tests.rs
@ -1,8 +1,17 @@
|
||||
use ParseError;
|
||||
use chrono::NaiveDate;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use parse;
|
||||
use ParseError;
|
||||
use Parser;
|
||||
|
||||
#[test]
|
||||
fn test_fuzz() {
|
||||
|
||||
assert_eq!(parse("\x2D\x38\x31\x39\x34\x38\x34"), Err(ParseError::InvalidMonth));
|
||||
|
||||
let default = NaiveDate::from_ymd(2016, 6, 29).and_hms(0, 0, 0);
|
||||
let mut p = Parser::default();
|
||||
let res = p.parse("\x0D\x31", None, None, false, false, Some(&default), false, HashMap::new()).unwrap();
|
||||
assert_eq!(res.0, default);
|
||||
}
|
Loading…
Reference in New Issue
Block a user