Chromium Code Reviews| Index: net/base/escape.cc |
| diff --git a/net/base/escape.cc b/net/base/escape.cc |
| index ab70f1db30187e6a28e09757819d2f307291fd53..26eaaa225f3ef6f8b8bce5b38b55981f86486aa9 100644 |
| --- a/net/base/escape.cc |
| +++ b/net/base/escape.cc |
| @@ -40,7 +40,8 @@ struct Charmap { |
| // return an escaped string. If use_plus is true, spaces are converted |
| // to +, otherwise, if spaces are in the charmap, they are converted to |
| // %20. |
| -std::string Escape(const std::string& text, const Charmap& charmap, |
| +std::string Escape(const std::string& text, |
| + const Charmap& charmap, |
| bool use_plus) { |
| std::string escaped; |
| escaped.reserve(text.length() * 3); |
| @@ -80,27 +81,26 @@ std::string Escape(const std::string& text, const Charmap& charmap, |
| // not unescaped, to avoid turning a valid url according to spec into an |
| // invalid one. |
| const char kUrlUnescape[128] = { |
| -// NULL, control chars... |
| - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| -// ' ' ! " # $ % & ' ( ) * + , - . / |
| - 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, |
| -// 0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, |
| -// @ A B C D E F G H I J K L M N O |
| - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| -// P Q R S T U V W X Y Z [ \ ] ^ _ |
| - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| -// ` a b c d e f g h i j k l m n o |
| - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| -// p q r s t u v w x y z { | } ~ <NBSP> |
| - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 |
| -}; |
| + // NULL, control chars... |
| + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| + // ' ' ! " # $ % & ' ( ) * + , - . / |
| + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, |
| + // 0 1 2 3 4 5 6 7 8 9 : ; < = > ? |
| + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, |
| + // @ A B C D E F G H I J K L M N O |
| + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| + // P Q R S T U V W X Y Z [ \ ] ^ _ |
| + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| + // ` a b c d e f g h i j k l m n o |
| + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| + // p q r s t u v w x y z { | } ~ <NBSP> |
|
Ryan Sleevi
2014/10/11 02:04:47
Seems like we should file a bug for this.
jkarlin
2014/10/16 00:19:32
I'm not sure what clang-format could do better her
jkarlin
2014/11/11 19:01:43
Fixed by wrapping with clang-format off
|
| + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0}; |
| // Attempts to unescape the sequence at |index| within |escaped_text|. If |
| // successful, sets |value| to the unescaped value. Returns whether |
| // unescaping succeeded. |
| -template<typename STR> |
| +template <typename STR> |
| bool UnescapeUnsignedCharAtIndex(const STR& escaped_text, |
| size_t index, |
| unsigned char* value) { |
| @@ -113,8 +113,8 @@ bool UnescapeUnsignedCharAtIndex(const STR& escaped_text, |
| const typename STR::value_type least_sig_digit( |
| static_cast<typename STR::value_type>(escaped_text[index + 2])); |
| if (IsHexDigit(most_sig_digit) && IsHexDigit(least_sig_digit)) { |
| - *value = HexDigitToInt(most_sig_digit) * 16 + |
| - HexDigitToInt(least_sig_digit); |
| + *value = |
| + HexDigitToInt(most_sig_digit) * 16 + HexDigitToInt(least_sig_digit); |
| return true; |
| } |
| return false; |
| @@ -125,7 +125,7 @@ bool UnescapeUnsignedCharAtIndex(const STR& escaped_text, |
| // the alterations done to the string that are not one-character-to-one- |
| // character. The resulting |adjustments| will always be sorted by increasing |
| // offset. |
| -template<typename STR> |
| +template <typename STR> |
| STR UnescapeURLWithAdjustmentsImpl( |
| const STR& escaped_text, |
| UnescapeRule::Type rules, |
| @@ -189,10 +189,10 @@ STR UnescapeURLWithAdjustmentsImpl( |
| ((second_byte == 0x80) || (second_byte == 0x81))) { |
| unsigned char third_byte; |
| if (UnescapeUnsignedCharAtIndex(escaped_text, i + 6, &third_byte) && |
| - ((second_byte == 0x80) ? |
| - ((third_byte == 0x8E) || (third_byte == 0x8F) || |
| - ((third_byte >= 0xAA) && (third_byte <= 0xAE))) : |
| - ((third_byte >= 0xA6) && (third_byte <= 0xA9)))) { |
| + ((second_byte == 0x80) |
| + ? ((third_byte == 0x8E) || (third_byte == 0x8F) || |
| + ((third_byte >= 0xAA) && (third_byte <= 0xAE))) |
| + : ((third_byte >= 0xA6) && (third_byte <= 0xA9)))) { |
|
Ryan Sleevi
2014/10/11 02:04:46
This is something that annoys rvargas@ and pkastin
jkarlin
2014/10/16 00:19:32
Hmm, the style guide specifically says binary oper
Peter Kasting
2014/10/30 20:20:59
We meant only to exclude unary operators. dcheng
|
| result.append(escaped_text, i, 9); |
| i += 8; |
| continue; |
| @@ -237,12 +237,12 @@ void AppendEscapedCharForHTMLImpl(typename str::value_type c, str* output) { |
| char key; |
| const char* replacement; |
| } kCharsToEscape[] = { |
| - { '<', "<" }, |
| - { '>', ">" }, |
| - { '&', "&" }, |
| - { '"', """ }, |
| - { '\'', "'" }, |
| - }; |
| + {'<', "<"}, |
| + {'>', ">"}, |
| + {'&', "&"}, |
| + {'"', """}, |
| + {'\'', "'"}, |
| + }; |
| size_t k; |
| for (k = 0; k < ARRAYSIZE_UNSAFE(kCharsToEscape); ++k) { |
| if (c == kCharsToEscape[k].key) { |
| @@ -269,35 +269,55 @@ str EscapeForHTMLImpl(const str& input) { |
| // Everything except alphanumerics and !'()*-._~ |
| // See RFC 2396 for the list of reserved characters. |
| -static const Charmap kQueryCharmap = {{ |
| - 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L, |
| - 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| -}}; |
| +static const Charmap kQueryCharmap = {{0xffffffffL, |
| + 0xfc00987dL, |
| + 0x78000001L, |
| + 0xb8000001L, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL}}; |
|
Ryan Sleevi
2014/10/11 02:04:46
Oof. This formatting is awful. Might be worth a bu
|
| // non-printable, non-7bit, and (including space) "#%:<>?[\]^`{|} |
| -static const Charmap kPathCharmap = {{ |
| - 0xffffffffL, 0xd400002dL, 0x78000000L, 0xb8000001L, |
| - 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| -}}; |
| +static const Charmap kPathCharmap = {{0xffffffffL, |
| + 0xd400002dL, |
| + 0x78000000L, |
| + 0xb8000001L, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL}}; |
| // non-printable, non-7bit, and (including space) ?>=<;+'&%$#"![\]^`{|} |
| -static const Charmap kUrlEscape = {{ |
| - 0xffffffffL, 0xf80008fdL, 0x78000001L, 0xb8000001L, |
| - 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| -}}; |
| +static const Charmap kUrlEscape = {{0xffffffffL, |
| + 0xf80008fdL, |
| + 0x78000001L, |
| + 0xb8000001L, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL}}; |
| // non-7bit |
| -static const Charmap kNonASCIICharmap = {{ |
| - 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, |
| - 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| -}}; |
| +static const Charmap kNonASCIICharmap = {{0x00000000L, |
| + 0x00000000L, |
| + 0x00000000L, |
| + 0x00000000L, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL}}; |
| // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and |
| // !'()*-._~% |
| -static const Charmap kExternalHandlerCharmap = {{ |
| - 0xffffffffL, 0x5000080dL, 0x68000000L, 0xb8000001L, |
| - 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| -}}; |
| +static const Charmap kExternalHandlerCharmap = {{0xffffffffL, |
| + 0x5000080dL, |
| + 0x68000000L, |
| + 0xb8000001L, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL, |
| + 0xffffffffL}}; |
| } // namespace |
| @@ -354,11 +374,10 @@ base::string16 UnescapeAndDecodeUTF8URLComponentWithAdjustments( |
| base::OffsetAdjuster::Adjustments* adjustments) { |
| base::string16 result; |
| base::OffsetAdjuster::Adjustments unescape_adjustments; |
| - std::string unescaped_url(UnescapeURLWithAdjustmentsImpl( |
| - text, rules, &unescape_adjustments)); |
| - if (base::UTF8ToUTF16WithAdjustments(unescaped_url.data(), |
| - unescaped_url.length(), |
| - &result, adjustments)) { |
| + std::string unescaped_url( |
| + UnescapeURLWithAdjustmentsImpl(text, rules, &unescape_adjustments)); |
| + if (base::UTF8ToUTF16WithAdjustments( |
| + unescaped_url.data(), unescaped_url.length(), &result, adjustments)) { |
| // Character set looks like it's valid. |
| if (adjustments) { |
| base::OffsetAdjuster::MergeSequentialAdjustments(unescape_adjustments, |
| @@ -375,20 +394,20 @@ base::string16 UnescapeForHTML(const base::string16& input) { |
| const char* ampersand_code; |
| const char replacement; |
| } kEscapeToChars[] = { |
| - { "<", '<' }, |
| - { ">", '>' }, |
| - { "&", '&' }, |
| - { """, '"' }, |
| - { "'", '\''}, |
| - }; |
| + {"<", '<'}, |
| + {">", '>'}, |
| + {"&", '&'}, |
| + {""", '"'}, |
| + {"'", '\''}, |
| + }; |
| if (input.find(base::ASCIIToUTF16("&")) == std::string::npos) |
| return input; |
| base::string16 ampersand_chars[ARRAYSIZE_UNSAFE(kEscapeToChars)]; |
| base::string16 text(input); |
| - for (base::string16::iterator iter = text.begin(); |
| - iter != text.end(); ++iter) { |
| + for (base::string16::iterator iter = text.begin(); iter != text.end(); |
| + ++iter) { |
| if (*iter == '&') { |
| // Potential ampersand encode char. |
| size_t index = iter - text.begin(); |
| @@ -398,8 +417,10 @@ base::string16 UnescapeForHTML(const base::string16& input) { |
| base::ASCIIToUTF16(kEscapeToChars[i].ampersand_code); |
| } |
| if (text.find(ampersand_chars[i], index) == index) { |
| - text.replace(iter, iter + ampersand_chars[i].length(), |
| - 1, kEscapeToChars[i].replacement); |
| + text.replace(iter, |
| + iter + ampersand_chars[i].length(), |
| + 1, |
| + kEscapeToChars[i].replacement); |
| break; |
| } |
| } |