From 41d8b99be93d224b074e8958386a94223c0a9b88 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 29 Jun 2018 22:50:39 -0400 Subject: [PATCH] Fixes #6 Pretty ugly workaround, there's likely a safer alternative. --- src/lib.rs | 17 ++++++++++++++++- src/tests.rs | 11 ++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f8a52ea..f295758 100644 --- a/src/lib.rs +++ b/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) } diff --git a/src/tests.rs b/src/tests.rs index fc9a0f0..16e5bc0 100644 --- a/src/tests.rs +++ b/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); } \ No newline at end of file