mirror of https://github.com/aria2/aria2
Rewrite util::divide
parent
2f6eff3821
commit
2761301dfb
|
@ -403,8 +403,9 @@ int FtpConnection::receiveSizeResponse(int64_t& size)
|
||||||
std::pair<int, std::string> response;
|
std::pair<int, std::string> response;
|
||||||
if(bulkReceiveResponse(response)) {
|
if(bulkReceiveResponse(response)) {
|
||||||
if(response.first == 213) {
|
if(response.first == 213) {
|
||||||
std::pair<Sip, Sip> rp;
|
auto rp = util::divide(std::begin(response.second),
|
||||||
util::divide(rp, response.second.begin(), response.second.end(), ' ');
|
std::end(response.second),
|
||||||
|
' ');
|
||||||
if(!util::parseLLIntNoThrow(size, std::string(rp.second.first,
|
if(!util::parseLLIntNoThrow(size, std::string(rp.second.first,
|
||||||
rp.second.second)) ||
|
rp.second.second)) ||
|
||||||
size < 0) {
|
size < 0) {
|
||||||
|
|
|
@ -289,14 +289,12 @@ bool HttpServer::authenticate()
|
||||||
if(authHeader.empty()) {
|
if(authHeader.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide(std::begin(authHeader), std::end(authHeader), ' ');
|
||||||
util::divide(p, authHeader.begin(), authHeader.end(), ' ');
|
|
||||||
if(!util::streq(p.first.first, p.first.second, "Basic")) {
|
if(!util::streq(p.first.first, p.first.second, "Basic")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string userpass = base64::decode(p.second.first, p.second.second);
|
std::string userpass = base64::decode(p.second.first, p.second.second);
|
||||||
std::pair<Sip, Sip> up;
|
auto up = util::divide(std::begin(userpass), std::end(userpass), ':');
|
||||||
util::divide(up, userpass.begin(), userpass.end(), ':');
|
|
||||||
return util::streq(up.first.first, up.first.second,
|
return util::streq(up.first.first, up.first.second,
|
||||||
username_.begin(), username_.end()) &&
|
username_.begin(), username_.end()) &&
|
||||||
util::streq(up.second.first, up.second.second,
|
util::streq(up.second.first, up.second.second,
|
||||||
|
|
|
@ -372,8 +372,7 @@ ChecksumOptionHandler::~ChecksumOptionHandler() {}
|
||||||
void ChecksumOptionHandler::parseArg(Option& option, const std::string& optarg)
|
void ChecksumOptionHandler::parseArg(Option& option, const std::string& optarg)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide(std::begin(optarg), std::end(optarg), '=');
|
||||||
util::divide(p, optarg.begin(), optarg.end(), '=');
|
|
||||||
std::string hashType(p.first.first, p.first.second);
|
std::string hashType(p.first.first, p.first.second);
|
||||||
std::string hexDigest(p.second.first, p.second.second);
|
std::string hexDigest(p.second.first, p.second.second);
|
||||||
util::lowercase(hashType);
|
util::lowercase(hashType);
|
||||||
|
|
|
@ -230,8 +230,7 @@ void OptionParser::parse(Option& option, std::istream& is) const
|
||||||
if(line.empty() || line[0] == '#') {
|
if(line.empty() || line[0] == '#') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::pair<Sip, Sip> nv;
|
auto nv = util::divide(std::begin(line), std::end(line), '=');
|
||||||
util::divide(nv, line.begin(), line.end(), '=');
|
|
||||||
if(nv.first.first == nv.first.second) {
|
if(nv.first.first == nv.first.second) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1450,8 +1450,7 @@ void changeOption
|
||||||
grOption->merge(option);
|
grOption->merge(option);
|
||||||
if(option.defined(PREF_CHECKSUM)) {
|
if(option.defined(PREF_CHECKSUM)) {
|
||||||
const std::string& checksum = grOption->get(PREF_CHECKSUM);
|
const std::string& checksum = grOption->get(PREF_CHECKSUM);
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide(std::begin(checksum), std::end(checksum), '=');
|
||||||
util::divide(p, checksum.begin(), checksum.end(), '=');
|
|
||||||
std::string hashType(p.first.first, p.first.second);
|
std::string hashType(p.first.first, p.first.second);
|
||||||
util::lowercase(hashType);
|
util::lowercase(hashType);
|
||||||
dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));
|
dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));
|
||||||
|
|
|
@ -185,8 +185,7 @@ bool ServerStatMan::load(const std::string& filename)
|
||||||
std::vector<std::string> m(MAX_FIELD);
|
std::vector<std::string> m(MAX_FIELD);
|
||||||
for(std::vector<Scip>::const_iterator i = items.begin(),
|
for(std::vector<Scip>::const_iterator i = items.begin(),
|
||||||
eoi = items.end(); i != eoi; ++i) {
|
eoi = items.end(); i != eoi; ++i) {
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide((*i).first, (*i).second, '=');
|
||||||
util::divide(p, (*i).first, (*i).second, '=');
|
|
||||||
int id = idField(p.first.first, p.first.second);
|
int id = idField(p.first.first, p.first.second);
|
||||||
if(id != MAX_FIELD) {
|
if(id != MAX_FIELD) {
|
||||||
m[id].assign(p.second.first, p.second.second);
|
m[id].assign(p.second.first, p.second.second);
|
||||||
|
|
|
@ -146,8 +146,7 @@ std::shared_ptr<RequestGroup> createRequestGroup
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
const std::string& checksum = option->get(PREF_CHECKSUM);
|
const std::string& checksum = option->get(PREF_CHECKSUM);
|
||||||
if(!checksum.empty()) {
|
if(!checksum.empty()) {
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide(std::begin(checksum), std::end(checksum), '=');
|
||||||
util::divide(p, std::begin(checksum), std::end(checksum), '=');
|
|
||||||
std::string hashType(p.first.first, p.first.second);
|
std::string hashType(p.first.first, p.first.second);
|
||||||
std::string hexDigest(p.second.first, p.second.second);
|
std::string hexDigest(p.second.first, p.second.second);
|
||||||
util::lowercase(hashType);
|
util::lowercase(hashType);
|
||||||
|
|
|
@ -52,8 +52,7 @@ std::shared_ptr<Dict> parse(const std::string& magnet)
|
||||||
'&');
|
'&');
|
||||||
for(std::vector<Scip>::const_iterator i = queries.begin(),
|
for(std::vector<Scip>::const_iterator i = queries.begin(),
|
||||||
eoi = queries.end(); i != eoi; ++i) {
|
eoi = queries.end(); i != eoi; ++i) {
|
||||||
std::pair<Scip, Scip> p;
|
auto p = util::divide((*i).first, (*i).second, '=');
|
||||||
util::divide(p, (*i).first, (*i).second, '=');
|
|
||||||
std::string name(p.first.first, p.first.second);
|
std::string name(p.first.first, p.first.second);
|
||||||
std::string value(util::percentDecode(p.second.first, p.second.second));
|
std::string value(util::percentDecode(p.second.first, p.second.second));
|
||||||
List* l = downcast<List>(dict->get(name));
|
List* l = downcast<List>(dict->get(name));
|
||||||
|
|
|
@ -1482,8 +1482,7 @@ std::string htmlEscape(const std::string& src)
|
||||||
std::pair<size_t, std::string>
|
std::pair<size_t, std::string>
|
||||||
parseIndexPath(const std::string& line)
|
parseIndexPath(const std::string& line)
|
||||||
{
|
{
|
||||||
std::pair<Scip, Scip> p;
|
auto p = divide(std::begin(line), std::end(line), '=');
|
||||||
divide(p, line.begin(), line.end(), '=');
|
|
||||||
uint32_t index;
|
uint32_t index;
|
||||||
if(!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) {
|
if(!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) {
|
||||||
throw DL_ABORT_EX("Bad path index");
|
throw DL_ABORT_EX("Bad path index");
|
||||||
|
|
17
src/util.h
17
src/util.h
|
@ -158,20 +158,15 @@ std::string strip
|
||||||
(const std::string& str, const char* chars = DEFAULT_STRIP_CHARSET);
|
(const std::string& str, const char* chars = DEFAULT_STRIP_CHARSET);
|
||||||
|
|
||||||
template<typename InputIterator>
|
template<typename InputIterator>
|
||||||
void divide
|
std::pair<std::pair<InputIterator, InputIterator>,
|
||||||
(std::pair<std::pair<InputIterator, InputIterator>,
|
std::pair<InputIterator, InputIterator>>
|
||||||
std::pair<InputIterator, InputIterator> >& hp,
|
divide(InputIterator first, InputIterator last, char delim)
|
||||||
InputIterator first,
|
|
||||||
InputIterator last,
|
|
||||||
char delim)
|
|
||||||
{
|
{
|
||||||
InputIterator dpos = std::find(first, last, delim);
|
auto dpos = std::find(first, last, delim);
|
||||||
if(dpos == last) {
|
if(dpos == last) {
|
||||||
hp.first = stripIter(first, last);
|
return {stripIter(first, last), {last, last}};
|
||||||
hp.second.first = hp.second.second;
|
|
||||||
} else {
|
} else {
|
||||||
hp.first = stripIter(first, dpos);
|
return {stripIter(first, dpos), stripIter(dpos+1, last)};
|
||||||
hp.second = stripIter(dpos+1, last);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -266,33 +266,32 @@ void UtilTest::testLstripIter_char()
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testDivide() {
|
void UtilTest::testDivide() {
|
||||||
std::pair<Sip, Sip> p1;
|
|
||||||
std::string s = "name=value";
|
std::string s = "name=value";
|
||||||
util::divide(p1, s.begin(), s.end(), '=');
|
auto p1 = util::divide(std::begin(s), std::end(s), '=');
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
||||||
std::string(p1.first.first, p1.first.second));
|
std::string(p1.first.first, p1.first.second));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
||||||
std::string(p1.second.first, p1.second.second));
|
std::string(p1.second.first, p1.second.second));
|
||||||
s = " name = value ";
|
s = " name = value ";
|
||||||
util::divide(p1, s.begin(), s.end(), '=');
|
p1 = util::divide(std::begin(s), std::end(s), '=');
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
||||||
std::string(p1.first.first, p1.first.second));
|
std::string(p1.first.first, p1.first.second));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
||||||
std::string(p1.second.first, p1.second.second));
|
std::string(p1.second.first, p1.second.second));
|
||||||
s = "=value";
|
s = "=value";
|
||||||
util::divide(p1, s.begin(), s.end(), '=');
|
p1 = util::divide(std::begin(s), std::end(s), '=');
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
||||||
std::string(p1.first.first, p1.first.second));
|
std::string(p1.first.first, p1.first.second));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
CPPUNIT_ASSERT_EQUAL(std::string("value"),
|
||||||
std::string(p1.second.first, p1.second.second));
|
std::string(p1.second.first, p1.second.second));
|
||||||
s = "name=";
|
s = "name=";
|
||||||
util::divide(p1, s.begin(), s.end(), '=');
|
p1 = util::divide(std::begin(s), std::end(s), '=');
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
||||||
std::string(p1.first.first, p1.first.second));
|
std::string(p1.first.first, p1.first.second));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
||||||
std::string(p1.second.first, p1.second.second));
|
std::string(p1.second.first, p1.second.second));
|
||||||
s = "name";
|
s = "name";
|
||||||
util::divide(p1, s.begin(), s.end(), '=');
|
p1 = util::divide(std::begin(s), std::end(s), '=');
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
CPPUNIT_ASSERT_EQUAL(std::string("name"),
|
||||||
std::string(p1.first.first, p1.first.second));
|
std::string(p1.first.first, p1.first.second));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
||||||
|
|
Loading…
Reference in New Issue