From 93c70917afab49ca5fbb0c97fbd322428f47c72a Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Sat, 24 Oct 2015 09:21:18 +0300 Subject: [PATCH] Make all tests pass. --- .../opening_hours/osm_parsers_terminals.hpp | 2 +- 3party/opening_hours/osm_time_range.cpp | 74 ++++++++++++------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/3party/opening_hours/osm_parsers_terminals.hpp b/3party/opening_hours/osm_parsers_terminals.hpp index f264c57d2a..2835a4d47b 100644 --- a/3party/opening_hours/osm_parsers_terminals.hpp +++ b/3party/opening_hours/osm_parsers_terminals.hpp @@ -123,7 +123,7 @@ struct weeknum_ : qi::symbols } } weeknum; -struct daynum_ : qi::symbols +struct daynum_ : qi::symbols { daynum_() { diff --git a/3party/opening_hours/osm_time_range.cpp b/3party/opening_hours/osm_time_range.cpp index 6804b5c99d..6e9e992d9b 100644 --- a/3party/opening_hours/osm_time_range.cpp +++ b/3party/opening_hours/osm_time_range.cpp @@ -34,7 +34,7 @@ namespace { template -void print_vector(std::ostream & ost, std::vector const & v) +void PrintVector(std::ostream & ost, std::vector const & v) { auto it = begin(v); if (it == end(v)) @@ -46,7 +46,7 @@ void print_vector(std::ostream & ost, std::vector const & v) } } -void print_offset(std::ostream & ost, int32_t const offset) +void PrintOffset(std::ostream & ost, int32_t const offset) { if (offset == 0) return; @@ -59,6 +59,32 @@ void print_offset(std::ostream & ost, int32_t const offset) if (std::abs(offset) > 1) ost << 's'; } + +class StreamFlagsKeeper +{ + public: + StreamFlagsKeeper(std::ostream & ost): + m_ost(ost), + m_flags(m_ost.flags()) + { + } + + ~StreamFlagsKeeper() + { + m_ost.flags(m_flags); + } + + private: + std::ostream & m_ost; + std::ios_base::fmtflags m_flags; +}; + +void PrintPaddedNumber(std::ostream & ost, uint32_t const number, uint32_t const padding = 1) +{ + StreamFlagsKeeper keeper{ost}; + ost << std::setw(padding) << std::setfill('0') << number; +} + } // namespace namespace osmoh @@ -193,7 +219,6 @@ std::ostream & operator<<(std::ostream & ost, Time::EEvent const event) std::ostream & operator<<(std::ostream & ost, Time const & time) { - std::ios_base::fmtflags backupFlags = ost.flags(); if (!time.HasValue()) { ost << "hh:mm"; @@ -211,26 +236,22 @@ std::ostream & operator<<(std::ostream & ost, Time const & time) ost << '-'; else ost << '+'; - ost << std::setw(2) << std::setfill('0') - << std::abs(hours) - << ':' << std::setw(2) - << std::abs(minutes) << ')'; + PrintPaddedNumber(ost, std::abs(hours), 2); + ost << ':'; + PrintPaddedNumber(ost, std::abs(minutes), 2); + ost << ')'; } ost << time.GetEvent(); } if (time.IsMinutes()) - { - ost << std::setw(2) << std::setfill('0') - << std::abs(minutes); - } + PrintPaddedNumber(ost, std::abs(minutes), 2); else { - ost << std::setw(2) << std::setfill('0') - << std::abs(hours) - << ':' << std::setw(2) - << std::abs(minutes); + PrintPaddedNumber(ost, std::abs(hours), 2); + ost << ':'; + PrintPaddedNumber(ost, std::abs(minutes), 2); } - ost.flags(backupFlags); + return ost; } @@ -306,7 +327,7 @@ std::ostream & operator<<(std::ostream & ost, Timespan const & span) std::ostream & operator<<(std::ostream & ost, osmoh::TTimespans const & timespans) { - print_vector(ost, timespans); + PrintVector(ost, timespans); return ost; } @@ -481,16 +502,16 @@ std::ostream & operator<<(std::ostream & ost, WeekdayRange const & range) { if (range.HasNth()) { - print_vector(ost, range.GetNths()); + PrintVector(ost, range.GetNths()); } - print_offset(ost, range.GetOffset()); + PrintOffset(ost, range.GetOffset()); } return ost; } std::ostream & operator<<(std::ostream & ost, TWeekdayRanges const & ranges) { - print_vector(ost, ranges); + PrintVector(ost, ranges); return ost; } @@ -522,14 +543,14 @@ std::ostream & operator<<(std::ostream & ost, Holiday const & holiday) else { ost << "SH"; - print_offset(ost, holiday.GetOffset()); + PrintOffset(ost, holiday.GetOffset()); } return ost; } std::ostream & operator<<(std::ostream & ost, THolidays const & holidays) { - print_vector(ost, holidays); + PrintVector(ost, holidays); return ost; } @@ -634,7 +655,7 @@ std::ostream & operator<<(std::ostream & ost, DateOffset const & offset) if (offset.HasWDayOffset()) ost << (offset.IsWDayOffsetPositive() ? '+' : '-') << offset.GetWDayOffset(); - print_offset(ost, offset.GetOffset()); + PrintOffset(ost, offset.GetOffset()); return ost; } @@ -792,7 +813,10 @@ std::ostream & operator<<(std::ostream & ost, MonthDay const md) if (md.HasMonth()) ost << md.GetMonth(); if (md.HasDayNum()) - ost << ' ' << md.GetDayNum(); + { + ost << ' '; + PrintPaddedNumber(ost, md.GetDayNum(), 0); + } if (md.HasOffset()) ost << md.GetOffset(); } @@ -877,7 +901,7 @@ std::ostream & operator<<(std::ostream & ost, MonthdayRange const & range) std::ostream & operator<<(std::ostream & ost, TMonthdayRanges const & ranges) { - print_vector(ost, ranges); + PrintVector(ost, ranges); return ost; }