Selaa lähdekoodia

Rewrite util::divide

Tatsuhiro Tsujikawa 12 vuotta sitten
vanhempi
commit
2761301dfb

+ 3 - 2
src/FtpConnection.cc

@@ -403,8 +403,9 @@ int FtpConnection::receiveSizeResponse(int64_t& size)
   std::pair<int, std::string> response;
   if(bulkReceiveResponse(response)) {
     if(response.first == 213) {
-      std::pair<Sip, Sip> rp;
-      util::divide(rp, response.second.begin(), response.second.end(), ' ');
+      auto rp = util::divide(std::begin(response.second),
+                             std::end(response.second),
+                             ' ');
       if(!util::parseLLIntNoThrow(size, std::string(rp.second.first,
                                                     rp.second.second)) ||
          size < 0) {

+ 2 - 4
src/HttpServer.cc

@@ -289,14 +289,12 @@ bool HttpServer::authenticate()
   if(authHeader.empty()) {
     return false;
   }
-  std::pair<Scip, Scip> p;
-  util::divide(p, authHeader.begin(), authHeader.end(), ' ');
+  auto p = util::divide(std::begin(authHeader), std::end(authHeader), ' ');
   if(!util::streq(p.first.first, p.first.second, "Basic")) {
     return false;
   }
   std::string userpass = base64::decode(p.second.first, p.second.second);
-  std::pair<Sip, Sip> up;
-  util::divide(up, userpass.begin(), userpass.end(), ':');
+  auto up = util::divide(std::begin(userpass), std::end(userpass), ':');
   return util::streq(up.first.first, up.first.second,
                      username_.begin(), username_.end()) &&
     util::streq(up.second.first, up.second.second,

+ 1 - 2
src/OptionHandlerImpl.cc

@@ -372,8 +372,7 @@ ChecksumOptionHandler::~ChecksumOptionHandler() {}
 void ChecksumOptionHandler::parseArg(Option& option, const std::string& optarg)
   const
 {
-  std::pair<Scip, Scip> p;
-  util::divide(p, optarg.begin(), optarg.end(), '=');
+  auto p = util::divide(std::begin(optarg), std::end(optarg), '=');
   std::string hashType(p.first.first, p.first.second);
   std::string hexDigest(p.second.first, p.second.second);
   util::lowercase(hashType);

+ 1 - 2
src/OptionParser.cc

@@ -230,8 +230,7 @@ void OptionParser::parse(Option& option, std::istream& is) const
     if(line.empty() || line[0] == '#') {
       continue;
     }
-    std::pair<Sip, Sip> nv;
-    util::divide(nv, line.begin(), line.end(), '=');
+    auto nv = util::divide(std::begin(line), std::end(line), '=');
     if(nv.first.first == nv.first.second) {
       continue;
     }

+ 1 - 2
src/RpcMethodImpl.cc

@@ -1450,8 +1450,7 @@ void changeOption
   grOption->merge(option);
   if(option.defined(PREF_CHECKSUM)) {
     const std::string& checksum = grOption->get(PREF_CHECKSUM);
-    std::pair<Scip, Scip> p;
-    util::divide(p, checksum.begin(), checksum.end(), '=');
+    auto p = util::divide(std::begin(checksum), std::end(checksum), '=');
     std::string hashType(p.first.first, p.first.second);
     util::lowercase(hashType);
     dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));

+ 1 - 2
src/ServerStatMan.cc

@@ -185,8 +185,7 @@ bool ServerStatMan::load(const std::string& filename)
     std::vector<std::string> m(MAX_FIELD);
     for(std::vector<Scip>::const_iterator i = items.begin(),
           eoi = items.end(); i != eoi; ++i) {
-      std::pair<Scip, Scip> p;
-      util::divide(p, (*i).first, (*i).second, '=');
+      auto p = util::divide((*i).first, (*i).second, '=');
       int id = idField(p.first.first, p.first.second);
       if(id != MAX_FIELD) {
         m[id].assign(p.second.first, p.second.second);

+ 1 - 2
src/download_helper.cc

@@ -146,8 +146,7 @@ std::shared_ptr<RequestGroup> createRequestGroup
 #ifdef ENABLE_MESSAGE_DIGEST
   const std::string& checksum = option->get(PREF_CHECKSUM);
   if(!checksum.empty()) {
-    std::pair<Scip, Scip> p;
-    util::divide(p, std::begin(checksum), std::end(checksum), '=');
+    auto p = util::divide(std::begin(checksum), std::end(checksum), '=');
     std::string hashType(p.first.first, p.first.second);
     std::string hexDigest(p.second.first, p.second.second);
     util::lowercase(hashType);

+ 1 - 2
src/magnet.cc

@@ -52,8 +52,7 @@ std::shared_ptr<Dict> parse(const std::string& magnet)
                   '&');
   for(std::vector<Scip>::const_iterator i = queries.begin(),
         eoi = queries.end(); i != eoi; ++i) {
-    std::pair<Scip, Scip> p;
-    util::divide(p, (*i).first, (*i).second, '=');
+    auto p = util::divide((*i).first, (*i).second, '=');
     std::string name(p.first.first, p.first.second);
     std::string value(util::percentDecode(p.second.first, p.second.second));
     List* l = downcast<List>(dict->get(name));

+ 1 - 2
src/util.cc

@@ -1482,8 +1482,7 @@ std::string htmlEscape(const std::string& src)
 std::pair<size_t, std::string>
 parseIndexPath(const std::string& line)
 {
-  std::pair<Scip, Scip> p;
-  divide(p, line.begin(), line.end(), '=');
+  auto p = divide(std::begin(line), std::end(line), '=');
   uint32_t index;
   if(!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) {
     throw DL_ABORT_EX("Bad path index");

+ 6 - 11
src/util.h

@@ -158,20 +158,15 @@ std::string strip
 (const std::string& str, const char* chars = DEFAULT_STRIP_CHARSET);
 
 template<typename InputIterator>
-void divide
-(std::pair<std::pair<InputIterator, InputIterator>,
-           std::pair<InputIterator, InputIterator> >& hp,
- InputIterator first,
- InputIterator last,
- char delim)
+std::pair<std::pair<InputIterator, InputIterator>,
+          std::pair<InputIterator, InputIterator>>
+divide(InputIterator first, InputIterator last, char delim)
 {
-  InputIterator dpos = std::find(first, last, delim);
+  auto dpos = std::find(first, last, delim);
   if(dpos == last) {
-    hp.first = stripIter(first, last);
-    hp.second.first = hp.second.second;
+    return {stripIter(first, last), {last, last}};
   } else {
-    hp.first = stripIter(first, dpos);
-    hp.second = stripIter(dpos+1, last);
+    return {stripIter(first, dpos), stripIter(dpos+1, last)};
   }
 }
 

+ 5 - 6
test/UtilTest.cc

@@ -266,33 +266,32 @@ void UtilTest::testLstripIter_char()
 }
 
 void UtilTest::testDivide() {
-  std::pair<Sip, Sip> p1;
   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"),
                        std::string(p1.first.first, p1.first.second));
   CPPUNIT_ASSERT_EQUAL(std::string("value"),
                        std::string(p1.second.first, p1.second.second));
   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"),
                        std::string(p1.first.first, p1.first.second));
   CPPUNIT_ASSERT_EQUAL(std::string("value"),
                        std::string(p1.second.first, p1.second.second));
   s = "=value";
-  util::divide(p1, s.begin(), s.end(), '=');
+  p1 = util::divide(std::begin(s), std::end(s), '=');
   CPPUNIT_ASSERT_EQUAL(std::string(""),
                        std::string(p1.first.first, p1.first.second));
   CPPUNIT_ASSERT_EQUAL(std::string("value"),
                        std::string(p1.second.first, p1.second.second));
   s = "name=";
-  util::divide(p1, s.begin(), s.end(), '=');
+  p1 = util::divide(std::begin(s), std::end(s), '=');
   CPPUNIT_ASSERT_EQUAL(std::string("name"),
                        std::string(p1.first.first, p1.first.second));
   CPPUNIT_ASSERT_EQUAL(std::string(""),
                        std::string(p1.second.first, p1.second.second));
   s = "name";
-  util::divide(p1, s.begin(), s.end(), '=');
+  p1 = util::divide(std::begin(s), std::end(s), '=');
   CPPUNIT_ASSERT_EQUAL(std::string("name"),
                        std::string(p1.first.first, p1.first.second));
   CPPUNIT_ASSERT_EQUAL(std::string(""),