blob: d1e37432f92eac06cdd9e1fb224006313ef4573a [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]47944fd2008-08-07 19:31:165#include "base/sys_string_conversions.h"
[email protected]4bdaceb42008-08-19 13:19:246
[email protected]03d95ac2008-10-08 21:02:567#import <Foundation/Foundation.h>
8
[email protected]47944fd2008-08-07 19:31:169#include <vector>
[email protected]4bdaceb42008-08-19 13:19:2410
[email protected]1671162f2011-04-29 15:06:3211#include "base/mac/foundation_util.h"
[email protected]df0ca6c82010-10-17 04:09:0612#include "base/mac/scoped_cftyperef.h"
[email protected]4bdaceb42008-08-19 13:19:2413#include "base/string_piece.h"
[email protected]15af80e2008-08-07 03:11:4214
15namespace base {
16
17namespace {
18
[email protected]47944fd2008-08-07 19:31:1619// Convert the supplied CFString into the specified encoding, and return it as
[email protected]15af80e2008-08-07 03:11:4220// an STL string of the template type. Returns an empty string on failure.
21//
22// Do not assert in this function since it is used by the asssertion code!
23template<typename StringType>
24static StringType CFStringToSTLStringWithEncodingT(CFStringRef cfstring,
25 CFStringEncoding encoding) {
26 CFIndex length = CFStringGetLength(cfstring);
27 if (length == 0)
28 return StringType();
29
30 CFRange whole_string = CFRangeMake(0, length);
31 CFIndex out_size;
32 CFIndex converted = CFStringGetBytes(cfstring,
33 whole_string,
34 encoding,
35 0, // lossByte
36 false, // isExternalRepresentation
37 NULL, // buffer
38 0, // maxBufLen
39 &out_size);
[email protected]15af80e2008-08-07 03:11:4240 if (converted == 0 || out_size == 0)
41 return StringType();
42
43 // out_size is the number of UInt8-sized units needed in the destination.
44 // A buffer allocated as UInt8 units might not be properly aligned to
45 // contain elements of StringType::value_type. Use a container for the
46 // proper value_type, and convert out_size by figuring the number of
47 // value_type elements per UInt8. Leave room for a NUL terminator.
48 typename StringType::size_type elements =
49 out_size * sizeof(UInt8) / sizeof(typename StringType::value_type) + 1;
50
51 std::vector<typename StringType::value_type> out_buffer(elements);
52 converted = CFStringGetBytes(cfstring,
53 whole_string,
54 encoding,
55 0, // lossByte
56 false, // isExternalRepresentation
57 reinterpret_cast<UInt8*>(&out_buffer[0]),
58 out_size,
59 NULL); // usedBufLen
60 if (converted == 0)
61 return StringType();
62
63 out_buffer[elements - 1] = '\0';
[email protected]cf7e5922008-10-22 20:24:0764 return StringType(&out_buffer[0], elements - 1);
[email protected]15af80e2008-08-07 03:11:4265}
66
67// Given an STL string |in| with an encoding specified by |in_encoding|,
68// convert it to |out_encoding| and return it as an STL string of the
69// |OutStringType| template type. Returns an empty string on failure.
70//
71// Do not assert in this function since it is used by the asssertion code!
[email protected]47944fd2008-08-07 19:31:1672template<typename InStringType, typename OutStringType>
[email protected]15af80e2008-08-07 03:11:4273static OutStringType STLStringToSTLStringWithEncodingsT(
74 const InStringType& in,
75 CFStringEncoding in_encoding,
76 CFStringEncoding out_encoding) {
77 typename InStringType::size_type in_length = in.length();
78 if (in_length == 0)
79 return OutStringType();
80
[email protected]df0ca6c82010-10-17 04:09:0681 base::mac::ScopedCFTypeRef<CFStringRef> cfstring(
[email protected]15af80e2008-08-07 03:11:4282 CFStringCreateWithBytesNoCopy(NULL,
[email protected]a90d613c2008-08-19 16:14:1083 reinterpret_cast<const UInt8*>(in.data()),
[email protected]15af80e2008-08-07 03:11:4284 in_length *
85 sizeof(typename InStringType::value_type),
86 in_encoding,
87 false,
88 kCFAllocatorNull));
[email protected]15af80e2008-08-07 03:11:4289 if (!cfstring)
90 return OutStringType();
91
92 return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring,
93 out_encoding);
94}
95
[email protected]d2a10d12008-08-22 19:55:2696// Given an STL string |in| with an encoding specified by |in_encoding|,
97// return it as a CFStringRef. Returns NULL on failure.
98template<typename StringType>
99static 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]15af80e2008-08-07 03:11:42114// Specify the byte ordering explicitly, otherwise CFString will be confused
115// when strings don't carry BOMs, as they typically won't.
116static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8;
117#ifdef __BIG_ENDIAN__
[email protected]f224d572009-02-18 21:39:23118static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16BE;
[email protected]15af80e2008-08-07 03:11:42119static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE;
[email protected]47944fd2008-08-07 19:31:16120#elif defined(__LITTLE_ENDIAN__)
[email protected]f224d572009-02-18 21:39:23121static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16LE;
[email protected]15af80e2008-08-07 03:11:42122static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE;
[email protected]47944fd2008-08-07 19:31:16123#endif // __LITTLE_ENDIAN__
[email protected]15af80e2008-08-07 03:11:42124
125} // namespace
126
127// Do not assert in this function since it is used by the asssertion code!
128std::string SysWideToUTF8(const std::wstring& wide) {
[email protected]47944fd2008-08-07 19:31:16129 return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>(
[email protected]15af80e2008-08-07 03:11:42130 wide, kWideStringEncoding, kNarrowStringEncoding);
131}
132
133// Do not assert in this function since it is used by the asssertion code!
[email protected]6c82baf2008-08-20 11:17:00134std::wstring SysUTF8ToWide(const StringPiece& utf8) {
[email protected]4bdaceb42008-08-19 13:19:24135 return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>(
[email protected]15af80e2008-08-07 03:11:42136 utf8, kNarrowStringEncoding, kWideStringEncoding);
137}
138
139std::string SysWideToNativeMB(const std::wstring& wide) {
[email protected]47944fd2008-08-07 19:31:16140 return SysWideToUTF8(wide);
[email protected]15af80e2008-08-07 03:11:42141}
142
[email protected]6c82baf2008-08-20 11:17:00143std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
[email protected]47944fd2008-08-07 19:31:16144 return SysUTF8ToWide(native_mb);
[email protected]15af80e2008-08-07 03:11:42145}
146
[email protected]d2a10d12008-08-22 19:55:26147CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) {
148 return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding);
149}
150
[email protected]f224d572009-02-18 21:39:23151CFStringRef SysUTF16ToCFStringRef(const string16& utf16) {
152 return STLStringToCFStringWithEncodingsT(utf16, kMediumStringEncoding);
153}
154
[email protected]03d95ac2008-10-08 21:02:56155NSString* SysUTF8ToNSString(const std::string& utf8) {
[email protected]1671162f2011-04-29 15:06:32156 return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
157 SysUTF8ToCFStringRef(utf8));
[email protected]03d95ac2008-10-08 21:02:56158}
159
[email protected]f224d572009-02-18 21:39:23160NSString* SysUTF16ToNSString(const string16& utf16) {
[email protected]1671162f2011-04-29 15:06:32161 return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
162 SysUTF16ToCFStringRef(utf16));
[email protected]f224d572009-02-18 21:39:23163}
164
[email protected]d2a10d12008-08-22 19:55:26165std::string SysCFStringRefToUTF8(CFStringRef ref) {
166 return CFStringToSTLStringWithEncodingT<std::string>(ref,
167 kNarrowStringEncoding);
168}
169
[email protected]f224d572009-02-18 21:39:23170string16 SysCFStringRefToUTF16(CFStringRef ref) {
171 return CFStringToSTLStringWithEncodingT<string16>(ref,
172 kMediumStringEncoding);
173}
174
[email protected]03d95ac2008-10-08 21:02:56175std::string SysNSStringToUTF8(NSString* nsstring) {
[email protected]a2494cb2009-11-08 19:04:54176 if (!nsstring)
177 return std::string();
[email protected]03d95ac2008-10-08 21:02:56178 return SysCFStringRefToUTF8(reinterpret_cast<CFStringRef>(nsstring));
179}
license.botbf09a502008-08-24 00:55:55180
[email protected]f224d572009-02-18 21:39:23181string16 SysNSStringToUTF16(NSString* nsstring) {
[email protected]a2494cb2009-11-08 19:04:54182 if (!nsstring)
183 return string16();
[email protected]f224d572009-02-18 21:39:23184 return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring));
185}
186
[email protected]03d95ac2008-10-08 21:02:56187} // namespace base