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/json/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 base { |
| 12 |
| 13 namespace { |
12 | 14 |
13 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, | 15 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful, |
14 // returns true and appends the escape sequence to |dst|. This isn't required | 16 // 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. | 17 // by the spec, but it's more readable by humans than the \uXXXX alternatives. |
16 template<typename CHAR> | 18 template<typename CHAR> |
17 static bool JsonSingleEscapeChar(const CHAR c, std::string* dst) { | 19 static bool JsonSingleEscapeChar(const CHAR c, std::string* dst) { |
18 // WARNING: if you add a new case here, you need to update the reader as well. | 20 // 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 | 21 // Note: \v is in the reader, but not here since the JSON spec doesn't |
20 // allow it. | 22 // allow it. |
21 switch (c) { | 23 switch (c) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 unsigned char ascii = static_cast<unsigned char>(*it); | 67 unsigned char ascii = static_cast<unsigned char>(*it); |
66 dst->push_back(ascii); | 68 dst->push_back(ascii); |
67 } | 69 } |
68 } | 70 } |
69 } | 71 } |
70 | 72 |
71 if (put_in_quotes) | 73 if (put_in_quotes) |
72 dst->push_back('"'); | 74 dst->push_back('"'); |
73 } | 75 } |
74 | 76 |
| 77 } // namespace |
| 78 |
75 void JsonDoubleQuote(const std::string& str, | 79 void JsonDoubleQuote(const std::string& str, |
76 bool put_in_quotes, | 80 bool put_in_quotes, |
77 std::string* dst) { | 81 std::string* dst) { |
78 JsonDoubleQuoteT(str, put_in_quotes, dst); | 82 JsonDoubleQuoteT(str, put_in_quotes, dst); |
79 } | 83 } |
80 | 84 |
81 void JsonDoubleQuote(const string16& str, | 85 void JsonDoubleQuote(const string16& str, |
82 bool put_in_quotes, | 86 bool put_in_quotes, |
83 std::string* dst) { | 87 std::string* dst) { |
84 JsonDoubleQuoteT(str, put_in_quotes, dst); | 88 JsonDoubleQuoteT(str, put_in_quotes, dst); |
85 } | 89 } |
86 | 90 |
87 } // namespace string_escape | 91 } // namespace base |
OLD | NEW |