OLD | NEW |
---|---|
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. On return, |dest| will |
Mark Mentovai
2013/12/06 15:35:12
Silently replaced with U+FFFD? Sounds like a recip
Avi (use Gerrit)
2013/12/06 15:45:25
U+FFFD is REPLACEMENT CHARACTER, literally designe
jungshik at Google
2013/12/07 13:16:59
I agree with Avi. Putting in U+FFFD for an invalid
Mark Mentovai
2013/12/07 22:22:49
Jungshik Shin wrote:
Robert Sesek
2013/12/09 19:52:09
Done.
| |
20 // The outputted literal, when interpreted by the browser, should result in a | 20 // contain a valid UTF-8 JSON string. |
21 // javascript string that is identical and the same length as the input |str|. | 21 // |
22 BASE_EXPORT void JsonDoubleQuote(const StringPiece& str, | 22 // Non-printing control characters will be escaped as \uXXXX sequences for |
23 bool put_in_quotes, | 23 // readability. |
24 std::string* dst); | 24 // |
25 // If |put_in_quotes| is true, then a leading and trailing double-quote mark | |
26 // will be appended to |dest| as well. | |
27 BASE_EXPORT void EscapeJSONString(const StringPiece& str, | |
28 bool put_in_quotes, | |
29 std::string* dest); | |
25 | 30 |
26 // Same as above, but always returns the result double quoted. | 31 // Performs a similar function to the UTF-8 StringPiece version above, but |
27 BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece& str); | 32 // instead operates on UTF-16 code units. Unlike UTF-8 code units, the |
33 // UTF-16 units will be escaped into \uXXXX sequences. Invalid code units | |
Mark Mentovai
2013/12/06 15:35:12
I don’t know why you’d maintain this distinction.
jungshik at Google
2013/12/07 13:16:59
I'm with Mark and curious as to why you'd make t
Robert Sesek
2013/12/09 19:52:09
Changed to a templatized impl that results in the
| |
34 // will be replaced with \uFFFD. On return, |dest| will contain a valid | |
35 // UTF-8 JSON string. | |
36 BASE_EXPORT void EscapeJSONString(const StringPiece16& str, | |
37 bool put_in_quotes, | |
38 std::string* dest); | |
28 | 39 |
29 BASE_EXPORT void JsonDoubleQuote(const StringPiece16& str, | 40 // Helper functions that wrap the above two functions but return the value |
30 bool put_in_quotes, | 41 // instead of appending. |put_in_quotes| is always true. |
31 std::string* dst); | 42 BASE_EXPORT std::string GetQuotedJSONString(const StringPiece& str); |
43 BASE_EXPORT std::string GetQuotedJSONString(const StringPiece16& str); | |
32 | 44 |
33 // Same as above, but always returns the result double quoted. | 45 // Given an arbitrary byte string |str|, this will escape all non-ASCII bytes |
34 BASE_EXPORT std::string GetDoubleQuotedJson(const StringPiece16& str); | 46 // as \uXXXX escape sequences. This function is *NOT* meant to be used with |
47 // Unicode strings and does not validate |str| as one. | |
48 // | |
49 // CAVEAT CALLER: The output of this function may not be valid JSON, since | |
50 // JSON requires escape sequences to be valid UTF-16 code units. This output | |
51 // will be rejcted via a parser error if passed to to the base::JSONReader. | |
jungshik at Google
2013/12/07 13:16:59
If you're converting 0x80-0xFF to \u0080 - \u00FF,
Mark Mentovai
2013/12/07 22:22:49
Jungshik Shin wrote:
Mark Mentovai
2013/12/07 22:22:49
rejcted → rejected
Robert Sesek
2013/12/09 19:52:09
Done.
| |
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, | |
Mark Mentovai
2013/12/06 15:35:12
Good name.
| |
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_ |
OLD | NEW |