From f615db8a4cd75c4081faecba85ad963e9dc8eafd Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 26 Sep 2015 13:38:32 -0400 Subject: [PATCH] ISO8601d: Added ::length and tests --- src/ISO8601.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/ISO8601.h | 2 ++ test/iso8601d.t.cpp | 26 +++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/ISO8601.cpp b/src/ISO8601.cpp index 014d579ca..d6d16b203 100644 --- a/src/ISO8601.cpp +++ b/src/ISO8601.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #define DAY 86400 @@ -815,6 +816,43 @@ int ISO8601d::dayOfWeek (const std::string& input) return -1; } +//////////////////////////////////////////////////////////////////////////////// +int ISO8601d::length (const std::string& format) +{ + int len = 0; + for (auto& i : format) + { + switch (i) + { + case 'm': + case 'M': + case 'd': + case 'D': + case 'y': + case 'v': + case 'V': + case 'h': + case 'H': + case 'n': + case 'N': + case 's': + case 'S': len += 2; break; + case 'b': + case 'j': + case 'J': + case 'a': len += 3; break; + case 'Y': len += 4; break; + case 'A': + case 'B': len += 10; break; + + // Calculate the width, don't assume a single character width. + default: len += mk_wcwidth (i); break; + } + } + + return len; +} + //////////////////////////////////////////////////////////////////////////////// void ISO8601p::clear () { diff --git a/src/ISO8601.h b/src/ISO8601.h index b6df2aed4..0038b2552 100644 --- a/src/ISO8601.h +++ b/src/ISO8601.h @@ -46,6 +46,8 @@ public: static int dayOfWeek (const std::string&); + static int length (const std::string&); + private: void clear (); bool parse_date_time (Nibbler&); diff --git a/test/iso8601d.t.cpp b/test/iso8601d.t.cpp index 8baab02a5..dbd1f2712 100644 --- a/test/iso8601d.t.cpp +++ b/test/iso8601d.t.cpp @@ -71,7 +71,7 @@ void testParse ( //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (767); + UnitTest t (788); ISO8601d iso; std::string::size_type start = 0; @@ -227,6 +227,30 @@ int main (int argc, char** argv) t.is (ISO8601d::dayOfWeek ("Thursday"), 4, "Thursday == 4"); t.is (ISO8601d::dayOfWeek ("Friday"), 5, "Friday == 5"); t.is (ISO8601d::dayOfWeek ("Saturday"), 6, "Saturday == 6"); + + // int ISO8601d::length (const std::string&); + t.is (ISO8601d::length ("m"), 2, "length 'm' --> 2"); + t.is (ISO8601d::length ("M"), 2, "length 'M' --> 2"); + t.is (ISO8601d::length ("d"), 2, "length 'd' --> 2"); + t.is (ISO8601d::length ("D"), 2, "length 'D' --> 2"); + t.is (ISO8601d::length ("y"), 2, "length 'y' --> 2"); + t.is (ISO8601d::length ("Y"), 4, "length 'Y' --> 4"); + t.is (ISO8601d::length ("a"), 3, "length 'a' --> 3"); + t.is (ISO8601d::length ("A"), 10, "length 'A' --> 10"); + t.is (ISO8601d::length ("b"), 3, "length 'b' --> 3"); + t.is (ISO8601d::length ("B"), 10, "length 'B' --> 10"); + t.is (ISO8601d::length ("v"), 2, "length 'v' --> 2"); + t.is (ISO8601d::length ("V"), 2, "length 'V' --> 2"); + t.is (ISO8601d::length ("h"), 2, "length 'h' --> 2"); + t.is (ISO8601d::length ("H"), 2, "length 'H' --> 2"); + t.is (ISO8601d::length ("n"), 2, "length 'n' --> 2"); + t.is (ISO8601d::length ("N"), 2, "length 'N' --> 2"); + t.is (ISO8601d::length ("s"), 2, "length 's' --> 2"); + t.is (ISO8601d::length ("S"), 2, "length 'S' --> 2"); + t.is (ISO8601d::length ("j"), 3, "length 'j' --> 3"); + t.is (ISO8601d::length ("J"), 3, "length 'J' --> 3"); + + t.is (ISO8601d::length (" "), 1, "length ' ' --> 1"); } catch (const std::string& e)