Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 6c93ed2110f68dedf56938ac805f1f87641e6868 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]bbe15712013-12-11 22:10:454
5// This file defines utility functions for escaping strings suitable for JSON.
initial.commitd7cae122008-07-26 21:49:386
[email protected]93d49d72009-10-23 20:00:207#ifndef BASE_JSON_STRING_ESCAPE_H_
8#define BASE_JSON_STRING_ESCAPE_H_
initial.commitd7cae122008-07-26 21:49:389
[email protected]d36519b2009-05-20 16:43:4910#include <string>
Helmut Januschka4e477ef42024-02-27 19:26:4511#include <string_view>
[email protected]d36519b2009-05-20 16:43:4912
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
initial.commitd7cae122008-07-26 21:49:3814
[email protected]93d49d72009-10-23 20:00:2015namespace base {
initial.commitd7cae122008-07-26 21:49:3816
sabbakumov51d82b242017-06-13 04:28:1317// Appends to |dest| an escaped version of |str|. Valid UTF-8 code units and
18// characters will pass through from the input to the output. Invalid code
19// units and characters will be replaced with the U+FFFD replacement character.
20// This function returns true if no replacement was necessary and false if
21// there was a lossy replacement. On return, |dest| will contain a valid UTF-8
22// JSON string.
[email protected]bbe15712013-12-11 22:10:4523//
24// Non-printing control characters will be escaped as \uXXXX sequences for
25// readability.
26//
27// If |put_in_quotes| is true, then a leading and trailing double-quote mark
28// will be appended to |dest| as well.
Helmut Januschka4e477ef42024-02-27 19:26:4529BASE_EXPORT bool EscapeJSONString(std::string_view str,
[email protected]bbe15712013-12-11 22:10:4530 bool put_in_quotes,
31 std::string* dest);
initial.commitd7cae122008-07-26 21:49:3832
Helmut Januschka4e477ef42024-02-27 19:26:4533// Performs a similar function to the UTF-8 std::string_view version above,
[email protected]bbe15712013-12-11 22:10:4534// converting UTF-16 code units to UTF-8 code units and escaping non-printing
35// control characters. On return, |dest| will contain a valid UTF-8 JSON string.
Helmut Januschka4e477ef42024-02-27 19:26:4536BASE_EXPORT bool EscapeJSONString(std::u16string_view str,
[email protected]bbe15712013-12-11 22:10:4537 bool put_in_quotes,
38 std::string* dest);
[email protected]a9602de2010-03-18 23:43:1139
[email protected]bbe15712013-12-11 22:10:4540// Helper functions that wrap the above two functions but return the value
41// instead of appending. |put_in_quotes| is always true.
Helmut Januschka4e477ef42024-02-27 19:26:4542BASE_EXPORT std::string GetQuotedJSONString(std::string_view str);
43BASE_EXPORT std::string GetQuotedJSONString(std::u16string_view str);
[email protected]d36519b2009-05-20 16:43:4944
[email protected]bbe15712013-12-11 22:10:4545// Given an arbitrary byte string |str|, this will escape all non-ASCII bytes
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 mangled if passed to to the base::JSONReader, since the reader will
52// interpret it as UTF-16 and convert it to UTF-8.
53//
54// The output of this function takes the *appearance* of JSON but is not in
Nigel Tao71c958db2020-04-09 23:18:4455// fact valid according to RFC 8259.
Helmut Januschka4e477ef42024-02-27 19:26:4556BASE_EXPORT std::string EscapeBytesAsInvalidJSONString(std::string_view str,
[email protected]bbe15712013-12-11 22:10:4557 bool put_in_quotes);
initial.commitd7cae122008-07-26 21:49:3858
[email protected]93d49d72009-10-23 20:00:2059} // namespace base
initial.commitd7cae122008-07-26 21:49:3860
[email protected]93d49d72009-10-23 20:00:2061#endif // BASE_JSON_STRING_ESCAPE_H_