From: Mikko Rasa Date: Fri, 9 Dec 2022 22:18:32 +0000 (+0200) Subject: Prefer range-based for loops where possible X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=62ea6954a91568c900ad8155999bea264dfa72f3;p=libs%2Fnet.git Prefer range-based for loops where possible --- diff --git a/source/http/message.cpp b/source/http/message.cpp index b86f954..2ae0ff8 100644 --- a/source/http/message.cpp +++ b/source/http/message.cpp @@ -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(c)); upper = false; } else - *i = tolower(*i); + c = tolower(static_cast(c)); } return result; } diff --git a/source/http/server.cpp b/source/http/server.cpp index 667403e..6055f00 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -54,15 +54,15 @@ void Server::use_event_dispatcher(IO::EventDispatcher *ed) if(event_disp) { event_disp->remove(sock); - for(list::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::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::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 &result = poller.get_result(); - for(vector::const_iterator i=result.begin(); i!=result.end(); ++i) + for(const IO::Poller::PolledObject &p: poller.get_result()) for(list::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); diff --git a/source/http/utils.cpp b/source/http/utils.cpp index e52309a..2f13576 100644 --- a/source/http/utils.cpp +++ b/source/http/utils.cpp @@ -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 parts = split(str, '&'); Query query; - for(vector::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; } diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index db66382..9cba638 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -21,8 +21,8 @@ Protocol::Protocol(unsigned npi): Protocol::~Protocol() { - for(map::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); } diff --git a/source/net/protocol.h b/source/net/protocol.h index ff18e85..9f09fe5 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -401,8 +401,8 @@ template char *Protocol::ArraySerializer::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; }