From: Thomas Jarosch Date: Wed, 28 Jan 2009 14:37:34 +0000 (+0100) Subject: Fix some signed/unsigned issues in Week X-Git-Tag: v2.6~136^2~14^2~1 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=12963590a6f17baeec814de0165d520d72990f8f;p=libi2ncommon Fix some signed/unsigned issues in Week --- diff --git a/src/week.cpp b/src/week.cpp index 608459a..ed9d480 100644 --- a/src/week.cpp +++ b/src/week.cpp @@ -51,7 +51,7 @@ bool Week::is_valid() const */ void Week::clear() { - Days.reset(); + Days.reset(); //lint !e534 IsValid = true; } @@ -74,14 +74,14 @@ bool Week::set(const std::string& daystring) nr[0]=daystring[p]; nr[1]=0; istringstream c(nr); - int wnr=-1; - if (!(c >> wnr) || wnr<0 || wnr >6) + unsigned int wnr=7; + if (!(c >> wnr) || wnr > 6) { IsValid = false; return IsValid; } - new_days.set(wnr); + new_days.set(wnr); //lint !e534 } // Atomic switch-over Days = new_days; @@ -97,13 +97,14 @@ bool Week::set(const std::string& daystring) */ bool Week::set(const WeekDay day, bool value) { - if (day < 0 || day >= _WeekDay_END) + if (day >= _WeekDay_END) { IsValid = false; return IsValid; } - Days[day]=value; + Days.set(static_cast(day), value); //lint !e534 + return IsValid; } @@ -114,7 +115,7 @@ bool Week::set(const WeekDay day, bool value) */ bool Week::get(WeekDay day) const { - return Days[day]; + return Days.test(static_cast(day)); } /** @@ -124,7 +125,7 @@ bool Week::get(WeekDay day) const */ bool Week::is_set(WeekDay day) const { - return Days[day]; + return Days.test(static_cast(day)); } /** @@ -150,10 +151,10 @@ bool Week::none_set() const @param start weekday to start checking @note returns 0 if the start-day is set */ -int Week::days_till_set(WeekDay start) +unsigned int Week::days_till_set(WeekDay start) const { if (none_set()) - return -1; + return 0; for (unsigned int days=0; days < 8; days++) { @@ -167,7 +168,7 @@ int Week::days_till_set(WeekDay start) throw logic_error("can't find next weekday"); // fake - return -1; + return 0; //lint !e527 } /** @@ -175,14 +176,14 @@ int Week::days_till_set(WeekDay start) @param start weekday to start checking @note returns 0 if the start-day is set */ -int Week::days_since_set(WeekDay start) +unsigned int Week::days_since_set(WeekDay start) const { if (none_set()) - return -1; + return 0; for (unsigned int days=0; days < 8; days++) { - int check=start-days; + int check=start-static_cast(days); if (check < 0) check+=7; if (is_set(static_cast(check))) @@ -192,7 +193,7 @@ int Week::days_since_set(WeekDay start) throw logic_error("can't find next weekday"); // fake - return -1; + return 0; //lint !e527 } /** @@ -203,7 +204,7 @@ std::string Week::get_daystring() const { ostringstream out; - for (int i = 0; i < 7; i++) + for (unsigned int i = 0; i < 7; i++) if (Days[i]) out << i; @@ -219,7 +220,7 @@ std::string Week::get_displaystring() const string weekdays_str; // From Monday to Saturday - int j; + int j = 0; for (int i = 1; i < 7; i++) { if (Days[i]) @@ -252,7 +253,7 @@ std::string Week::get_displaystring() const } // special: sunday - if (Days[0] && j != 7) + if (Days[0] && j != 7) { if (!weekdays_str.empty()) weekdays_str += ", "; @@ -271,7 +272,7 @@ std::string Week::get_netfilterstring() const { string out; - for (int i = 0; i < 7; i++) + for (unsigned int i = 0; i < 7; i++) if (Days[i]) { if (!out.empty()) @@ -291,7 +292,8 @@ std::string Week::get_day_display(WeekDay day) { string weekday_str; - switch (day) { + switch (day) + { case Mo: weekday_str = i18n("Mon"); break; @@ -315,7 +317,7 @@ std::string Week::get_day_display(WeekDay day) break; default: break; - } + } //lint !e788: Don't complain about unused _WeekDay_END return weekday_str; } @@ -329,7 +331,8 @@ std::string Week::get_english_display(WeekDay day) { string weekday_str; - switch (day) { + switch (day) + { case Mo: weekday_str = "Mon"; break; @@ -353,7 +356,7 @@ std::string Week::get_english_display(WeekDay day) break; default: break; - } + } //lint !e788: Don't complain about unused _WeekDay_END return weekday_str; } diff --git a/src/week.hpp b/src/week.hpp index 9be554f..e50f7b8 100644 --- a/src/week.hpp +++ b/src/week.hpp @@ -48,8 +48,8 @@ class Week bool all_set() const; bool none_set() const; - int days_till_set(WeekDay day); - int days_since_set(WeekDay day); + unsigned int days_till_set(WeekDay day) const; + unsigned int days_since_set(WeekDay day) const; std::string get_daystring() const; std::string get_displaystring() const;