[email protected] | 9486104 | 2012-08-04 02:28:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 4 | |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 5 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 6 | |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
avi | 84f37e1 | 2015-12-25 09:31:42 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 9 | |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 11 | |
[email protected] | 1671162f | 2011-04-29 15:06:32 | [diff] [blame] | 12 | #include "base/mac/foundation_util.h" |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 13 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | namespace { |
| 19 | |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 20 | // Convert the supplied CFString into the specified encoding, and return it as |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 21 | // an STL string of the template type. Returns an empty string on failure. |
| 22 | // |
| 23 | // Do not assert in this function since it is used by the asssertion code! |
| 24 | template<typename StringType> |
| 25 | static StringType CFStringToSTLStringWithEncodingT(CFStringRef cfstring, |
| 26 | CFStringEncoding encoding) { |
| 27 | CFIndex length = CFStringGetLength(cfstring); |
| 28 | if (length == 0) |
| 29 | return StringType(); |
| 30 | |
| 31 | CFRange whole_string = CFRangeMake(0, length); |
| 32 | CFIndex out_size; |
| 33 | CFIndex converted = CFStringGetBytes(cfstring, |
| 34 | whole_string, |
| 35 | encoding, |
| 36 | 0, // lossByte |
| 37 | false, // isExternalRepresentation |
| 38 | NULL, // buffer |
| 39 | 0, // maxBufLen |
| 40 | &out_size); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 41 | if (converted == 0 || out_size == 0) |
| 42 | return StringType(); |
| 43 | |
| 44 | // out_size is the number of UInt8-sized units needed in the destination. |
| 45 | // A buffer allocated as UInt8 units might not be properly aligned to |
| 46 | // contain elements of StringType::value_type. Use a container for the |
| 47 | // proper value_type, and convert out_size by figuring the number of |
| 48 | // value_type elements per UInt8. Leave room for a NUL terminator. |
| 49 | typename StringType::size_type elements = |
| 50 | out_size * sizeof(UInt8) / sizeof(typename StringType::value_type) + 1; |
| 51 | |
| 52 | std::vector<typename StringType::value_type> out_buffer(elements); |
| 53 | converted = CFStringGetBytes(cfstring, |
| 54 | whole_string, |
| 55 | encoding, |
| 56 | 0, // lossByte |
| 57 | false, // isExternalRepresentation |
| 58 | reinterpret_cast<UInt8*>(&out_buffer[0]), |
| 59 | out_size, |
| 60 | NULL); // usedBufLen |
| 61 | if (converted == 0) |
| 62 | return StringType(); |
| 63 | |
| 64 | out_buffer[elements - 1] = '\0'; |
[email protected] | cf7e592 | 2008-10-22 20:24:07 | [diff] [blame] | 65 | return StringType(&out_buffer[0], elements - 1); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Given an STL string |in| with an encoding specified by |in_encoding|, |
| 69 | // convert it to |out_encoding| and return it as an STL string of the |
| 70 | // |OutStringType| template type. Returns an empty string on failure. |
| 71 | // |
| 72 | // Do not assert in this function since it is used by the asssertion code! |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 73 | template<typename InStringType, typename OutStringType> |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 74 | static OutStringType STLStringToSTLStringWithEncodingsT( |
| 75 | const InStringType& in, |
| 76 | CFStringEncoding in_encoding, |
| 77 | CFStringEncoding out_encoding) { |
| 78 | typename InStringType::size_type in_length = in.length(); |
| 79 | if (in_length == 0) |
| 80 | return OutStringType(); |
| 81 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 82 | base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy( |
| 83 | NULL, |
| 84 | reinterpret_cast<const UInt8*>(in.data()), |
| 85 | in_length * sizeof(typename InStringType::value_type), |
| 86 | in_encoding, |
| 87 | false, |
| 88 | kCFAllocatorNull)); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 89 | if (!cfstring) |
| 90 | return OutStringType(); |
| 91 | |
| 92 | return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring, |
| 93 | out_encoding); |
| 94 | } |
| 95 | |
[email protected] | d2a10d1 | 2008-08-22 19:55:26 | [diff] [blame] | 96 | // Given an STL string |in| with an encoding specified by |in_encoding|, |
| 97 | // return it as a CFStringRef. Returns NULL on failure. |
| 98 | template<typename StringType> |
| 99 | static CFStringRef STLStringToCFStringWithEncodingsT( |
| 100 | const StringType& in, |
| 101 | CFStringEncoding in_encoding) { |
| 102 | typename StringType::size_type in_length = in.length(); |
| 103 | if (in_length == 0) |
| 104 | return CFSTR(""); |
| 105 | |
| 106 | return CFStringCreateWithBytes(kCFAllocatorDefault, |
| 107 | reinterpret_cast<const UInt8*>(in.data()), |
| 108 | in_length * |
| 109 | sizeof(typename StringType::value_type), |
| 110 | in_encoding, |
| 111 | false); |
| 112 | } |
| 113 | |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 114 | // Specify the byte ordering explicitly, otherwise CFString will be confused |
| 115 | // when strings don't carry BOMs, as they typically won't. |
| 116 | static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8; |
| 117 | #ifdef __BIG_ENDIAN__ |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 118 | static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16BE; |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 119 | static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE; |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 120 | #elif defined(__LITTLE_ENDIAN__) |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 121 | static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16LE; |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 122 | static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE; |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 123 | #endif // __LITTLE_ENDIAN__ |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 124 | |
| 125 | } // namespace |
| 126 | |
| 127 | // Do not assert in this function since it is used by the asssertion code! |
| 128 | std::string SysWideToUTF8(const std::wstring& wide) { |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 129 | return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>( |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 130 | wide, kWideStringEncoding, kNarrowStringEncoding); |
| 131 | } |
| 132 | |
| 133 | // Do not assert in this function since it is used by the asssertion code! |
Reilly Grant | 39aecc3 | 2018-01-04 00:52:52 | [diff] [blame] | 134 | std::wstring SysUTF8ToWide(StringPiece utf8) { |
[email protected] | 4bdaceb4 | 2008-08-19 13:19:24 | [diff] [blame] | 135 | return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>( |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 136 | utf8, kNarrowStringEncoding, kWideStringEncoding); |
| 137 | } |
| 138 | |
| 139 | std::string SysWideToNativeMB(const std::wstring& wide) { |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 140 | return SysWideToUTF8(wide); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 141 | } |
| 142 | |
Reilly Grant | 39aecc3 | 2018-01-04 00:52:52 | [diff] [blame] | 143 | std::wstring SysNativeMBToWide(StringPiece native_mb) { |
[email protected] | 47944fd | 2008-08-07 19:31:16 | [diff] [blame] | 144 | return SysUTF8ToWide(native_mb); |
[email protected] | 15af80e | 2008-08-07 03:11:42 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | d2a10d1 | 2008-08-22 19:55:26 | [diff] [blame] | 147 | CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) { |
| 148 | return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding); |
| 149 | } |
| 150 | |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 151 | CFStringRef SysUTF16ToCFStringRef(const string16& utf16) { |
| 152 | return STLStringToCFStringWithEncodingsT(utf16, kMediumStringEncoding); |
| 153 | } |
| 154 | |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 155 | NSString* SysUTF8ToNSString(const std::string& utf8) { |
[email protected] | 1671162f | 2011-04-29 15:06:32 | [diff] [blame] | 156 | return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease( |
| 157 | SysUTF8ToCFStringRef(utf8)); |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 160 | NSString* SysUTF16ToNSString(const string16& utf16) { |
[email protected] | 1671162f | 2011-04-29 15:06:32 | [diff] [blame] | 161 | return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease( |
| 162 | SysUTF16ToCFStringRef(utf16)); |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | d2a10d1 | 2008-08-22 19:55:26 | [diff] [blame] | 165 | std::string SysCFStringRefToUTF8(CFStringRef ref) { |
| 166 | return CFStringToSTLStringWithEncodingT<std::string>(ref, |
| 167 | kNarrowStringEncoding); |
| 168 | } |
| 169 | |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 170 | string16 SysCFStringRefToUTF16(CFStringRef ref) { |
| 171 | return CFStringToSTLStringWithEncodingT<string16>(ref, |
| 172 | kMediumStringEncoding); |
| 173 | } |
| 174 | |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 175 | std::string SysNSStringToUTF8(NSString* nsstring) { |
[email protected] | a2494cb | 2009-11-08 19:04:54 | [diff] [blame] | 176 | if (!nsstring) |
| 177 | return std::string(); |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 178 | return SysCFStringRefToUTF8(reinterpret_cast<CFStringRef>(nsstring)); |
| 179 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 180 | |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 181 | string16 SysNSStringToUTF16(NSString* nsstring) { |
[email protected] | a2494cb | 2009-11-08 19:04:54 | [diff] [blame] | 182 | if (!nsstring) |
| 183 | return string16(); |
[email protected] | f224d57 | 2009-02-18 21:39:23 | [diff] [blame] | 184 | return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring)); |
| 185 | } |
| 186 | |
[email protected] | 03d95ac | 2008-10-08 21:02:56 | [diff] [blame] | 187 | } // namespace base |