OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/escape.h" | 5 #include "net/base/escape.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 char key; | 237 char key; |
238 const char* replacement; | 238 const char* replacement; |
239 } kCharsToEscape[] = { | 239 } kCharsToEscape[] = { |
240 { '<', "<" }, | 240 { '<', "<" }, |
241 { '>', ">" }, | 241 { '>', ">" }, |
242 { '&', "&" }, | 242 { '&', "&" }, |
243 { '"', """ }, | 243 { '"', """ }, |
244 { '\'', "'" }, | 244 { '\'', "'" }, |
245 }; | 245 }; |
246 size_t k; | 246 size_t k; |
247 for (k = 0; k < ARRAYSIZE_UNSAFE(kCharsToEscape); ++k) { | 247 for (k = 0; k < arraysize(kCharsToEscape); ++k) { |
248 if (c == kCharsToEscape[k].key) { | 248 if (c == kCharsToEscape[k].key) { |
249 const char* p = kCharsToEscape[k].replacement; | 249 const char* p = kCharsToEscape[k].replacement; |
250 while (*p) | 250 while (*p) |
251 output->push_back(*p++); | 251 output->push_back(*p++); |
252 break; | 252 break; |
253 } | 253 } |
254 } | 254 } |
255 if (k == ARRAYSIZE_UNSAFE(kCharsToEscape)) | 255 if (k == arraysize(kCharsToEscape)) |
256 output->push_back(c); | 256 output->push_back(c); |
257 } | 257 } |
258 | 258 |
259 template <class str> | 259 template <class str> |
260 str EscapeForHTMLImpl(const str& input) { | 260 str EscapeForHTMLImpl(const str& input) { |
261 str result; | 261 str result; |
262 result.reserve(input.size()); // Optimize for no escaping. | 262 result.reserve(input.size()); // Optimize for no escaping. |
263 | 263 |
264 for (typename str::const_iterator i = input.begin(); i != input.end(); ++i) | 264 for (typename str::const_iterator i = input.begin(); i != input.end(); ++i) |
265 AppendEscapedCharForHTMLImpl(*i, &result); | 265 AppendEscapedCharForHTMLImpl(*i, &result); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 { "<", '<' }, | 378 { "<", '<' }, |
379 { ">", '>' }, | 379 { ">", '>' }, |
380 { "&", '&' }, | 380 { "&", '&' }, |
381 { """, '"' }, | 381 { """, '"' }, |
382 { "'", '\''}, | 382 { "'", '\''}, |
383 }; | 383 }; |
384 | 384 |
385 if (input.find(base::ASCIIToUTF16("&")) == std::string::npos) | 385 if (input.find(base::ASCIIToUTF16("&")) == std::string::npos) |
386 return input; | 386 return input; |
387 | 387 |
388 base::string16 ampersand_chars[ARRAYSIZE_UNSAFE(kEscapeToChars)]; | 388 base::string16 ampersand_chars[arraysize(kEscapeToChars)]; |
389 base::string16 text(input); | 389 base::string16 text(input); |
390 for (base::string16::iterator iter = text.begin(); | 390 for (base::string16::iterator iter = text.begin(); |
391 iter != text.end(); ++iter) { | 391 iter != text.end(); ++iter) { |
392 if (*iter == '&') { | 392 if (*iter == '&') { |
393 // Potential ampersand encode char. | 393 // Potential ampersand encode char. |
394 size_t index = iter - text.begin(); | 394 size_t index = iter - text.begin(); |
395 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kEscapeToChars); i++) { | 395 for (size_t i = 0; i < arraysize(kEscapeToChars); i++) { |
396 if (ampersand_chars[i].empty()) { | 396 if (ampersand_chars[i].empty()) { |
397 ampersand_chars[i] = | 397 ampersand_chars[i] = |
398 base::ASCIIToUTF16(kEscapeToChars[i].ampersand_code); | 398 base::ASCIIToUTF16(kEscapeToChars[i].ampersand_code); |
399 } | 399 } |
400 if (text.find(ampersand_chars[i], index) == index) { | 400 if (text.find(ampersand_chars[i], index) == index) { |
401 text.replace(iter, iter + ampersand_chars[i].length(), | 401 text.replace(iter, iter + ampersand_chars[i].length(), |
402 1, kEscapeToChars[i].replacement); | 402 1, kEscapeToChars[i].replacement); |
403 break; | 403 break; |
404 } | 404 } |
405 } | 405 } |
406 } | 406 } |
407 } | 407 } |
408 return text; | 408 return text; |
409 } | 409 } |
410 | 410 |
411 } // namespace net | 411 } // namespace net |
OLD | NEW |