Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: base/string_escape.cc

Issue 316016: Move the json-related files into a separate json directory. This hopefully al... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/string_escape.h ('k') | base/string_escape_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/string_escape.h"
6
7 #include <string>
8
9 #include "base/string_util.h"
10
11 namespace string_escape {
12
13 // 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
15 // by the spec, but it's more readable by humans than the \uXXXX alternatives.
16 template<typename CHAR>
17 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.
19 // Note: \v is in the reader, but not here since the JSON spec doesn't
20 // allow it.
21 switch (c) {
22 case '\b':
23 dst->append("\\b");
24 break;
25 case '\f':
26 dst->append("\\f");
27 break;
28 case '\n':
29 dst->append("\\n");
30 break;
31 case '\r':
32 dst->append("\\r");
33 break;
34 case '\t':
35 dst->append("\\t");
36 break;
37 case '\\':
38 dst->append("\\\\");
39 break;
40 case '"':
41 dst->append("\\\"");
42 break;
43 default:
44 return false;
45 }
46 return true;
47 }
48
49 template <class STR>
50 void JsonDoubleQuoteT(const STR& str,
51 bool put_in_quotes,
52 std::string* dst) {
53 if (put_in_quotes)
54 dst->push_back('"');
55
56 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) {
57 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it;
58 if (!JsonSingleEscapeChar(c, dst)) {
59 if (c < 32 || c > 126) {
60 // Technically, we could also pass through c > 126 as UTF8, but this is
61 // also optional. It would also be a pain to implement here.
62 unsigned int as_uint = static_cast<unsigned int>(c);
63 StringAppendF(dst, "\\u%04X", as_uint);
64 } else {
65 unsigned char ascii = static_cast<unsigned char>(*it);
66 dst->push_back(ascii);
67 }
68 }
69 }
70
71 if (put_in_quotes)
72 dst->push_back('"');
73 }
74
75 void JsonDoubleQuote(const std::string& str,
76 bool put_in_quotes,
77 std::string* dst) {
78 JsonDoubleQuoteT(str, put_in_quotes, dst);
79 }
80
81 void JsonDoubleQuote(const string16& str,
82 bool put_in_quotes,
83 std::string* dst) {
84 JsonDoubleQuoteT(str, put_in_quotes, dst);
85 }
86
87 } // namespace string_escape
OLDNEW
« no previous file with comments | « base/string_escape.h ('k') | base/string_escape_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698