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

Side by Side Diff: base/json/string_escape.cc

Issue 1512013: Improve JsonDoubleQuoteT to do script escapes (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 8 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 | « no previous file | base/json/string_escape_unittest.cc » ('j') | net/base/dir_header.html » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/string_util.h" 9 #include "base/string_util.h"
10 10
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 template <class STR> 51 template <class STR>
52 void JsonDoubleQuoteT(const STR& str, 52 void JsonDoubleQuoteT(const STR& str,
53 bool put_in_quotes, 53 bool put_in_quotes,
54 std::string* dst) { 54 std::string* dst) {
55 if (put_in_quotes) 55 if (put_in_quotes)
56 dst->push_back('"'); 56 dst->push_back('"');
57 57
58 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) { 58 for (typename STR::const_iterator it = str.begin(); it != str.end(); ++it) {
59 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it; 59 typename ToUnsigned<typename STR::value_type>::Unsigned c = *it;
60 if (!JsonSingleEscapeChar(c, dst)) { 60 if (!JsonSingleEscapeChar(c, dst)) {
61 if (c < 32 || c > 126) { 61 if (c < 32 || c > 126 || c == 60 || c == 62) {
eroman 2010/04/06 01:43:12 nit: can you '<' instead of 60, and '>' instead of
62 // Technically, we could also pass through c > 126 as UTF8, but this is 62 // 1. Escaping <, > to prevent script execution.
63 // also optional. It would also be a pain to implement here. 63 // 2. Technically, we could also pass through c > 126 as UTF8, but this
64 // is also optional. It would also be a pain to implement here.
64 unsigned int as_uint = static_cast<unsigned int>(c); 65 unsigned int as_uint = static_cast<unsigned int>(c);
65 StringAppendF(dst, "\\u%04X", as_uint); 66 StringAppendF(dst, "\\u%04X", as_uint);
66 } else { 67 } else {
67 unsigned char ascii = static_cast<unsigned char>(*it); 68 unsigned char ascii = static_cast<unsigned char>(*it);
68 dst->push_back(ascii); 69 dst->push_back(ascii);
69 } 70 }
70 } 71 }
71 } 72 }
72 73
73 if (put_in_quotes) 74 if (put_in_quotes)
(...skipping 20 matching lines...) Expand all
94 JsonDoubleQuoteT(str, put_in_quotes, dst); 95 JsonDoubleQuoteT(str, put_in_quotes, dst);
95 } 96 }
96 97
97 std::string GetDoubleQuotedJson(const string16& str) { 98 std::string GetDoubleQuotedJson(const string16& str) {
98 std::string dst; 99 std::string dst;
99 JsonDoubleQuote(str, true, &dst); 100 JsonDoubleQuote(str, true, &dst);
100 return dst; 101 return dst;
101 } 102 }
102 103
103 } // namespace base 104 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/json/string_escape_unittest.cc » ('j') | net/base/dir_header.html » ('J')

Powered by Google App Engine
This is Rietveld 408576698