xref: /aosp_15_r20/external/cronet/base/strings/sys_string_conversions.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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_SYS_STRING_CONVERSIONS_H_
6 #define BASE_STRINGS_SYS_STRING_CONVERSIONS_H_
7 
8 // Provides system-dependent string type conversions for cases where it's
9 // necessary to not use ICU. Generally, you should not need this in Chrome,
10 // but it is used in some shared code. Dependencies should be minimal.
11 
12 #include <stdint.h>
13 
14 #include <string>
15 
16 #include "base/base_export.h"
17 #include "base/strings/string_piece.h"
18 #include "build/build_config.h"
19 
20 #if BUILDFLAG(IS_APPLE)
21 #include <CoreFoundation/CoreFoundation.h>
22 
23 #include "base/apple/scoped_cftyperef.h"
24 
25 #ifdef __OBJC__
26 @class NSString;
27 #endif
28 #endif  // BUILDFLAG(IS_APPLE)
29 
30 namespace base {
31 
32 // Converts between wide and UTF-8 representations of a string. On error, the
33 // result is system-dependent.
34 [[nodiscard]] BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide);
35 [[nodiscard]] BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8);
36 
37 // Converts between wide and the system multi-byte representations of a string.
38 // DANGER: This will lose information and can change (on Windows, this can
39 // change between reboots).
40 [[nodiscard]] BASE_EXPORT std::string SysWideToNativeMB(
41     const std::wstring& wide);
42 [[nodiscard]] BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb);
43 
44 // Windows-specific ------------------------------------------------------------
45 
46 #if BUILDFLAG(IS_WIN)
47 
48 // Converts between 8-bit and wide strings, using the given code page. The
49 // code page identifier is one accepted by the Windows function
50 // MultiByteToWideChar().
51 [[nodiscard]] BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb,
52                                                           uint32_t code_page);
53 [[nodiscard]] BASE_EXPORT std::string SysWideToMultiByte(
54     const std::wstring& wide,
55     uint32_t code_page);
56 
57 #endif  // BUILDFLAG(IS_WIN)
58 
59 // Mac-specific ----------------------------------------------------------------
60 
61 #if BUILDFLAG(IS_APPLE)
62 
63 // Converts between strings and CFStringRefs/NSStrings.
64 
65 // Converts a string to a CFStringRef. Returns null on failure.
66 [[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
67 SysUTF8ToCFStringRef(StringPiece utf8);
68 [[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
69 SysUTF16ToCFStringRef(StringPiece16 utf16);
70 
71 // Converts a CFStringRef to a string. Returns an empty string on failure. It is
72 // not valid to call these with a null `ref`.
73 [[nodiscard]] BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
74 [[nodiscard]] BASE_EXPORT std::u16string SysCFStringRefToUTF16(CFStringRef ref);
75 
76 #ifdef __OBJC__
77 
78 // Converts a string to an autoreleased NSString. Returns nil on failure.
79 [[nodiscard]] BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8);
80 [[nodiscard]] BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16);
81 
82 // Converts an NSString to a string. Returns an empty string on failure or if
83 // `ref` is nil.
84 [[nodiscard]] BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
85 [[nodiscard]] BASE_EXPORT std::u16string SysNSStringToUTF16(NSString* ref);
86 
87 #endif  // __OBJC__
88 
89 #endif  // BUILDFLAG(IS_APPLE)
90 
91 }  // namespace base
92 
93 #endif  // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_
94