From 66364da233cd1d8b07ee633e791addbb69ee9dfd Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Sun, 1 May 2011 04:09:04 +0200 Subject: [PATCH] Made templated method utils::to_string --- base/base_tests/string_utils_test.cpp | 9 ++++++++- base/string_utils.cpp | 7 ------- base/string_utils.hpp | 8 +++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 2e65aa389f..254e6f1d59 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -42,5 +42,12 @@ UNIT_TEST(to_double) s = "-2"; TEST(utils::to_double(s, d), ()); TEST_ALMOST_EQUAL(-2.0, d, ()); - +} + +UNIT_TEST(to_string) +{ + TEST_EQUAL(utils::to_string(-1), "-1", ()); + TEST_EQUAL(utils::to_string(1234567890), "1234567890", ()); + TEST_EQUAL(utils::to_string(0.56), "0.56", ()); + TEST_EQUAL(utils::to_string(-100.2), "-100.2", ()); } diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 53043ae3a1..edd76d1d5e 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -73,13 +73,6 @@ bool to_double(char const * s, double & d) return false; } -string to_string(size_t i) -{ - ostringstream ss; - ss << i; - return ss.str(); -} - void make_lower_case(string & s) { if (!s.empty()) diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 3d21062c5b..ae02c64c67 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -58,7 +58,13 @@ namespace utils bool to_int64(char const * s, int64_t & i); bool to_double(char const * s, double & d); - string to_string(size_t i); + template + string to_string(T i) + { + ostringstream ss; + ss << i; + return ss.str(); + } inline bool to_int(string const & s, int & i) { return to_int(s.c_str(), i); } inline bool to_uint64(string const & s, uint64_t & i) { return to_uint64(s.c_str(), i); }