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/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/strings/utf_string_conversion_utils.h" | 11 #include "base/strings/utf_string_conversion_utils.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/third_party/icu/icu_utf.h" | 13 #include "base/third_party/icu/icu_utf.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Format string for printing a \uXXXX escape sequence. | 19 // Format string for printing a \uXXXX escape sequence. |
20 const char kU16EscapeFormat[] = "\\u%04X"; | 20 const char kU16EscapeFormat[] = "\\u%04X"; |
21 | 21 |
22 // The code point to output for an invalid input code unit. | 22 // The code point to output for an invalid input code unit. |
23 const uint32 kReplacementCodePoint = 0xFFFD; | 23 const uint32 kReplacementCodePoint = 0xFFFD; |
24 | 24 |
25 // Used below in EscapeSpecialCodePoint(). | 25 // Used below in EscapeSpecialCodePoint(). |
26 COMPILE_ASSERT('<' == 0x3C, less_than_sign_is_0x3c); | 26 static_assert('<' == 0x3C, "less than sign must be 0x3c"); |
27 | 27 |
28 // Try to escape the |code_point| if it is a known special character. If | 28 // Try to escape the |code_point| if it is a known special character. If |
29 // successful, returns true and appends the escape sequence to |dest|. This | 29 // successful, returns true and appends the escape sequence to |dest|. This |
30 // isn't required by the spec, but it's more readable by humans. | 30 // isn't required by the spec, but it's more readable by humans. |
31 bool EscapeSpecialCodePoint(uint32 code_point, std::string* dest) { | 31 bool EscapeSpecialCodePoint(uint32 code_point, std::string* dest) { |
32 // WARNING: if you add a new case here, you need to update the reader as well. | 32 // WARNING: if you add a new case here, you need to update the reader as well. |
33 // Note: \v is in the reader, but not here since the JSON spec doesn't | 33 // Note: \v is in the reader, but not here since the JSON spec doesn't |
34 // allow it. | 34 // allow it. |
35 switch (code_point) { | 35 switch (code_point) { |
36 case '\b': | 36 case '\b': |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 dest.push_back(*it); | 153 dest.push_back(*it); |
154 } | 154 } |
155 | 155 |
156 if (put_in_quotes) | 156 if (put_in_quotes) |
157 dest.push_back('"'); | 157 dest.push_back('"'); |
158 | 158 |
159 return dest; | 159 return dest; |
160 } | 160 } |
161 | 161 |
162 } // namespace base | 162 } // namespace base |
OLD | NEW |