Roll src/third_party/WebKit 57aef96:a1089e6 (svn 201978:201979)
[chromium-blink-merge.git] / base / json / string_escape.cc
blobf5d6a760218377384e4412fea2f2ab89fc6b5424
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
3 // found in the LICENSE file.
5 #include "base/json/string_escape.h"
7 #include <string>
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversion_utils.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/third_party/icu/icu_utf.h"
15 namespace base {
17 namespace {
19 // Format string for printing a \uXXXX escape sequence.
20 const char kU16EscapeFormat[] = "\\u%04X";
22 // The code point to output for an invalid input code unit.
23 const uint32 kReplacementCodePoint = 0xFFFD;
25 // Used below in EscapeSpecialCodePoint().
26 COMPILE_ASSERT('<' == 0x3C, less_than_sign_is_0x3c);
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
30 // isn't required by the spec, but it's more readable by humans.
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.
33 // Note: \v is in the reader, but not here since the JSON spec doesn't
34 // allow it.
35 switch (code_point) {
36 case '\b':
37 dest->append("\\b");
38 break;
39 case '\f':
40 dest->append("\\f");
41 break;
42 case '\n':
43 dest->append("\\n");
44 break;
45 case '\r':
46 dest->append("\\r");
47 break;
48 case '\t':
49 dest->append("\\t");
50 break;
51 case '\\':
52 dest->append("\\\\");
53 break;
54 case '"':
55 dest->append("\\\"");
56 break;
57 // Escape < to prevent script execution; escaping > is not necessary and
58 // not doing so save a few bytes.
59 case '<':
60 dest->append("\\u003C");
61 break;
62 // Escape the "Line Separator" and "Paragraph Separator" characters, since
63 // they should be treated like a new line \r or \n.
64 case 0x2028:
65 dest->append("\\u2028");
66 break;
67 case 0x2029:
68 dest->append("\\u2029");
69 break;
70 default:
71 return false;
73 return true;
76 template <typename S>
77 bool EscapeJSONStringImpl(const S& str, bool put_in_quotes, std::string* dest) {
78 bool did_replacement = false;
80 if (put_in_quotes)
81 dest->push_back('"');
83 // Casting is necessary because ICU uses int32. Try and do so safely.
84 CHECK_LE(str.length(), static_cast<size_t>(kint32max));
85 const int32 length = static_cast<int32>(str.length());
87 for (int32 i = 0; i < length; ++i) {
88 uint32 code_point;
89 if (!ReadUnicodeCharacter(str.data(), length, &i, &code_point)) {
90 code_point = kReplacementCodePoint;
91 did_replacement = true;
94 if (EscapeSpecialCodePoint(code_point, dest))
95 continue;
97 // Escape non-printing characters.
98 if (code_point < 32)
99 base::StringAppendF(dest, kU16EscapeFormat, code_point);
100 else
101 WriteUnicodeCharacter(code_point, dest);
104 if (put_in_quotes)
105 dest->push_back('"');
107 return !did_replacement;
110 } // namespace
112 bool EscapeJSONString(const StringPiece& str,
113 bool put_in_quotes,
114 std::string* dest) {
115 return EscapeJSONStringImpl(str, put_in_quotes, dest);
118 bool EscapeJSONString(const StringPiece16& str,
119 bool put_in_quotes,
120 std::string* dest) {
121 return EscapeJSONStringImpl(str, put_in_quotes, dest);
124 std::string GetQuotedJSONString(const StringPiece& str) {
125 std::string dest;
126 bool ok = EscapeJSONStringImpl(str, true, &dest);
127 DCHECK(ok);
128 return dest;
131 std::string GetQuotedJSONString(const StringPiece16& str) {
132 std::string dest;
133 bool ok = EscapeJSONStringImpl(str, true, &dest);
134 DCHECK(ok);
135 return dest;
138 std::string EscapeBytesAsInvalidJSONString(const StringPiece& str,
139 bool put_in_quotes) {
140 std::string dest;
142 if (put_in_quotes)
143 dest.push_back('"');
145 for (StringPiece::const_iterator it = str.begin(); it != str.end(); ++it) {
146 unsigned char c = *it;
147 if (EscapeSpecialCodePoint(c, &dest))
148 continue;
150 if (c < 32 || c > 126)
151 base::StringAppendF(&dest, kU16EscapeFormat, c);
152 else
153 dest.push_back(*it);
156 if (put_in_quotes)
157 dest.push_back('"');
159 return dest;
162 } // namespace base