@@ -506,12 +506,11 @@ static inline unsigned hex2bin(const T ch) {
506
506
return static_cast <unsigned >(-1 );
507
507
}
508
508
509
- static inline void PercentDecode (const char * input,
510
- size_t len,
511
- std::string* dest) {
509
+ inline std::string PercentDecode (const char * input, size_t len) {
510
+ std::string dest;
512
511
if (len == 0 )
513
- return ;
514
- dest-> reserve (len);
512
+ return dest ;
513
+ dest. reserve (len);
515
514
const char * pointer = input;
516
515
const char * end = input + len;
517
516
@@ -522,17 +521,18 @@ static inline void PercentDecode(const char* input,
522
521
(ch == ' %' &&
523
522
(!IsASCIIHexDigit (pointer[1 ]) ||
524
523
!IsASCIIHexDigit (pointer[2 ])))) {
525
- * dest += ch;
524
+ dest += ch;
526
525
pointer++;
527
526
continue ;
528
527
} else {
529
528
unsigned a = hex2bin (pointer[1 ]);
530
529
unsigned b = hex2bin (pointer[2 ]);
531
530
char c = static_cast <char >(a * 16 + b);
532
- * dest += c;
531
+ dest += c;
533
532
pointer += 3 ;
534
533
}
535
534
}
535
+ return dest;
536
536
}
537
537
538
538
#define SPECIALS (XX ) \
@@ -860,7 +860,7 @@ static url_host_type ParseHost(url_host* host,
860
860
return ParseOpaqueHost (host, input, length);
861
861
862
862
// First, we have to percent decode
863
- PercentDecode (input, length, &decoded );
863
+ decoded = PercentDecode (input, length);
864
864
865
865
// Then we have to punycode toASCII
866
866
if (!ToASCII (decoded, &decoded))
@@ -894,13 +894,13 @@ static url_host_type ParseHost(url_host* host,
894
894
895
895
// Locates the longest sequence of 0 segments in an IPv6 address
896
896
// in order to use the :: compression when serializing
897
- static inline uint16_t * FindLongestZeroSequence ( uint16_t * values,
898
- size_t len) {
899
- uint16_t * start = values;
900
- uint16_t * end = start + len;
901
- uint16_t * result = nullptr ;
897
+ template < typename T>
898
+ static inline T* FindLongestZeroSequence (T* values, size_t len) {
899
+ T * start = values;
900
+ T * end = start + len;
901
+ T * result = nullptr ;
902
902
903
- uint16_t * current = nullptr ;
903
+ T * current = nullptr ;
904
904
unsigned counter = 0 , longest = 1 ;
905
905
906
906
while (start < end) {
@@ -923,7 +923,7 @@ static inline uint16_t* FindLongestZeroSequence(uint16_t* values,
923
923
return result;
924
924
}
925
925
926
- static url_host_type WriteHost (url_host* host, std::string* dest) {
926
+ static url_host_type WriteHost (const url_host* host, std::string* dest) {
927
927
dest->clear ();
928
928
switch (host->type ) {
929
929
case HOST_TYPE_DOMAIN:
@@ -934,8 +934,7 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
934
934
uint32_t value = host->value .ipv4 ;
935
935
for (int n = 0 ; n < 4 ; n++) {
936
936
char buf[4 ];
937
- char * buffer = buf;
938
- snprintf (buffer, sizeof (buf), " %d" , value % 256 );
937
+ snprintf (buf, sizeof (buf), " %d" , value % 256 );
939
938
dest->insert (0 , buf);
940
939
if (n < 3 )
941
940
dest->insert (0 , 1 , ' .' );
@@ -946,12 +945,12 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
946
945
case HOST_TYPE_IPV6: {
947
946
dest->reserve (41 );
948
947
*dest+= ' [' ;
949
- uint16_t * start = &host->value .ipv6 [0 ];
950
- uint16_t * compress_pointer =
948
+ const uint16_t * start = &host->value .ipv6 [0 ];
949
+ const uint16_t * compress_pointer =
951
950
FindLongestZeroSequence (start, 8 );
952
951
bool ignore0 = false ;
953
952
for (int n = 0 ; n <= 7 ; n++) {
954
- uint16_t * piece = &host->value .ipv6 [n];
953
+ const uint16_t * piece = &host->value .ipv6 [n];
955
954
if (ignore0 && *piece == 0 )
956
955
continue ;
957
956
else if (ignore0)
@@ -962,8 +961,7 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
962
961
continue ;
963
962
}
964
963
char buf[5 ];
965
- char * buffer = buf;
966
- snprintf (buffer, sizeof (buf), " %x" , *piece);
964
+ snprintf (buf, sizeof (buf), " %x" , *piece);
967
965
*dest += buf;
968
966
if (n < 7 )
969
967
*dest += ' :' ;
@@ -980,16 +978,16 @@ static url_host_type WriteHost(url_host* host, std::string* dest) {
980
978
return host->type ;
981
979
}
982
980
983
- static bool ParseHost (std::string* input,
981
+ static bool ParseHost (const std::string& input,
984
982
std::string* output,
985
983
bool is_special,
986
984
bool unicode = false ) {
987
- if (input-> length () == 0 ) {
985
+ if (input. length () == 0 ) {
988
986
output->clear ();
989
987
return true ;
990
988
}
991
989
url_host host{{" " }, HOST_TYPE_DOMAIN};
992
- ParseHost (&host, input-> c_str (), input-> length (), is_special, unicode);
990
+ ParseHost (&host, input. c_str (), input. length (), is_special, unicode);
993
991
if (host.type == HOST_TYPE_FAILED)
994
992
return false ;
995
993
WriteHost (&host, output);
@@ -1092,7 +1090,7 @@ static inline void HarvestContext(Environment* env,
1092
1090
}
1093
1091
1094
1092
// Single dot segment can be ".", "%2e", or "%2E"
1095
- static inline bool IsSingleDotSegment (std::string str) {
1093
+ static inline bool IsSingleDotSegment (const std::string& str) {
1096
1094
switch (str.size ()) {
1097
1095
case 1 :
1098
1096
return str == " ." ;
@@ -1108,7 +1106,7 @@ static inline bool IsSingleDotSegment(std::string str) {
1108
1106
// Double dot segment can be:
1109
1107
// "..", ".%2e", ".%2E", "%2e.", "%2E.",
1110
1108
// "%2e%2e", "%2E%2E", "%2e%2E", or "%2E%2e"
1111
- static inline bool IsDoubleDotSegment (std::string str) {
1109
+ static inline bool IsDoubleDotSegment (const std::string& str) {
1112
1110
switch (str.size ()) {
1113
1111
case 2 :
1114
1112
return str == " .." ;
@@ -1542,7 +1540,7 @@ void URL::Parse(const char* input,
1542
1540
return ;
1543
1541
}
1544
1542
url->flags |= URL_FLAGS_HAS_HOST;
1545
- if (!ParseHost (& buffer, &url->host , special)) {
1543
+ if (!ParseHost (buffer, &url->host , special)) {
1546
1544
url->flags |= URL_FLAGS_FAILED;
1547
1545
return ;
1548
1546
}
@@ -1569,7 +1567,7 @@ void URL::Parse(const char* input,
1569
1567
return ;
1570
1568
}
1571
1569
url->flags |= URL_FLAGS_HAS_HOST;
1572
- if (!ParseHost (& buffer, &url->host , special)) {
1570
+ if (!ParseHost (buffer, &url->host , special)) {
1573
1571
url->flags |= URL_FLAGS_FAILED;
1574
1572
return ;
1575
1573
}
@@ -1741,7 +1739,7 @@ void URL::Parse(const char* input,
1741
1739
state = kPathStart ;
1742
1740
} else {
1743
1741
std::string host;
1744
- if (!ParseHost (& buffer, &host, special)) {
1742
+ if (!ParseHost (buffer, &host, special)) {
1745
1743
url->flags |= URL_FLAGS_FAILED;
1746
1744
return ;
1747
1745
}
@@ -2104,8 +2102,7 @@ std::string URL::ToFilePath() const {
2104
2102
#endif
2105
2103
std::string decoded_path;
2106
2104
for (const std::string& part : context_.path ) {
2107
- std::string decoded;
2108
- PercentDecode (part.c_str (), part.length (), &decoded);
2105
+ std::string decoded = PercentDecode (part.c_str (), part.length ());
2109
2106
for (char & ch : decoded) {
2110
2107
if (is_slash (ch)) {
2111
2108
return " " ;
0 commit comments