From 54fd7526e6a1d2d161fda10bccd609ec078b8fa9 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 5 Jun 2015 02:20:12 +0300 Subject: Add fast versions for function toString for different types. --- src/gui/windows/serverdialog.cpp | 8 ++--- src/utils/stringutils.cpp | 74 +++++++++++++++++++++++++++++++++++++++ src/utils/stringutils.h | 25 +++++++------ src/utils/stringutils_unittest.cc | 20 +++++++++++ 4 files changed, 113 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/gui/windows/serverdialog.cpp b/src/gui/windows/serverdialog.cpp index 6290724e9..684ff448a 100644 --- a/src/gui/windows/serverdialog.cpp +++ b/src/gui/windows/serverdialog.cpp @@ -686,10 +686,10 @@ void ServerDialog::saveCustomServers(const ServerInfo ¤tServer, ("MostUsedServerOnlineList" + num); const std::string persistentIpKey("persistentIp" + num); - config.setValue(nameKey, toString(server.name)); - config.setValue(descKey, toString(server.description)); - config.setValue(onlineListUrlKey, toString(server.onlineListUrl)); - config.setValue(hostKey, toString(server.hostname)); + config.setValue(nameKey, server.name); + config.setValue(descKey, server.description); + config.setValue(onlineListUrlKey, server.onlineListUrl); + config.setValue(hostKey, server.hostname); config.setValue(typeKey, serverTypeToString(server.type)); config.setValue(portKey, toString(server.port)); config.setValue(persistentIpKey, server.persistentIp); diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index b4e6e01b0..706135a56 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -709,6 +709,80 @@ std::string toStringPrint(const unsigned int val) return str; } +std::string toString(unsigned int num) +{ + char buf[30]; + buf[29] = '\0'; + size_t idx = 28; + do + buf[idx--] = static_cast((num % 10) + '0'); + while(num /= 10); + return buf + idx + 1; +} + +std::string toString(size_t num) +{ + char buf[100]; + buf[99] = '\0'; + size_t idx = 98; + do + buf[idx--] = static_cast((num % 10) + '0'); + while(num /= 10); + return buf + idx + 1; +} + +std::string toString(uint16_t num) +{ + char buf[10]; + buf[9] = '\0'; + size_t idx = 8; + do + buf[idx--] = static_cast((num % 10) + '0'); + while(num /= 10); + return buf + idx + 1; +} + +std::string toString(unsigned char num) +{ + char buf[5]; + buf[4] = '\0'; + size_t idx = 3; + do + buf[idx--] = static_cast((num % 10) + '0'); + while(num /= 10); + return buf + idx + 1; +} + +std::string toString(int num) +{ + char buf[30]; + bool useSign(false); + buf[29] = '\0'; + size_t idx = 28; + + if (num < 0) + { + useSign = true; + num = -num; + } + do + buf[idx--] = static_cast((num % 10) + '0'); + while(num /= 10); + if (useSign) + buf[idx--] = '-'; + return buf + idx + 1; +} + +std::string toString(float num) +{ + return strprintf("%f", num); +} + +std::string toString(double num) +{ + return strprintf("%f", static_cast(num)); +} + bool isDigit(const std::string &str) { if (str.empty()) diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index da75a42fa..cc479d80c 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -64,20 +64,25 @@ std::string &toUpper(std::string &str); */ unsigned int atox(const std::string &str) A_WARN_UNUSED; -template std::string toString(const T &arg) A_WARN_UNUSED; - /** - * Converts the given value to a string using std::stringstream. + * Converts the given value to a string. * - * @param arg the value to convert to a string + * @param num the value to convert to a string * @return the string representation of arg */ -template std::string toString(const T &arg) -{ - std::stringstream ss; - ss << arg; - return ss.str(); -} +std::string toString(unsigned int num); + +std::string toString(size_t num); + +std::string toString(unsigned char num); + +std::string toString(int num); + +std::string toString(uint16_t num); + +std::string toString(float num); + +std::string toString(double num); std::string toStringPrint(const unsigned int val); diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc index f984167a6..4dca829d7 100644 --- a/src/utils/stringutils_unittest.cc +++ b/src/utils/stringutils_unittest.cc @@ -117,6 +117,26 @@ TEST_CASE("stringuntils ipToString 1") REQUIRE("219.255.210.73" == std::string(ipToString(1238564827))); } +TEST_CASE("stringuntils toString 1") +{ + REQUIRE(strprintf("%d", 0) == toString(0)); + REQUIRE(strprintf("%d", -1) == toString(-1)); + REQUIRE(strprintf("%d", 30000000) == toString(30000000)); + REQUIRE(strprintf("%d", -10000000) == toString(-10000000)); + REQUIRE(strprintf("%d", 30000000) == toString( + static_cast(30000000))); + REQUIRE(strprintf("%d", 3000) == toString(static_cast(3000))); + REQUIRE(strprintf("%d", 123) == toString(static_cast(123))); +} + +TEST_CASE("stringuntils toString 2") +{ + for (int f = 0; f < 10000000; f += 123) + { + REQUIRE(strprintf("%d", f) == toString(f)); + } +} + TEST_CASE("stringuntils removeColors 1") { REQUIRE("" == removeColors("")); -- cgit v1.2.3-70-g09d2