]> git.tdb.fi Git - libs/net.git/blobdiff - source/http/utils.cpp
Prefer range-based for loops where possible
[libs/net.git] / source / http / utils.cpp
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;
 }