From 7637fd76a27d8a97cd32d7597d5afb3361fe0d28 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 5 Mar 2010 14:38:49 +0000 Subject: [PATCH] 2010-03-05 Tatsuhiro Tsujikawa Fixed the bug that util::itos(INT64_MIN) fails. * src/util.cc * src/util.h * test/UtilTest.cc --- ChangeLog | 7 +++++++ src/util.cc | 23 +++++++++++++++++++++++ src/util.h | 15 +-------------- test/UtilTest.cc | 10 ++++++++-- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index acd489a3..8546595d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-03-05 Tatsuhiro Tsujikawa + + Fixed the bug that util::itos(INT64_MIN) fails. + * src/util.cc + * src/util.h + * test/UtilTest.cc + 2010-03-05 Tatsuhiro Tsujikawa Fixed memory leak in SocketBuffer when _bufq is not empty when diff --git a/src/util.cc b/src/util.cc index 354be86e..d69967c1 100644 --- a/src/util.cc +++ b/src/util.cc @@ -139,6 +139,29 @@ std::pair split(const std::string& src, const std::str return hp; } +std::string itos(int64_t value, bool comma) +{ + bool flag = false; + std::string str; + if(value < 0) { + if(value == INT64_MIN) { + if(comma) { + str = "-9,223,372,036,854,775,808"; + } else { + str = "-9223372036854775808"; + } + return str; + } + flag = true; + value = -value; + } + str = uitos(value, comma); + if(flag) { + str.insert(str.begin(), '-'); + } + return str; +} + int64_t difftv(struct timeval tv1, struct timeval tv2) { if((tv1.tv_sec < tv2.tv_sec) || ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) { diff --git a/src/util.h b/src/util.h index 4aefe6b0..e98a9103 100644 --- a/src/util.h +++ b/src/util.h @@ -113,20 +113,7 @@ std::string uitos(T value, bool comma = false) return str; } -template -std::string itos(T value, bool comma = false) -{ - bool flag = false; - if(value < 0) { - flag = true; - value = -value; - } - std::string str = uitos(value, comma); - if(flag) { - str.insert(str.begin(), '-'); - } - return str; -} +std::string itos(int64_t value, bool comma = false); /** * Computes difference in micro-seconds between tv1 and tv2, diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 91ab1355..3d807e83 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -775,8 +775,14 @@ void UtilTest::testItos() } { int64_t i = INT64_MAX; - CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), util::itos(i, true)); - } + CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), + util::itos(i, true)); + } + { + int64_t i = INT64_MIN; + CPPUNIT_ASSERT_EQUAL(std::string("-9,223,372,036,854,775,808"), + util::itos(i, true)); + } } void UtilTest::testUitos()