mirror of https://github.com/aria2/aria2
2010-02-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Rewritten util::parseInt() and parseUInt() * src/util.ccpull/1/head
parent
aa2036a70d
commit
8f9af67b99
|
@ -1,3 +1,8 @@
|
||||||
|
2010-02-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Rewritten util::parseInt() and parseUInt()
|
||||||
|
* src/util.cc
|
||||||
|
|
||||||
2010-02-21 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2010-02-21 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Fixed compile error with mingw32 cross compiler. Defined
|
Fixed compile error with mingw32 cross compiler. Defined
|
||||||
|
|
39
src/util.cc
39
src/util.cc
|
@ -421,47 +421,20 @@ int getNum(const char* buf, int offset, size_t length) {
|
||||||
|
|
||||||
int32_t parseInt(const std::string& s, int32_t base)
|
int32_t parseInt(const std::string& s, int32_t base)
|
||||||
{
|
{
|
||||||
std::string trimed = trim(s);
|
int64_t v = parseLLInt(s, base);
|
||||||
if(trimed.empty()) {
|
if(v < INT32_MIN || INT32_MAX < v) {
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
||||||
"empty string").str());
|
s.c_str()).str());
|
||||||
}
|
|
||||||
char* stop;
|
|
||||||
errno = 0;
|
|
||||||
long int v = strtol(trimed.c_str(), &stop, base);
|
|
||||||
if(*stop != '\0') {
|
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
|
||||||
trimed.c_str()).str());
|
|
||||||
} else if((((v == LONG_MIN) || (v == LONG_MAX)) && (errno == ERANGE)) ||
|
|
||||||
(v > INT32_MAX) ||
|
|
||||||
(v < INT32_MIN)) {
|
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
|
||||||
trimed.c_str()).str());
|
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t parseUInt(const std::string& s, int base)
|
uint32_t parseUInt(const std::string& s, int base)
|
||||||
{
|
{
|
||||||
std::string trimed = trim(s);
|
uint64_t v = parseULLInt(s, base);
|
||||||
if(trimed.empty()) {
|
if(UINT32_MAX < v) {
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
||||||
"empty string").str());
|
s.c_str()).str());
|
||||||
}
|
|
||||||
// We don't allow negative number.
|
|
||||||
if(trimed[0] == '-') {
|
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
|
||||||
trimed.c_str()).str());
|
|
||||||
}
|
|
||||||
char* stop;
|
|
||||||
errno = 0;
|
|
||||||
unsigned long int v = strtoul(trimed.c_str(), &stop, base);
|
|
||||||
if(*stop != '\0') {
|
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
|
||||||
trimed.c_str()).str());
|
|
||||||
} else if(((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT32_MAX)) {
|
|
||||||
throw DL_ABORT_EX(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
|
|
||||||
trimed.c_str()).str());
|
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue