diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 16685b36cd..38b238c4fb 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -152,6 +152,97 @@ UNIT_TEST(to_double) s = "-2"; TEST(strings::to_double(s, d), ()); TEST_ALMOST_EQUAL(-2.0, d, ()); + + s = "labuda"; + TEST(!strings::to_double(s, d), ()); +} + +UNIT_TEST(to_float) +{ + string s; + float f; + + s = "0.123"; + TEST(strings::to_float(s, f), ()); + TEST_ALMOST_EQUAL(0.123f, f, ()); + + s = "1."; + TEST(strings::to_float(s, f), ()); + TEST_ALMOST_EQUAL(1.0f, f, ()); + + s = "0"; + TEST(strings::to_float(s, f), ()); + TEST_ALMOST_EQUAL(0.f, f, ()); + + s = "5.68434e-14"; + TEST(strings::to_float(s, f), ()); + TEST_ALMOST_EQUAL(5.68434e-14f, f, ()); + + s = "-179.654321"; + TEST(strings::to_float(s, f), ()); + TEST_ALMOST_EQUAL(-179.654321f, f, ()); + + s = "labuda"; + TEST(!strings::to_float(s, f), ()); +} + +UNIT_TEST(to_int) +{ + int i; + string s; + + s = "-2"; + TEST(strings::to_int(s, i), ()); + TEST_EQUAL(-2, i, ()); + + s = "0"; + TEST(strings::to_int(s, i), ()); + TEST_EQUAL(0, i, ()); + + s = "123456789"; + TEST(strings::to_int(s, i), ()); + TEST_EQUAL(123456789, i, ()); + + s = "labuda"; + TEST(!strings::to_int(s, i), ()); +} + +UNIT_TEST(to_uint64) +{ + uint64_t i; + string s; + + s = "0"; + TEST(strings::to_uint64(s, i), ()); + TEST_EQUAL(0, i, ()); + + s = "123456789101112"; + TEST(strings::to_uint64(s, i), ()); + TEST_EQUAL(123456789101112UL, i, ()); + + s = "labuda"; + TEST(!strings::to_uint64(s, i), ()); +} + +UNIT_TEST(to_int64) +{ + int64_t i; + string s; + + s = "-24567"; + TEST(strings::to_int64(s, i), ()); + TEST_EQUAL(-24567, i, ()); + + s = "0"; + TEST(strings::to_int64(s, i), ()); + TEST_EQUAL(0, i, ()); + + s = "12345678911212"; + TEST(strings::to_int64(s, i), ()); + TEST_EQUAL(12345678911212L, i, ()); + + s = "labuda"; + TEST(!strings::to_int64(s, i), ()); } UNIT_TEST(to_string) diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 43d9f73cb6..6dc718a939 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -1,6 +1,7 @@ #include "string_utils.hpp" #include "assert.hpp" +#include "../std/target_os.hpp" #include "../std/iterator.hpp" namespace strings @@ -34,10 +35,11 @@ UniChar LastUniChar(string const & s) bool to_int(char const * s, int & i) { char * stop; - int x = strtol(s, &stop, 10); + long const x = strtol(s, &stop, 10); if (stop && *stop == 0) { - i = x; + i = static_cast(x); + ASSERT_EQUAL(static_cast(i), x, ()); return true; } return false; @@ -45,30 +47,31 @@ bool to_int(char const * s, int & i) bool to_uint64(char const * s, uint64_t & i) { - istringstream ss; - ss.str(s); - ss >> i; - return !ss.fail(); + char * stop; +#ifdef OMIM_OS_WINDOWS + i = _strtoui64(s, &stop, 10); +#else + i = strtoull(s, &stop, 10); +#endif + return stop && *stop == 0; } bool to_int64(char const * s, int64_t & i) { - istringstream ss; - ss.str(s); - ss >> i; - return !ss.fail(); + char * stop; +#ifdef OMIM_OS_WINDOWS + i = _strtoi64(s, &stop, 10); +#else + i = strtoll(s, &stop, 10); +#endif + return stop && *stop == 0; } bool to_double(char const * s, double & d) { char * stop; - double x = strtod(s, &stop); - if (stop && *stop == 0) - { - d = x; - return true; - } - return false; + d = strtod(s, &stop); + return stop && *stop == 0; } UniString MakeLowerCase(UniString const & s)