From b44546279430f1d0a5de97d4d72eee98a047d829 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Mon, 25 Sep 2017 15:42:59 +0200 Subject: [PATCH] fix(utils): better code for strtoxxx --- src/utils/utils.cpp | 107 ++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 64 deletions(-) diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 66261305b..0ff383c5f 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -43,6 +43,8 @@ bool Utils::iequals (const string &a, const string &b) { return true; } +// ----------------------------------------------------------------------------- + vector Utils::split (const string &str, const string &delimiter) { vector out; @@ -54,6 +56,8 @@ vector Utils::split (const string &str, const string &delimiter) { return out; } +// ----------------------------------------------------------------------------- + #ifndef __ANDROID__ #define TO_STRING_IMPL(TYPE) \ string Utils::toString(TYPE val) { \ @@ -78,86 +82,59 @@ TO_STRING_IMPL(float) TO_STRING_IMPL(double) TO_STRING_IMPL(long double) +#undef TO_STRING_IMPL + string Utils::toString (const void *val) { ostringstream ss; ss << val; return ss.str(); } -int Utils::stoi (const string &str, size_t *idx, int base) { - return stoi(str.c_str(), idx, base); -} +// ----------------------------------------------------------------------------- -long long Utils::stoll (const string &str, size_t *idx, int base) { - return stoll(str.c_str(), idx, base); -} +#define STRING_TO_NUMBER_IMPL(TYPE, SUFFIX) \ + TYPE Utils::sto ## SUFFIX (const string &str, size_t *idx, int base) { \ + return sto ## SUFFIX(str.c_str(), idx, base); \ + } \ + TYPE Utils::sto ## SUFFIX (const char *str, size_t *idx, int base) { \ + char *p; \ + TYPE v = strto ## SUFFIX(str, &p, base); \ + if (idx) \ + *idx = static_cast(p - str); \ + return v; \ + } \ -unsigned long long Utils::stoull (const string &str, size_t *idx, int base) { - return stoull(str.c_str(), idx, base); -} +#define STRING_TO_NUMBER_IMPL_BASE_LESS(TYPE, SUFFIX) \ + TYPE Utils::sto ## SUFFIX (const string &str, size_t *idx) { \ + return sto ## SUFFIX(str.c_str(), idx); \ + } \ + TYPE Utils::sto ## SUFFIX (const char *str, size_t *idx) { \ + char *p; \ + TYPE v = strto ## SUFFIX(str, &p); \ + if (idx) \ + *idx = static_cast(p - str); \ + return v; \ + } \ -double Utils::stod (const string &str, size_t *idx) { - return stod(str.c_str(), idx); -} +#define strtoi(STR, IDX, BASE) static_cast(strtol(STR, IDX, BASE)) +STRING_TO_NUMBER_IMPL(int, i) +#undef strtoi -float Utils::stof (const string &str, size_t *idx) { - return stof(str.c_str(), idx); -} +STRING_TO_NUMBER_IMPL(long long, ll) +STRING_TO_NUMBER_IMPL(unsigned long long, ull) + +STRING_TO_NUMBER_IMPL_BASE_LESS(double, d) +STRING_TO_NUMBER_IMPL_BASE_LESS(float, f) + +#undef STRING_TO_NUMBER_IMPL +#undef STRING_TO_NUMBER_IMPL_BASE_LESS bool Utils::stob (const string &str) { const string lowerStr = stringToLower(str); return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1"); } -int Utils::stoi (const char *str, size_t *idx, int base) { - char *p; - int v = static_cast(strtol(str, &p, base)); - - if (idx) - *idx = static_cast(p - str); - - return v; -} - -long long Utils::stoll (const char *str, size_t *idx, int base) { - char *p; - long long v = static_cast(strtoll(str, &p, base)); - - if (idx) - *idx = static_cast(p - str); - - return v; -} - -unsigned long long Utils::stoull (const char *str, size_t *idx, int base) { - char *p; - unsigned long long v = static_cast(strtoull(str, &p, base)); - - if (idx) - *idx = static_cast(p - str); - - return v; -} - -double Utils::stod (const char *str, size_t *idx) { - char *p; - double v = strtod(str, &p); - - if (idx) - *idx = static_cast(p - str); - - return v; -} - -float Utils::stof (const char *str, size_t *idx) { - char *p; - float v = strtof(str, &p); - - if (idx) - *idx = static_cast(p - str); - - return v; -} +// ----------------------------------------------------------------------------- string Utils::stringToLower (const string &str) { string result(str.size(), ' '); @@ -165,6 +142,8 @@ string Utils::stringToLower (const string &str) { return result; } +// ----------------------------------------------------------------------------- + char *Utils::utf8ToChar (uint32_t ic) { char *result = new char[5]; int size = 0;