Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/json/string_escape.h" | 5 #include "base/json/string_escape.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/stringprintf.h" | |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, | 16 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, |
| 16 // returns true and appends the escape sequence to |dst|. This isn't required | 17 // returns true and appends the escape sequence to |dst|. This isn't required |
| 17 // by the spec, but it's more readable by humans than the \uXXXX alternatives. | 18 // by the spec, but it's more readable by humans than the \uXXXX alternatives. |
| 18 template<typename CHAR> | 19 template<typename CHAR> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 dst->push_back('"'); | 57 dst->push_back('"'); |
| 57 | 58 |
| 58 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) { | 59 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) { |
| 59 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it; | 60 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it; |
| 60 if (!JsonSingleEscapeChar(c, dst)) { | 61 if (!JsonSingleEscapeChar(c, dst)) { |
| 61 if (c < 32 || c > 126 || c == '<' || c == '>') { | 62 if (c < 32 || c > 126 || c == '<' || c == '>') { |
| 62 // 1. Escaping <, > to prevent script execution. | 63 // 1. Escaping <, > to prevent script execution. |
| 63 // 2. Technically, we could also pass through c > 126 as UTF8, but this | 64 // 2. Technically, we could also pass through c > 126 as UTF8, but this |
| 64 // is also optional. It would also be a pain to implement here. | 65 // is also optional. It would also be a pain to implement here. |
| 65 unsigned int as_uint = static_cast<unsigned int>(c); | 66 unsigned int as_uint = static_cast<unsigned int>(c); |
| 66 StringAppendF(dst, "\\u%04X", as_uint); | 67 base::StringAppendF(dst, "\\u%04X", as_uint); |
|
Nico
2011/05/10 02:57:51
hm, these are not necessary because of ADL. does i
James Hawkins
2011/05/10 04:01:43
No, I added them. I prefer them, but I don't thin
| |
| 67 } else { | 68 } else { |
| 68 unsigned char ascii = static_cast<unsigned char>(*it); | 69 unsigned char ascii = static_cast<unsigned char>(*it); |
| 69 dst->push_back(ascii); | 70 dst->push_back(ascii); |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 74 if (put_in_quotes) | 75 if (put_in_quotes) |
| 75 dst->push_back('"'); | 76 dst->push_back('"'); |
| 76 } | 77 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 95 JsonDoubleQuoteT(str, put_in_quotes, dst); | 96 JsonDoubleQuoteT(str, put_in_quotes, dst); |
| 96 } | 97 } |
| 97 | 98 |
| 98 std::string GetDoubleQuotedJson(const string16& str) { | 99 std::string GetDoubleQuotedJson(const string16& str) { |
| 99 std::string dst; | 100 std::string dst; |
| 100 JsonDoubleQuote(str, true, &dst); | 101 JsonDoubleQuote(str, true, &dst); |
| 101 return dst; | 102 return dst; |
| 102 } | 103 } |
| 103 | 104 |
| 104 } // namespace base | 105 } // namespace base |
| OLD | NEW |