• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string_view>
16 
17 #include "base/base_export.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(std::string_view 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(
43     std::string_view native_mb);
44 
45 // Windows-specific ------------------------------------------------------------
46 
47 #if BUILDFLAG(IS_WIN)
48 
49 // Converts between 8-bit and wide strings, using the given code page. The
50 // code page identifier is one accepted by the Windows function
51 // MultiByteToWideChar().
52 [[nodiscard]] BASE_EXPORT std::wstring SysMultiByteToWide(std::string_view mb,
53                                                           uint32_t code_page);
54 [[nodiscard]] BASE_EXPORT std::string SysWideToMultiByte(
55     const std::wstring& wide,
56     uint32_t code_page);
57 
58 #endif  // BUILDFLAG(IS_WIN)
59 
60 // Mac-specific ----------------------------------------------------------------
61 
62 #if BUILDFLAG(IS_APPLE)
63 
64 // Converts between strings and CFStringRefs/NSStrings.
65 
66 // Converts a string to a CFStringRef. Returns null on failure.
67 [[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
68 SysUTF8ToCFStringRef(std::string_view utf8);
69 [[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
70 SysUTF16ToCFStringRef(std::u16string_view utf16);
71 
72 // Converts a CFStringRef to a string. Returns an empty string on failure. It is
73 // not valid to call these with a null `ref`.
74 [[nodiscard]] BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
75 [[nodiscard]] BASE_EXPORT std::u16string SysCFStringRefToUTF16(CFStringRef ref);
76 
77 #ifdef __OBJC__
78 
79 // Converts a string to an autoreleased NSString. Returns nil on failure.
80 [[nodiscard]] BASE_EXPORT NSString* SysUTF8ToNSString(std::string_view utf8);
81 [[nodiscard]] BASE_EXPORT NSString* SysUTF16ToNSString(
82     std::u16string_view utf16);
83 
84 // Converts an NSString to a string. Returns an empty string on failure or if
85 // `ref` is nil.
86 [[nodiscard]] BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
87 [[nodiscard]] BASE_EXPORT std::u16string SysNSStringToUTF16(NSString* ref);
88 
89 #endif  // __OBJC__
90 
91 #endif  // BUILDFLAG(IS_APPLE)
92 
93 }  // namespace base
94 
95 #endif  // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_
96