]> git.tdb.fi Git - libs/net.git/commitdiff
Prefer range-based for loops where possible
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 22:18:32 +0000 (00:18 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 22:18:32 +0000 (00:18 +0200)
source/http/message.cpp
source/http/server.cpp
source/http/utils.cpp
source/net/protocol.cpp
source/net/protocol.h

index b86f95495eb267639bbc1bdf9ee58662dd198bf1..2ae0ff80a641e3f133f051c37fe66569e6a93944 100644 (file)
@@ -120,9 +120,9 @@ string Message::str_common() const
 {
        string result;
 
-       for(HeaderMap::const_iterator i=headers.begin(); i!=headers.end(); ++i)
-               if(i->first[0]!='-')
-                       result += format("%s: %s\r\n", i->first, i->second);
+       for(auto &kvp: headers)
+               if(kvp.first[0]!='-')
+                       result += format("%s: %s\r\n", kvp.first, kvp.second);
        result += "\r\n";
        result += content;
 
@@ -133,17 +133,17 @@ string Message::normalize_header_name(const string &hdr) const
 {
        string result = hdr;
        bool upper = true;
-       for(string::iterator i=result.begin(); i!=result.end(); ++i)
+       for(char &c: result)
        {
-               if(*i=='-')
+               if(c=='-')
                        upper = true;
                else if(upper)
                {
-                       *i = toupper(*i);
+                       c = toupper(static_cast<unsigned char>(c));
                        upper = false;
                }
                else
-                       *i = tolower(*i);
+                       c = tolower(static_cast<unsigned char>(c));
        }
        return result;
 }
index 667403e86788c9228e179e1f9677feb1552fb6cc..6055f008e4ff13328a6bf5409584a7e96f682458 100644 (file)
@@ -54,15 +54,15 @@ void Server::use_event_dispatcher(IO::EventDispatcher *ed)
        if(event_disp)
        {
                event_disp->remove(sock);
-               for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
-                       event_disp->remove(*i->sock);
+               for(Client &c: clients)
+                       event_disp->remove(*c.sock);
        }
        event_disp = ed;
        if(event_disp)
        {
                event_disp->add(sock);
-               for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
-                       event_disp->add(*i->sock);
+               for(Client &c: clients)
+                       event_disp->add(*c.sock);
        }
 }
 
@@ -86,18 +86,17 @@ void Server::cancel_keepalive(Response &resp)
 void Server::close_connections(const Time::TimeDelta &timeout)
 {
        IO::Poller poller;
-       for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+       for(Client &c: clients)
        {
-               i->sock->shutdown(IO::M_WRITE);
-               poller.set_object(*i->sock, IO::P_INPUT);
+               c.sock->shutdown(IO::M_WRITE);
+               poller.set_object(*c.sock, IO::P_INPUT);
        }
 
        while(!clients.empty() && poller.poll(timeout))
        {
-               const vector<IO::Poller::PolledObject> &result = poller.get_result();
-               for(vector<IO::Poller::PolledObject>::const_iterator i=result.begin(); i!=result.end(); ++i)
+               for(const IO::Poller::PolledObject &p: poller.get_result())
                        for(list<Client>::iterator j=clients.begin(); j!=clients.end(); ++j)
-                               if(j->sock.get()==i->object)
+                               if(j->sock.get()==p.object)
                                {
                                        poller.set_object(*j->sock, IO::P_NONE);
                                        clients.erase(j);
index e52309aae50aab02648a1f1278d6787027063e74..2f13576166ba9b128f0a111a1e507a8cb645fb96 100644 (file)
@@ -31,12 +31,12 @@ namespace Http {
 string urlencode(const string &str, EncodeLevel level)
 {
        string result;
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(char c: str)
        {
-               if(is_reserved(*i, level))
-                       result += format("%%%02X", *i);
+               if(is_reserved(c, level))
+                       result += format("%%%02X", c);
                else
-                       result += *i;
+                       result += c;
        }
        return result;
 }
@@ -44,14 +44,14 @@ string urlencode(const string &str, EncodeLevel level)
 string urlencode_plus(const string &str, EncodeLevel level)
 {
        string result;
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(char c: str)
        {
-               if(*i==' ')
+               if(c==' ')
                        result += '+';
-               else if(is_reserved(*i, level))
-                       result += format("%%%02X", *i);
+               else if(is_reserved(c, level))
+                       result += format("%%%02X", c);
                else
-                       result += *i;
+                       result += c;
        }
        return result;
 }
@@ -119,14 +119,13 @@ string build_url(const Url &url)
 
 Query parse_query(const std::string &str)
 {
-       vector<string> parts = split(str, '&');
        Query query;
-       for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+       for(const string &p: split(str, '&'))
        {
-               string::size_type equals = i->find('=');
-               string &value = query[urldecode(i->substr(0, equals))];
+               string::size_type equals = p.find('=');
+               string &value = query[urldecode(p.substr(0, equals))];
                if(equals!=string::npos)
-                       value = urldecode(i->substr(equals+1));
+                       value = urldecode(p.substr(equals+1));
        }
        return query;
 }
@@ -134,13 +133,13 @@ Query parse_query(const std::string &str)
 string build_query(const Query &query)
 {
        string str;
-       for(Query::const_iterator i=query.begin(); i!=query.end(); ++i)
+       for(const auto &kvp: query)
        {
-               if(i!=query.begin())
+               if(!str.empty())
                        str += '&';
-               str += urlencode_plus(i->first);
+               str += urlencode_plus(kvp.first);
                str += '=';
-               str += urlencode_plus(i->second);
+               str += urlencode_plus(kvp.second);
        }
        return str;
 }
index db663821c370cef914676eb1da698cdb473f5c50..9cba6382d307dcae861d3f4298c76ee4f86ac09b 100644 (file)
@@ -21,8 +21,8 @@ Protocol::Protocol(unsigned npi):
 
 Protocol::~Protocol()
 {
-       for(map<unsigned, PacketDefBase *>::iterator i=packet_class_defs.begin(); i!=packet_class_defs.end(); ++i)
-               delete i->second;
+       for(auto &kvp: packet_class_defs)
+               delete kvp.second;
 }
 
 unsigned Protocol::get_next_packet_class_id()
@@ -77,8 +77,8 @@ size_t Protocol::get_packet_size(const char *buf, size_t size) const
 uint64_t Protocol::get_hash() const
 {
        string description;
-       for(PacketMap::const_iterator i=packet_id_defs.begin(); i!=packet_id_defs.end(); ++i)
-               description += format("%d:%s\n", i->first, i->second->describe());
+       for(auto &kvp: packet_id_defs)
+               description += format("%d:%s\n", kvp.first, kvp.second->describe());
        return hash<64>(description);
 }
 
index ff18e85f905b83852a3dba5f5816032e4ae3b2da..9f09fe5a1c7e9eda0ece48162254c8f5484fa80f 100644 (file)
@@ -401,8 +401,8 @@ template<typename A>
 char *Protocol::ArraySerializer<A>::serialize(const A &array, char *buf, char *end) const
 {
        buf = length_serializer.serialize(array.size(), buf, end);
-       for(typename A::const_iterator i=array.begin(); i!=array.end(); ++i)
-               buf = element_serializer.serialize(*i, buf, end);
+       for(const auto &e: array)
+               buf = element_serializer.serialize(e, buf, end);
        return buf;
 }