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

blob: 4499cb30bdb72cb67e1dcd68f6728038b80afe37 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]9fe1a5b2013-02-07 19:18:032// 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
avi84f37e12015-12-25 09:31:4212#include <stdint.h>
13
[email protected]9fe1a5b2013-02-07 19:18:0314#include <string>
Helmut Januschka1dce9dc2024-06-11 13:05:3515#include <string_view>
[email protected]9fe1a5b2013-02-07 19:18:0316
17#include "base/base_export.h"
avi84f37e12015-12-25 09:31:4218#include "build/build_config.h"
[email protected]9fe1a5b2013-02-07 19:18:0319
Xiaohan Wang6700dcf12022-01-15 14:47:0020#if BUILDFLAG(IS_APPLE)
[email protected]9fe1a5b2013-02-07 19:18:0321#include <CoreFoundation/CoreFoundation.h>
Robert Sesek6c5a910252020-06-26 22:25:4722
Avi Drissmana09d7dd2023-08-17 16:26:5823#include "base/apple/scoped_cftyperef.h"
Robert Sesek6c5a910252020-06-26 22:25:4724
[email protected]9fe1a5b2013-02-07 19:18:0325#ifdef __OBJC__
26@class NSString;
[email protected]9fe1a5b2013-02-07 19:18:0327#endif
Xiaohan Wang6700dcf12022-01-15 14:47:0028#endif // BUILDFLAG(IS_APPLE)
[email protected]9fe1a5b2013-02-07 19:18:0329
30namespace base {
31
32// Converts between wide and UTF-8 representations of a string. On error, the
33// result is system-dependent.
Daniel Cheng4455c9842022-01-13 23:26:3734[[nodiscard]] BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide);
Helmut Januschka1dce9dc2024-06-11 13:05:3535[[nodiscard]] BASE_EXPORT std::wstring SysUTF8ToWide(std::string_view utf8);
[email protected]9fe1a5b2013-02-07 19:18:0336
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).
Daniel Cheng4455c9842022-01-13 23:26:3740[[nodiscard]] BASE_EXPORT std::string SysWideToNativeMB(
41 const std::wstring& wide);
Helmut Januschka1dce9dc2024-06-11 13:05:3542[[nodiscard]] BASE_EXPORT std::wstring SysNativeMBToWide(
43 std::string_view native_mb);
[email protected]9fe1a5b2013-02-07 19:18:0344
45// Windows-specific ------------------------------------------------------------
46
Xiaohan Wang6700dcf12022-01-15 14:47:0047#if BUILDFLAG(IS_WIN)
[email protected]9fe1a5b2013-02-07 19:18:0348
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().
Helmut Januschka1dce9dc2024-06-11 13:05:3552[[nodiscard]] BASE_EXPORT std::wstring SysMultiByteToWide(std::string_view mb,
Daniel Cheng4455c9842022-01-13 23:26:3753 uint32_t code_page);
54[[nodiscard]] BASE_EXPORT std::string SysWideToMultiByte(
55 const std::wstring& wide,
56 uint32_t code_page);
[email protected]9fe1a5b2013-02-07 19:18:0357
Xiaohan Wang6700dcf12022-01-15 14:47:0058#endif // BUILDFLAG(IS_WIN)
[email protected]9fe1a5b2013-02-07 19:18:0359
60// Mac-specific ----------------------------------------------------------------
61
Xiaohan Wang6700dcf12022-01-15 14:47:0062#if BUILDFLAG(IS_APPLE)
[email protected]9fe1a5b2013-02-07 19:18:0363
Avi Drissman2b8673a2022-01-04 20:38:4964// Converts between strings and CFStringRefs/NSStrings.
[email protected]9fe1a5b2013-02-07 19:18:0365
Avi Drissman2b8673a2022-01-04 20:38:4966// Converts a string to a CFStringRef. Returns null on failure.
Avi Drissman28154a62023-08-22 04:06:4567[[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
Helmut Januschka1dce9dc2024-06-11 13:05:3568SysUTF8ToCFStringRef(std::string_view utf8);
Avi Drissman28154a62023-08-22 04:06:4569[[nodiscard]] BASE_EXPORT apple::ScopedCFTypeRef<CFStringRef>
Helmut Januschka1dce9dc2024-06-11 13:05:3570SysUTF16ToCFStringRef(std::u16string_view utf16);
[email protected]9fe1a5b2013-02-07 19:18:0371
Avi Drissman2b8673a2022-01-04 20:38:4972// Converts a CFStringRef to a string. Returns an empty string on failure. It is
73// not valid to call these with a null `ref`.
Daniel Cheng4455c9842022-01-13 23:26:3774[[nodiscard]] BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
75[[nodiscard]] BASE_EXPORT std::u16string SysCFStringRefToUTF16(CFStringRef ref);
[email protected]9fe1a5b2013-02-07 19:18:0376
Avi Drissman2b8673a2022-01-04 20:38:4977#ifdef __OBJC__
78
79// Converts a string to an autoreleased NSString. Returns nil on failure.
Helmut Januschka1dce9dc2024-06-11 13:05:3580[[nodiscard]] BASE_EXPORT NSString* SysUTF8ToNSString(std::string_view utf8);
81[[nodiscard]] BASE_EXPORT NSString* SysUTF16ToNSString(
82 std::u16string_view utf16);
Avi Drissman2b8673a2022-01-04 20:38:4983
84// Converts an NSString to a string. Returns an empty string on failure or if
85// `ref` is nil.
Daniel Cheng4455c9842022-01-13 23:26:3786[[nodiscard]] BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
87[[nodiscard]] BASE_EXPORT std::u16string SysNSStringToUTF16(NSString* ref);
[email protected]9fe1a5b2013-02-07 19:18:0388
Avi Drissman2b8673a2022-01-04 20:38:4989#endif // __OBJC__
90
Xiaohan Wang6700dcf12022-01-15 14:47:0091#endif // BUILDFLAG(IS_APPLE)
[email protected]9fe1a5b2013-02-07 19:18:0392
93} // namespace base
94
95#endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_