From b76be98227bd6e62bc7e5a5fc1c3af0a3dd01b9c Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Wed, 8 Sep 2010 21:42:48 +0200 Subject: improved extract*Number functions from FbTk/StringUtil.cc --- src/FbTk/StringUtil.cc | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index 4678f03..691b600 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -63,13 +63,14 @@ using std::transform; namespace { -int extractLongNumber(const char* in, long long int& out) { +template +int extractBigNumber(const char* in, T (*extractFunc)(const char*, char**, int), T& out) { errno = 0; int ret = 0; char* end = 0; - long long int result = strtoll(in, &end, 0); + T result = extractFunc(in, &end, 0); if (errno == 0 && end != in) { out = result; @@ -80,11 +81,11 @@ int extractLongNumber(const char* in, long long int& out) { } template -int extractNumber(const std::string& in, T& out) { +int extractSignedNumber(const std::string& in, T& out) { long long int result = 0; - if (::extractLongNumber(in.c_str(), result) && out >= 0) { + if (::extractBigNumber(in.c_str(), strtoll, result)) { out = static_cast(result); return 1; } @@ -92,6 +93,21 @@ int extractNumber(const std::string& in, T& out) { return 0; } +template +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(result); + return 1; + } + + return 0; +} + + + } @@ -100,11 +116,11 @@ namespace FbTk { namespace StringUtil { int extractNumber(const std::string& in, int& out) { - return ::extractNumber(in, out); + return ::extractSignedNumber(in, out); } int extractNumber(const std::string& in, unsigned int& out) { - return ::extractNumber(in, out); + return ::extractUnsignedNumber(in, out); } std::string number2String(int num) { -- cgit v0.11.2