improved extract*Number functions from FbTk/StringUtil.cc
This commit is contained in:
parent
6ecfa0ef3e
commit
b76be98227
1 changed files with 22 additions and 6 deletions
|
@ -63,13 +63,14 @@ using std::transform;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
int extractLongNumber(const char* in, long long int& out) {
|
template <typename T>
|
||||||
|
int extractBigNumber(const char* in, T (*extractFunc)(const char*, char**, int), T& out) {
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char* end = 0;
|
char* end = 0;
|
||||||
long long int result = strtoll(in, &end, 0);
|
T result = extractFunc(in, &end, 0);
|
||||||
|
|
||||||
if (errno == 0 && end != in) {
|
if (errno == 0 && end != in) {
|
||||||
out = result;
|
out = result;
|
||||||
|
@ -80,11 +81,11 @@ int extractLongNumber(const char* in, long long int& out) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int extractNumber(const std::string& in, T& out) {
|
int extractSignedNumber(const std::string& in, T& out) {
|
||||||
|
|
||||||
long long int result = 0;
|
long long int result = 0;
|
||||||
|
|
||||||
if (::extractLongNumber(in.c_str(), result) && out >= 0) {
|
if (::extractBigNumber(in.c_str(), strtoll, result)) {
|
||||||
out = static_cast<T>(result);
|
out = static_cast<T>(result);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -92,6 +93,21 @@ int extractNumber(const std::string& in, T& out) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
int extractUnsignedNumber(const std::string& in, T& out) {
|
||||||
|
|
||||||
|
unsigned long long int result = 0;
|
||||||
|
|
||||||
|
if (::extractBigNumber(in.c_str(), strtoull, result) && result >= 0) {
|
||||||
|
out = static_cast<T>(result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,11 +116,11 @@ namespace FbTk {
|
||||||
namespace StringUtil {
|
namespace StringUtil {
|
||||||
|
|
||||||
int extractNumber(const std::string& in, int& out) {
|
int extractNumber(const std::string& in, int& out) {
|
||||||
return ::extractNumber<int>(in, out);
|
return ::extractSignedNumber<int>(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
int extractNumber(const std::string& in, unsigned int& out) {
|
int extractNumber(const std::string& in, unsigned int& out) {
|
||||||
return ::extractNumber<unsigned int>(in, out);
|
return ::extractUnsignedNumber<unsigned int>(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string number2String(int num) {
|
std::string number2String(int num) {
|
||||||
|
|
Loading…
Reference in a new issue