xref: /aosp_15_r20/external/cronet/base/strings/utf_ostream_operators.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_STRINGS_UTF_OSTREAM_OPERATORS_H_
6 #define BASE_STRINGS_UTF_OSTREAM_OPERATORS_H_
7 
8 #include <iosfwd>
9 #include <string>
10 #include <string_view>
11 
12 #include "base/base_export.h"
13 
14 // Note that "The behavior of a C++ program is undefined if it adds declarations
15 // or definitions to namespace std or to a namespace within namespace std unless
16 // otherwise specified." --C++11[namespace.std]
17 //
18 // We've checked that this particular definition has the intended behavior on
19 // our implementations, but it's prone to breaking in the future, and please
20 // don't imitate this in your own definitions without checking with some
21 // standard library experts.
22 namespace std {
23 // These functions are provided as a convenience for logging, which is where we
24 // use streams (it is against Google style to use streams in other places). It
25 // is designed to allow you to emit non-ASCII Unicode strings to the log file,
26 // which is normally ASCII. It is relatively slow, so try not to use it for
27 // common cases. Non-ASCII characters will be converted to UTF-8 by these
28 // operators.
29 //
30 // The `std::basic_string<T>` overloads are necessary to allow logging types
31 // which are implicitly convertible to `std::basic_string<T>`. Simply taking
32 // `std::basic_string_view<T>` would not work because C++ only allows one
33 // implicit conversion.
34 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
35 BASE_EXPORT std::ostream& operator<<(std::ostream& out, std::wstring_view wstr);
36 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
37                                      const std::wstring& wstr);
38 
39 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const char16_t* str16);
40 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
41                                      std::u16string_view str16);
42 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
43                                      const std::u16string& str16);
44 }  // namespace std
45 
46 #endif  // BASE_STRINGS_UTF_OSTREAM_OPERATORS_H_
47