blob: 2a29b77b0c07c5fdbff18bfd8b8d303ba8f1486b [file] [log] [blame]
[email protected]94861042012-08-04 02:28:361// Copyright (c) 2012 The Chromium Authors. All rights reserved.
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]15af80e2008-08-07 03:11:424
[email protected]9fe1a5b2013-02-07 19:18:035#include "base/strings/sys_string_conversions.h"
[email protected]4bdaceb42008-08-19 13:19:246
[email protected]03d95ac2008-10-08 21:02:567#import <Foundation/Foundation.h>
avi84f37e12015-12-25 09:31:428#include <stddef.h>
[email protected]03d95ac2008-10-08 21:02:569
[email protected]47944fd2008-08-07 19:31:1610#include <vector>
[email protected]4bdaceb42008-08-19 13:19:2411
[email protected]1671162f2011-04-29 15:06:3212#include "base/mac/foundation_util.h"
[email protected]df0ca6c82010-10-17 04:09:0613#include "base/mac/scoped_cftyperef.h"
Peter Kastingb74454862022-07-13 00:40:1214#include "base/numerics/safe_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0015#include "base/strings/string_piece.h"
[email protected]15af80e2008-08-07 03:11:4216
17namespace base {
18
19namespace {
20
Avi Drissman2b8673a2022-01-04 20:38:4921// Converts the supplied CFString into the specified encoding, and returns it as
22// a C++ library string of the template type. Returns an empty string on
23// failure.
[email protected]15af80e2008-08-07 03:11:4224//
Avi Drissman2b8673a2022-01-04 20:38:4925// Do not assert in this function since it is used by the assertion code!
26template <typename StringType>
27StringType CFStringToStringWithEncodingT(CFStringRef cfstring,
28 CFStringEncoding encoding) {
[email protected]15af80e2008-08-07 03:11:4229 CFIndex length = CFStringGetLength(cfstring);
30 if (length == 0)
31 return StringType();
32
33 CFRange whole_string = CFRangeMake(0, length);
34 CFIndex out_size;
Avi Drissman2b8673a2022-01-04 20:38:4935 CFIndex converted = CFStringGetBytes(cfstring, whole_string, encoding,
36 /*lossByte=*/0,
37 /*isExternalRepresentation=*/false,
38 /*buffer=*/nullptr,
39 /*maxBufLen=*/0, &out_size);
Peter Kastingb74454862022-07-13 00:40:1240 if (converted == 0 || out_size <= 0)
[email protected]15af80e2008-08-07 03:11:4241 return StringType();
42
Avi Drissman2b8673a2022-01-04 20:38:4943 // `out_size` is the number of UInt8-sized units needed in the destination.
[email protected]15af80e2008-08-07 03:11:4244 // A buffer allocated as UInt8 units might not be properly aligned to
45 // contain elements of StringType::value_type. Use a container for the
Avi Drissman2b8673a2022-01-04 20:38:4946 // proper value_type, and convert `out_size` by figuring the number of
[email protected]15af80e2008-08-07 03:11:4247 // value_type elements per UInt8. Leave room for a NUL terminator.
Peter Kastingb74454862022-07-13 00:40:1248 size_t elements = static_cast<size_t>(out_size) * sizeof(UInt8) /
49 sizeof(typename StringType::value_type) +
50 1;
[email protected]15af80e2008-08-07 03:11:4251
52 std::vector<typename StringType::value_type> out_buffer(elements);
Avi Drissman2b8673a2022-01-04 20:38:4953 converted =
54 CFStringGetBytes(cfstring, whole_string, encoding,
55 /*lossByte=*/0,
56 /*isExternalRepresentation=*/false,
57 reinterpret_cast<UInt8*>(&out_buffer[0]), out_size,
58 /*usedBufLen=*/nullptr);
[email protected]15af80e2008-08-07 03:11:4259 if (converted == 0)
60 return StringType();
61
62 out_buffer[elements - 1] = '\0';
[email protected]cf7e5922008-10-22 20:24:0763 return StringType(&out_buffer[0], elements - 1);
[email protected]15af80e2008-08-07 03:11:4264}
65
Avi Drissman2b8673a2022-01-04 20:38:4966// Given a C++ library string `in` with an encoding specified by `in_encoding`,
67// converts it to `out_encoding` and returns it as a C++ library string of the
68// `OutStringType` template type. Returns an empty string on failure.
[email protected]15af80e2008-08-07 03:11:4269//
Avi Drissman2b8673a2022-01-04 20:38:4970// Do not assert in this function since it is used by the assertion code!
71template <typename InStringType, typename OutStringType>
72OutStringType StringToStringWithEncodingsT(const InStringType& in,
73 CFStringEncoding in_encoding,
74 CFStringEncoding out_encoding) {
[email protected]15af80e2008-08-07 03:11:4275 typename InStringType::size_type in_length = in.length();
76 if (in_length == 0)
77 return OutStringType();
78
[email protected]3df79f42013-06-24 18:49:0579 base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
Avi Drissman2b8673a2022-01-04 20:38:4980 kCFAllocatorDefault, reinterpret_cast<const UInt8*>(in.data()),
Peter Kastingb74454862022-07-13 00:40:1281 checked_cast<CFIndex>(in_length *
82 sizeof(typename InStringType::value_type)),
83 in_encoding,
Avi Drissman2b8673a2022-01-04 20:38:4984 /*isExternalRepresentation=*/false, kCFAllocatorNull));
[email protected]15af80e2008-08-07 03:11:4285 if (!cfstring)
86 return OutStringType();
87
Avi Drissman2b8673a2022-01-04 20:38:4988 return CFStringToStringWithEncodingT<OutStringType>(cfstring, out_encoding);
[email protected]15af80e2008-08-07 03:11:4289}
90
Avi Drissman2b8673a2022-01-04 20:38:4991// Given a StringPiece `in` with an encoding specified by `in_encoding`, returns
92// it as a CFStringRef. Returns null on failure.
Jan Wilken Dörrie8ed6fce2021-03-25 23:00:3893template <typename CharT>
Avi Drissman2b8673a2022-01-04 20:38:4994ScopedCFTypeRef<CFStringRef> StringPieceToCFStringWithEncodingsT(
Jan Wilken Dörrie8ed6fce2021-03-25 23:00:3895 BasicStringPiece<CharT> in,
[email protected]d2a10d12008-08-22 19:55:2696 CFStringEncoding in_encoding) {
David Benjaminc2b74552019-04-22 23:05:5997 const auto in_length = in.length();
[email protected]d2a10d12008-08-22 19:55:2698 if (in_length == 0)
Robert Sesek6c5a910252020-06-26 22:25:4799 return ScopedCFTypeRef<CFStringRef>(CFSTR(""), base::scoped_policy::RETAIN);
[email protected]d2a10d12008-08-22 19:55:26100
Robert Sesek6c5a910252020-06-26 22:25:47101 return ScopedCFTypeRef<CFStringRef>(CFStringCreateWithBytes(
David Benjaminc2b74552019-04-22 23:05:59102 kCFAllocatorDefault, reinterpret_cast<const UInt8*>(in.data()),
Peter Kastingb74454862022-07-13 00:40:12103 checked_cast<CFIndex>(in_length * sizeof(CharT)), in_encoding, false));
[email protected]d2a10d12008-08-22 19:55:26104}
105
[email protected]15af80e2008-08-07 03:11:42106} // namespace
107
Avi Drissman2b8673a2022-01-04 20:38:49108// The CFStringEncodings used below specify the byte ordering explicitly,
109// otherwise CFString will be confused when strings don't carry BOMs, as they
110// typically won't.
111
112// Do not assert in this function since it is used by the assertion code!
[email protected]15af80e2008-08-07 03:11:42113std::string SysWideToUTF8(const std::wstring& wide) {
Avi Drissman2b8673a2022-01-04 20:38:49114 return StringToStringWithEncodingsT<std::wstring, std::string>(
115 wide, kCFStringEncodingUTF32LE, kCFStringEncodingUTF8);
[email protected]15af80e2008-08-07 03:11:42116}
117
Avi Drissman2b8673a2022-01-04 20:38:49118// Do not assert in this function since it is used by the assertion code!
Reilly Grant39aecc32018-01-04 00:52:52119std::wstring SysUTF8ToWide(StringPiece utf8) {
Avi Drissman2b8673a2022-01-04 20:38:49120 return StringToStringWithEncodingsT<StringPiece, std::wstring>(
121 utf8, kCFStringEncodingUTF8, kCFStringEncodingUTF32LE);
[email protected]15af80e2008-08-07 03:11:42122}
123
124std::string SysWideToNativeMB(const std::wstring& wide) {
[email protected]47944fd2008-08-07 19:31:16125 return SysWideToUTF8(wide);
[email protected]15af80e2008-08-07 03:11:42126}
127
Reilly Grant39aecc32018-01-04 00:52:52128std::wstring SysNativeMBToWide(StringPiece native_mb) {
[email protected]47944fd2008-08-07 19:31:16129 return SysUTF8ToWide(native_mb);
[email protected]15af80e2008-08-07 03:11:42130}
131
Robert Sesek6c5a910252020-06-26 22:25:47132ScopedCFTypeRef<CFStringRef> SysUTF8ToCFStringRef(StringPiece utf8) {
Avi Drissman2b8673a2022-01-04 20:38:49133 return StringPieceToCFStringWithEncodingsT(utf8, kCFStringEncodingUTF8);
[email protected]d2a10d12008-08-22 19:55:26134}
135
Robert Sesek6c5a910252020-06-26 22:25:47136ScopedCFTypeRef<CFStringRef> SysUTF16ToCFStringRef(StringPiece16 utf16) {
Avi Drissman2b8673a2022-01-04 20:38:49137 return StringPieceToCFStringWithEncodingsT(utf16, kCFStringEncodingUTF16LE);
[email protected]f224d572009-02-18 21:39:23138}
139
David Benjaminc2b74552019-04-22 23:05:59140NSString* SysUTF8ToNSString(StringPiece utf8) {
Robert Sesek6c5a910252020-06-26 22:25:47141 return [mac::CFToNSCast(SysUTF8ToCFStringRef(utf8).release()) autorelease];
[email protected]03d95ac2008-10-08 21:02:56142}
143
David Benjaminc2b74552019-04-22 23:05:59144NSString* SysUTF16ToNSString(StringPiece16 utf16) {
Robert Sesek6c5a910252020-06-26 22:25:47145 return [mac::CFToNSCast(SysUTF16ToCFStringRef(utf16).release()) autorelease];
[email protected]f224d572009-02-18 21:39:23146}
147
[email protected]d2a10d12008-08-22 19:55:26148std::string SysCFStringRefToUTF8(CFStringRef ref) {
Avi Drissman2b8673a2022-01-04 20:38:49149 return CFStringToStringWithEncodingT<std::string>(ref, kCFStringEncodingUTF8);
[email protected]d2a10d12008-08-22 19:55:26150}
151
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57152std::u16string SysCFStringRefToUTF16(CFStringRef ref) {
Avi Drissman2b8673a2022-01-04 20:38:49153 return CFStringToStringWithEncodingT<std::u16string>(
154 ref, kCFStringEncodingUTF16LE);
[email protected]f224d572009-02-18 21:39:23155}
156
[email protected]03d95ac2008-10-08 21:02:56157std::string SysNSStringToUTF8(NSString* nsstring) {
[email protected]a2494cb2009-11-08 19:04:54158 if (!nsstring)
159 return std::string();
Avi Drissman2b8673a2022-01-04 20:38:49160 return SysCFStringRefToUTF8(mac::NSToCFCast(nsstring));
[email protected]03d95ac2008-10-08 21:02:56161}
license.botbf09a502008-08-24 00:55:55162
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57163std::u16string SysNSStringToUTF16(NSString* nsstring) {
[email protected]a2494cb2009-11-08 19:04:54164 if (!nsstring)
Jan Wilken Dörrie085b2aa2021-03-12 16:26:57165 return std::u16string();
Avi Drissman2b8673a2022-01-04 20:38:49166 return SysCFStringRefToUTF16(mac::NSToCFCast(nsstring));
[email protected]f224d572009-02-18 21:39:23167}
168
[email protected]03d95ac2008-10-08 21:02:56169} // namespace base