mirror of https://github.com/aria2/aria2
2009-11-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added util::parseUIntNoThrow(). Use it in Request::parseUrl(). * src/Request.cc * src/util.cc * src/util.h * test/RequestTest.ccpull/1/head
parent
8865b9e8e6
commit
9cdf102850
|
@ -1,3 +1,11 @@
|
|||
2009-11-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Added util::parseUIntNoThrow(). Use it in Request::parseUrl().
|
||||
* src/Request.cc
|
||||
* src/util.cc
|
||||
* src/util.h
|
||||
* test/RequestTest.cc
|
||||
|
||||
2009-11-13 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||
|
||||
Rewritten util::uitos()
|
||||
|
|
|
@ -38,7 +38,6 @@
|
|||
|
||||
#include "util.h"
|
||||
#include "FeatureConfig.h"
|
||||
#include "RecoverableException.h"
|
||||
#include "StringFormat.h"
|
||||
#include "A2STR.h"
|
||||
#include "a2functional.h"
|
||||
|
@ -252,14 +251,13 @@ bool Request::parseUrl(const std::string& url) {
|
|||
// its protocol..
|
||||
_port = defPort;
|
||||
} else {
|
||||
try {
|
||||
unsigned int tempPort =
|
||||
util::parseUInt(std::string(portFirst, authorityLast));
|
||||
uint32_t tempPort;
|
||||
if(util::parseUIntNoThrow(tempPort, std::string(portFirst, authorityLast))){
|
||||
if(65535 < tempPort) {
|
||||
return false;
|
||||
}
|
||||
_port = tempPort;
|
||||
} catch(RecoverableException& e) {
|
||||
_port = tempPort;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
22
src/util.cc
22
src/util.cc
|
@ -395,6 +395,28 @@ uint32_t parseUInt(const std::string& s, int base)
|
|||
return v;
|
||||
}
|
||||
|
||||
bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base)
|
||||
{
|
||||
std::string trimed = trim(s);
|
||||
if(trimed.empty()) {
|
||||
return false;
|
||||
}
|
||||
// We don't allow negative number.
|
||||
if(trimed[0] == '-') {
|
||||
return false;
|
||||
}
|
||||
char* stop;
|
||||
errno = 0;
|
||||
unsigned long int v = strtoul(trimed.c_str(), &stop, base);
|
||||
if(*stop != '\0') {
|
||||
return false;
|
||||
} else if(((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT32_MAX)) {
|
||||
return false;
|
||||
}
|
||||
result = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t parseLLInt(const std::string& s, int32_t base)
|
||||
{
|
||||
std::string trimed = trim(s);
|
||||
|
|
|
@ -180,6 +180,8 @@ int32_t parseInt(const std::string& s, int32_t base = 10);
|
|||
|
||||
uint32_t parseUInt(const std::string& s, int base = 10);
|
||||
|
||||
bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base = 10);
|
||||
|
||||
int64_t parseLLInt(const std::string& s, int32_t base = 10);
|
||||
|
||||
uint64_t parseULLInt(const std::string& s, int base = 10);
|
||||
|
|
Loading…
Reference in New Issue