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

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

Issue 100823007: Stop doing unnecessary UTF-8 to UTF-16 conversions in JSONWriter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ChromeOS page encodings Created 7 years 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/json/json_writer.cc ('k') | base/json/string_escape.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // This file defines utility functions for escaping strings. 5 // This file defines utility functions for escaping strings suitable for JSON.
6 6
7 #ifndef BASE_JSON_STRING_ESCAPE_H_ 7 #ifndef BASE_JSON_STRING_ESCAPE_H_
8 #define BASE_JSON_STRING_ESCAPE_H_ 8 #define BASE_JSON_STRING_ESCAPE_H_
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/base_export.h" 12 #include "base/base_export.h"
13 #include "base/strings/string_piece.h" 13 #include "base/strings/string_piece.h"
14 14
15 namespace base { 15 namespace base {
16 16
17 // Escape |str| appropriately for a JSON string literal, _appending_ the 17 // Appends to |dest| an escaped version of |str|. Valid UTF-8 code units will
18 // result to |dst|. This will create unicode escape sequences (\uXXXX). 18 // pass through from the input to the output. Invalid code units will be
19 // If |put_in_quotes| is true, the result will be surrounded in double quotes. 19 // replaced with the U+FFFD replacement character. This function returns true
20 // The outputted literal, when interpreted by the browser, should result in a 20 // if no replacement was necessary and false if there was a lossy replacement.
21 // javascript string that is identical and the same length as the input |str|. 21 // On return, |dest| will contain a valid UTF-8 JSON string.
22 BASE_EXPORT void JsonDoubleQuote(const StringPiece& str, 22 //
23 bool put_in_quotes, 23 // Non-printing control characters will be escaped as \uXXXX sequences for
24 std::string* dst); 24 // readability.
25 //
26 // If |put_in_quotes| is true, then a leading and trailing double-quote mark
27 // will be appended to |dest| as well.
28 BASE_EXPORT bool EscapeJSONString(const StringPiece& str,
29 bool put_in_quotes,
30 std::string* dest);
25 31
26 // Same as above, but always returns the result double quoted. 32 // Performs a similar function to the UTF-8 StringPiece version above,
27 BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece& str); 33 // converting UTF-16 code units to UTF-8 code units and escaping non-printing
34 // control characters. On return, |dest| will contain a valid UTF-8 JSON string.
35 BASE_EXPORT bool EscapeJSONString(const StringPiece16& str,
36 bool put_in_quotes,
37 std::string* dest);
28 38
29 BASE_EXPORT void JsonDoubleQuote(const StringPiece16& str, 39 // Helper functions that wrap the above two functions but return the value
30 bool put_in_quotes, 40 // instead of appending. |put_in_quotes| is always true.
31 std::string* dst); 41 BASE_EXPORT std::string GetQuotedJSONString(const StringPiece& str);
42 BASE_EXPORT std::string GetQuotedJSONString(const StringPiece16& str);
32 43
33 // Same as above, but always returns the result double quoted. 44 // Given an arbitrary byte string |str|, this will escape all non-ASCII bytes
34 BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece16& str); 45 // as \uXXXX escape sequences. This function is *NOT* meant to be used with
46 // Unicode strings and does not validate |str| as one.
47 //
48 // CAVEAT CALLER: The output of this function may not be valid JSON, since
49 // JSON requires escape sequences to be valid UTF-16 code units. This output
50 // will be mangled if passed to to the base::JSONReader, since the reader will
51 // interpret it as UTF-16 and convert it to UTF-8.
52 //
53 // The output of this function takes the *appearance* of JSON but is not in
54 // fact valid according to RFC 4627.
55 BASE_EXPORT std::string EscapeBytesAsInvalidJSONString(const StringPiece& str,
56 bool put_in_quotes);
35 57
36 } // namespace base 58 } // namespace base
37 59
38 #endif // BASE_JSON_STRING_ESCAPE_H_ 60 #endif // BASE_JSON_STRING_ESCAPE_H_
OLDNEW
« no previous file with comments | « base/json/json_writer.cc ('k') | base/json/string_escape.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698