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/string_escape.h" | 5 #include "base/string_escape.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 | 10 |
11 namespace string_escape { | 11 namespace string_escape { |
12 | 12 |
13 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, | 13 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, |
14 // returns true and appends the escape sequence to |dst|. | 14 // returns true and appends the escape sequence to |dst|. This isn't required |
| 15 // by the spec, but it's more readable by humans than the \uXXXX alternatives. |
15 template<typename CHAR> | 16 template<typename CHAR> |
16 static bool JavascriptSingleEscapeChar(const CHAR c, std::string* dst) { | 17 static bool JsonSingleEscapeChar(const CHAR c, std::string* dst) { |
17 // WARNING: if you add a new case here, you need to update the reader as well. | 18 // WARNING: if you add a new case here, you need to update the reader as well. |
| 19 // Note: \v is in the reader, but not here since the JSON spec doesn't |
| 20 // allow it. |
18 switch (c) { | 21 switch (c) { |
19 case '\b': | 22 case '\b': |
20 dst->append("\\b"); | 23 dst->append("\\b"); |
21 break; | 24 break; |
22 case '\f': | 25 case '\f': |
23 dst->append("\\f"); | 26 dst->append("\\f"); |
24 break; | 27 break; |
25 case '\n': | 28 case '\n': |
26 dst->append("\\n"); | 29 dst->append("\\n"); |
27 break; | 30 break; |
28 case '\r': | 31 case '\r': |
29 dst->append("\\r"); | 32 dst->append("\\r"); |
30 break; | 33 break; |
31 case '\t': | 34 case '\t': |
32 dst->append("\\t"); | 35 dst->append("\\t"); |
33 break; | 36 break; |
34 case '\v': | |
35 dst->append("\\v"); | |
36 break; | |
37 case '\\': | 37 case '\\': |
38 dst->append("\\\\"); | 38 dst->append("\\\\"); |
39 break; | 39 break; |
40 case '"': | 40 case '"': |
41 dst->append("\\\""); | 41 dst->append("\\\""); |
42 break; | 42 break; |
43 default: | 43 default: |
44 return false; | 44 return false; |
45 } | 45 } |
46 return true; | 46 return true; |
47 } | 47 } |
48 | 48 |
49 void JavascriptDoubleQuote(const string16& str, | 49 template <class STR> |
50 bool put_in_quotes, | 50 void JsonDoubleQuoteT(const STR& str, |
51 std::string* dst) { | 51 bool put_in_quotes, |
| 52 std::string* dst) { |
52 if (put_in_quotes) | 53 if (put_in_quotes) |
53 dst->push_back('"'); | 54 dst->push_back('"'); |
54 | 55 |
55 for (string16::const_iterator it = str.begin(); it != str.end(); ++it) { | 56 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) { |
56 char16 c = *it; | 57 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it; |
57 if (!JavascriptSingleEscapeChar(c, dst)) { | 58 if (!JsonSingleEscapeChar(c, dst)) { |
58 if (c > 255) { | 59 if (c < 32 || c > 126) { |
59 // Non-ascii values need to be unicode dst-> | 60 // Technically, we could also pass through c > 126 as UTF8, but this is |
60 // TODO(tc): Some unicode values are handled specially. See | 61 // also optional. It would also be a pain to implement here. |
61 // spidermonkey code. | 62 unsigned int as_uint = static_cast<unsigned int>(c); |
62 StringAppendF(dst, "\\u%04X", c); | 63 StringAppendF(dst, "\\u%04X", as_uint); |
63 } else if (c < 32 || c > 126) { | |
64 // Spidermonkey hex escapes these values. | |
65 StringAppendF(dst, "\\x%02X", c); | |
66 } else { | 64 } else { |
67 dst->push_back(static_cast<char>(c)); | 65 unsigned char ascii = static_cast<unsigned char>(*it); |
| 66 dst->push_back(ascii); |
68 } | 67 } |
69 } | 68 } |
70 } | 69 } |
71 | 70 |
72 if (put_in_quotes) | 71 if (put_in_quotes) |
73 dst->push_back('"'); | 72 dst->push_back('"'); |
74 } | 73 } |
75 | 74 |
76 void JavascriptDoubleQuote(const std::string& str, | 75 void JsonDoubleQuote(const std::string& str, |
77 bool put_in_quotes, | 76 bool put_in_quotes, |
78 std::string* dst) { | 77 std::string* dst) { |
79 if (put_in_quotes) | 78 JsonDoubleQuoteT(str, put_in_quotes, dst); |
80 dst->push_back('"'); | 79 } |
81 | 80 |
82 for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) { | 81 void JsonDoubleQuote(const string16& str, |
83 unsigned char c = *it; | 82 bool put_in_quotes, |
84 if (!JavascriptSingleEscapeChar(c, dst)) { | 83 std::string* dst) { |
85 // Hex encode if the character is non-printable 7bit ascii | 84 JsonDoubleQuoteT(str, put_in_quotes, dst); |
86 if (c < 32 || c == 127) { | |
87 StringAppendF(dst, "\\x%02X", c); | |
88 } else { | |
89 dst->push_back(static_cast<char>(c)); | |
90 } | |
91 } | |
92 } | |
93 | |
94 if (put_in_quotes) | |
95 dst->push_back('"'); | |
96 } | 85 } |
97 | 86 |
98 } // namespace string_escape | 87 } // namespace string_escape |
OLD | NEW |