From 5170a842d61f927560a52d07b738b0aaaff61cd2 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 1 Jul 2014 17:22:20 -0400 Subject: [PATCH] Duration - Distinguishes between a 'standalone' duration like 'year', and one that requires a numeric leader, '2 weeks'. --- src/Duration.cpp | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Duration.cpp b/src/Duration.cpp index 02828b549..1c766f05f 100644 --- a/src/Duration.cpp +++ b/src/Duration.cpp @@ -290,35 +290,55 @@ bool Duration::parse (const std::string& input, std::string::size_type& start) std::string number; std::string unit; - if ((n.getNumber (number) && n.skipWS () && n.getOneOf (units, unit)) || - n.getOneOf (units, unit)) + + if (n.getOneOf (units, unit)) { if (n.depleted () || Lexer::is_ws (n.next ())) { - // TODO Determine which unit matched, and therefore whether the unit can - // exist as a standalone unit (with assumed quantity of 1), or - // whether the quantity is required. - start = original_start + n.cursor (); - double quantity = (number == "") - ? 1.0 - : strtod (number.c_str (), NULL); // Linear lookup - should be logarithmic. double seconds = 1; for (int i = 0; i < NUM_DURATIONS; i++) { - if (durations[i].unit == unit) + if (durations[i].unit == unit && + durations[i].standalone == true) { - seconds = durations[i].seconds; - _secs = static_cast (quantity * static_cast (seconds)); + _secs = static_cast (durations[i].seconds); return true; } } } } + else if (n.getNumber (number)) + { + n.skipWS (); + if (n.getOneOf (units, unit)) + { + if (n.depleted () || + Lexer::is_ws (n.next ())) + { + start = original_start + n.cursor (); + double quantity = strtod (number.c_str (), NULL); + + // Linear lookup - should be logarithmic. + double seconds = 1; + for (int i = 0; i < NUM_DURATIONS; i++) + { + if (durations[i].unit == unit && + durations[i].standalone == false) + { + seconds = durations[i].seconds; + _secs = static_cast (quantity * static_cast (seconds)); + return true; + } + } + } + } + } + return false; }