From 741afa34517d6bc1155713bbc5d66905fea13fad Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 17 Sep 2018 22:19:30 -0400 Subject: [PATCH] Remove a last println and use a static default parser Heap profilers are fun --- build_pycompat.py | 5 +++-- build_pycompat_tokenizer.py | 1 + src/lib.rs | 8 ++++---- src/tests/fuzzing.rs | 2 +- src/tests/pycompat_parser.rs | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) mode change 100644 => 100755 build_pycompat.py mode change 100644 => 100755 build_pycompat_tokenizer.py diff --git a/build_pycompat.py b/build_pycompat.py old mode 100644 new mode 100755 index f95e104..7f26438 --- a/build_pycompat.py +++ b/build_pycompat.py @@ -1,3 +1,4 @@ +#!/usr/bin/python3 from dateutil.parser import parse from dateutil.tz import tzutc from datetime import datetime @@ -232,7 +233,7 @@ fn parse_and_assert( tzinfos: &HashMap, ) { - let mut parser = Parser::new(info); + let parser = Parser::new(info); let rs_parsed = parser.parse( s, dayfirst, @@ -282,7 +283,7 @@ fn parse_fuzzy_and_assert( tzinfos: &HashMap, ) { - let mut parser = Parser::new(info); + let parser = Parser::new(info); let rs_parsed = parser.parse( s, dayfirst, diff --git a/build_pycompat_tokenizer.py b/build_pycompat_tokenizer.py old mode 100644 new mode 100755 index 0832618..d18230e --- a/build_pycompat_tokenizer.py +++ b/build_pycompat_tokenizer.py @@ -1,3 +1,4 @@ +#!/usr/bin/python3 from dateutil.parser import _timelex from build_pycompat import tests diff --git a/src/lib.rs b/src/lib.rs index b6cfd31..2ebad81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,6 +108,7 @@ lazy_static! { static ref ONE: Decimal = Decimal::new(1, 0); static ref TWENTY_FOUR: Decimal = Decimal::new(24, 0); static ref SIXTY: Decimal = Decimal::new(60, 0); + static ref DEFAULT_PARSER: Parser = Parser::default(); } impl From for ParseError { @@ -670,7 +671,7 @@ impl Parser { /// order to be resolved. #[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))] // Need to release a 2.0 for changing public API pub fn parse( - &mut self, + &self, timestr: &str, dayfirst: Option, yearfirst: Option, @@ -699,7 +700,7 @@ impl Parser { #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] // Imitating Python API is priority fn parse_with_tokens( - &mut self, + &self, timestr: &str, dayfirst: Option, yearfirst: Option, @@ -953,7 +954,6 @@ impl Parser { ) -> ParseResult> { // TODO: Actual timezone support if let Some(offset) = res.tzoffset { - println!("offset={}", offset); Ok(Some(FixedOffset::east(offset))) } else if res.tzoffset == None && (res.tzname == Some(" ".to_owned()) || res.tzname == Some(".".to_owned()) @@ -1283,7 +1283,7 @@ fn ljust(s: &str, chars: usize, replace: char) -> String { /// The default implementation assumes English values for names of months, /// days of the week, etc. It is equivalent to Python's `dateutil.parser.parse()` pub fn parse(timestr: &str) -> ParseResult<(NaiveDateTime, Option)> { - let res = Parser::default().parse( + let res = DEFAULT_PARSER.parse( timestr, None, None, diff --git a/src/tests/fuzzing.rs b/src/tests/fuzzing.rs index dc6e15a..426b8b7 100644 --- a/src/tests/fuzzing.rs +++ b/src/tests/fuzzing.rs @@ -16,7 +16,7 @@ fn test_fuzz() { // OverflowError: Python int too large to convert to C long // assert_eq!(parse("8888884444444888444444444881"), Err(ParseError::AmPmWithoutHour)); let default = NaiveDate::from_ymd(2016, 6, 29).and_hms(0, 0, 0); - let mut p = Parser::default(); + let p = Parser::default(); let res = p.parse("\x0D\x31", None, None, false, false, Some(&default), false, &HashMap::new()).unwrap(); assert_eq!(res.0, default); diff --git a/src/tests/pycompat_parser.rs b/src/tests/pycompat_parser.rs index e9f3825..72885ab 100644 --- a/src/tests/pycompat_parser.rs +++ b/src/tests/pycompat_parser.rs @@ -39,7 +39,7 @@ fn parse_and_assert( tzinfos: &HashMap, ) { - let mut parser = Parser::new(info); + let parser = Parser::new(info); let rs_parsed = parser.parse( s, dayfirst, @@ -89,7 +89,7 @@ fn parse_fuzzy_and_assert( tzinfos: &HashMap, ) { - let mut parser = Parser::new(info); + let parser = Parser::new(info); let rs_parsed = parser.parse( s, dayfirst,