From 08fcb5362e26f19610cd185c1faf136e69687d03 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 17 Aug 2011 00:45:09 -0400 Subject: [PATCH] Bug - Durations - Duration were parsed as in A3, whereas is correct. - Added Nibbler::str method for internal access. - Nibbler::save, ::restore now return positions. Useful for extracting substrings. - Modified countdown.t to use the (now) correct units for durations. --- src/A3.cpp | 11 ++++++----- src/Duration.cpp | 20 ++++++++++++++++++-- src/Nibbler.cpp | 14 ++++++++++---- src/Nibbler.h | 6 +++--- test/countdown.t | 4 ++-- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/A3.cpp b/src/A3.cpp index 3d96efb42..7c3ec21c3 100644 --- a/src/A3.cpp +++ b/src/A3.cpp @@ -1471,14 +1471,15 @@ bool A3::is_dom (Nibbler& n, std::string& result) // This prevents the interpretation of '31st' as a duration ('31s'). bool A3::is_duration (Nibbler& n, std::string& result) { - n.save (); + std::string::size_type start = n.save (); - int quantity; + int i; + double d; std::string unit; - std::vector units = Duration::get_units (); - if (n.getInt (quantity) && + if ((n.getInt (i) || + n.getNumber (d)) && n.getOneOf (units, unit)) { char next = n.next (); @@ -1486,7 +1487,7 @@ bool A3::is_duration (Nibbler& n, std::string& result) next == ')' || next == ' ') { - result = format (quantity) + unit; + result = n.str ().substr (start, n.cursor () - start); return true; } } diff --git a/src/Duration.cpp b/src/Duration.cpp index ae6b40687..8ffa15b33 100644 --- a/src/Duration.cpp +++ b/src/Duration.cpp @@ -52,23 +52,27 @@ static const char* durations[] = "d", "fortnight", "hours", + "hour", "hrs", + "hr", "h", "minutes", "mins", "min", - "mnths", "monthly", "months", "month", + "mnths", + "mths", + "mth", "mos", "mo", - "mths", "m", "quarterly", "quarters", "qrtrs", "qtrs", + "qtr", "q", "seconds", "secs", @@ -79,11 +83,15 @@ static const char* durations[] = "weekdays", "weekly", "weeks", + "week", "wks", + "wk", "w", "yearly", "years", + "year", "yrs", + "yr", "y", "-", }; @@ -393,7 +401,9 @@ void Duration::parse (const std::string& input) else if (match == "yearly") mSecs = (int) (value * 86400 * 365); else if (match == "annual") mSecs = (int) (value * 86400 * 365); else if (match == "years") mSecs = (int) (value * 86400 * 365); + else if (match == "year") mSecs = (int) (value * 86400 * 365); else if (match == "yrs") mSecs = (int) (value * 86400 * 365); + else if (match == "yr") mSecs = (int) (value * 86400 * 365); else if (match == "y") mSecs = (int) (value * 86400 * 365); else if (match == "semiannual") mSecs = (int) (value * 86400 * 183); @@ -403,6 +413,7 @@ void Duration::parse (const std::string& input) else if (match == "quarters") mSecs = (int) (value * 86400 * 91); else if (match == "qrtrs") mSecs = (int) (value * 86400 * 91); else if (match == "qtrs") mSecs = (int) (value * 86400 * 91); + else if (match == "qtr") mSecs = (int) (value * 86400 * 91); else if (match == "q") mSecs = (int) (value * 86400 * 91); else if (match == "monthly") mSecs = (int) (value * 86400 * 30); @@ -412,6 +423,7 @@ void Duration::parse (const std::string& input) else if (match == "mos") mSecs = (int) (value * 86400 * 30); else if (match == "mo") mSecs = (int) (value * 86400 * 30); else if (match == "mths") mSecs = (int) (value * 86400 * 30); + else if (match == "mth") mSecs = (int) (value * 86400 * 30); else if (match == "m") mSecs = (int) (value * 86400 * 30); else if (match == "biweekly") mSecs = (int) (value * 86400 * 14); @@ -420,7 +432,9 @@ void Duration::parse (const std::string& input) else if (match == "weekly") mSecs = (int) (value * 86400 * 7); else if (match == "sennight") mSecs = (int) (value * 86400 * 7); else if (match == "weeks") mSecs = (int) (value * 86400 * 7); + else if (match == "week") mSecs = (int) (value * 86400 * 7); else if (match == "wks") mSecs = (int) (value * 86400 * 7); + else if (match == "wk") mSecs = (int) (value * 86400 * 7); else if (match == "w") mSecs = (int) (value * 86400 * 7); else if (match == "daily") mSecs = (int) (value * 86400 * 1); @@ -430,7 +444,9 @@ void Duration::parse (const std::string& input) else if (match == "d") mSecs = (int) (value * 86400 * 1); else if (match == "hours") mSecs = (int) (value * 3600); + else if (match == "hour") mSecs = (int) (value * 3600); else if (match == "hrs") mSecs = (int) (value * 3600); + else if (match == "hr") mSecs = (int) (value * 3600); else if (match == "h") mSecs = (int) (value * 3600); else if (match == "minutes") mSecs = (int) (value * 60); diff --git a/src/Nibbler.cpp b/src/Nibbler.cpp index fc8ab29ca..646ab0439 100644 --- a/src/Nibbler.cpp +++ b/src/Nibbler.cpp @@ -1116,15 +1116,21 @@ std::string Nibbler::next (const int quantity) } //////////////////////////////////////////////////////////////////////////////// -void Nibbler::save () +std::string::size_type Nibbler::save () { - mSaved = mCursor; + return mSaved = mCursor; } //////////////////////////////////////////////////////////////////////////////// -void Nibbler::restore () +std::string::size_type Nibbler::restore () { - mCursor = mSaved; + return mCursor = mSaved; +} + +//////////////////////////////////////////////////////////////////////////////// +const std::string& Nibbler::str () const +{ + return mInput; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Nibbler.h b/src/Nibbler.h index df336f1b7..c29de1487 100644 --- a/src/Nibbler.h +++ b/src/Nibbler.h @@ -80,9 +80,9 @@ public: std::string next (const int quantity); std::string::size_type cursor (); - - void save (); - void restore (); + std::string::size_type save (); + std::string::size_type restore (); + const std::string& str () const; bool depleted (); diff --git a/test/countdown.t b/test/countdown.t index 8866267a9..8cdf6933f 100755 --- a/test/countdown.t +++ b/test/countdown.t @@ -47,13 +47,13 @@ if (open my $fh, '>', 'countdown.rc') print $fh "report.down.sort=due-\n"; print $fh "report.upc.description=countdown_compact+ report\n"; - print $fh "report.upc.columns=id,countdown_compact,description\n"; + print $fh "report.upc.columns=id,due.countdown,description\n"; print $fh "report.upc.labels=ID,Countdown,Description\n"; print $fh "report.upc.filter=status:pending\n"; print $fh "report.upc.sort=due+\n"; print $fh "report.downc.description=countdown_compact- report\n"; - print $fh "report.downc.columns=id,countdown_compact,description\n"; + print $fh "report.downc.columns=id,due.countdown,description\n"; print $fh "report.downc.labels=ID,Countdown,Description\n"; print $fh "report.downc.filter=status:pending\n"; print $fh "report.downc.sort=due-\n";