diff options
author | Ilya Zverev <ilya@zverev.info> | 2016-05-31 12:49:59 +0300 |
---|---|---|
committer | Ilya Zverev <ilya@zverev.info> | 2016-05-31 12:49:59 +0300 |
commit | 6022f55ab630637453fd391effcf0cad6f5ae6bb (patch) | |
tree | 8e8b3d4702e74f3616c81613592f7108f63fffc3 /base | |
parent | fb768dcab81653470e0ca4dfde6c650bfa8c487e (diff) | |
parent | ff1fc714c7e4b47c224113158a591f3db2e46656 (diff) |
Merge pull request #3302 from syershov/MAPSME-1232
[booking] Process data from booking.com
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/string_utils_test.cpp | 39 | ||||
-rw-r--r-- | base/string_utils.cpp | 31 | ||||
-rw-r--r-- | base/string_utils.hpp | 2 |
3 files changed, 64 insertions, 8 deletions
diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index f1e2dff2cc..684a33a527 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -176,6 +176,10 @@ UNIT_TEST(to_int) int i; string s; + s = "AF"; + TEST(strings::to_int(s, i, 16), ()); + TEST_EQUAL(175, i, ()); + s = "-2"; TEST(strings::to_int(s, i), ()); TEST_EQUAL(-2, i, ()); @@ -190,10 +194,43 @@ UNIT_TEST(to_int) s = "labuda"; TEST(!strings::to_int(s, i), ()); +} + +UNIT_TEST(to_uint) +{ + unsigned int i; + string s; + + s = ""; + TEST(!strings::to_uint(s, i), ()); + s = "-2"; + TEST(!strings::to_uint(s, i), ()); + + s = "0"; + TEST(strings::to_uint(s, i), ()); + TEST_EQUAL(0, i, ()); + + s = "123456789123456789123456789"; + TEST(!strings::to_uint(s, i), ()); + + s = "labuda"; + TEST(!strings::to_uint(s, i), ()); + s = "AF"; - TEST(strings::to_int(s, i, 16), ()); + TEST(strings::to_uint(s, i, 16), ()); TEST_EQUAL(175, i, ()); + + s = "100"; + TEST(strings::to_uint(s, i), ()); + TEST_EQUAL(100, i, ()); + + s = "4294967295"; + TEST(strings::to_uint(s, i), ()); + TEST_EQUAL(0xFFFFFFFF, i, ()); + + s = "4294967296"; + TEST(!strings::to_uint(s, i), ()); } UNIT_TEST(to_uint64) diff --git a/base/string_utils.cpp b/base/string_utils.cpp index da13ecb282..22dfad3030 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -42,19 +42,36 @@ UniChar LastUniChar(string const & s) return *iter; } -bool to_int(char const * s, int & i, int base /*= 10*/) +namespace { - char * stop; - long const x = strtol(s, &stop, base); - if (*stop == 0) +template <typename T, typename TResult> +bool IntegerCheck(char const * start, char const * stop, T x, TResult & out) +{ + if (errno != EINVAL && *stop == 0) { - i = static_cast<int>(x); - ASSERT_EQUAL(static_cast<long>(i), x, ()); - return true; + out = static_cast<TResult>(x); + return static_cast<T>(out) == x; } + errno = 0; return false; } +} // namespace + +bool to_int(char const * start, int & i, int base /*= 10*/) +{ + char * stop; + long const v = strtol(start, &stop, base); + return IntegerCheck(start, stop, v, i); +} + +bool to_uint(char const * start, unsigned int & i, int base /*= 10*/) +{ + char * stop; + unsigned long const v = strtoul(start, &stop, base); + return IntegerCheck(start, stop, v, i); +} + bool to_uint64(char const * s, uint64_t & i) { char * stop; diff --git a/base/string_utils.hpp b/base/string_utils.hpp index f7c5b775ff..ca5449ae2b 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -209,6 +209,7 @@ template <class T, size_t N, class TT> bool IsInArray(T (&arr) [N], TT const & t /// @name From string to numeric. //@{ bool to_int(char const * s, int & i, int base = 10); +bool to_uint(char const * s, unsigned int & i, int base = 10); bool to_uint64(char const * s, uint64_t & i); bool to_int64(char const * s, int64_t & i); bool to_double(char const * s, double & d); @@ -216,6 +217,7 @@ bool to_double(char const * s, double & d); inline bool is_number(string const & s) { int64_t dummy; return to_int64(s.c_str(), dummy); } inline bool to_int(string const & s, int & i, int base = 10) { return to_int(s.c_str(), i, base); } +inline bool to_uint(string const & s, unsigned int & i, int base = 10) { return to_uint(s.c_str(), i, base); } inline bool to_uint64(string const & s, uint64_t & i) { return to_uint64(s.c_str(), i); } inline bool to_int64(string const & s, int64_t & i) { return to_int64(s.c_str(), i); } inline bool to_double(string const & s, double & d) { return to_double(s.c_str(), d); } |