1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/strings/sys_string_conversions.h"
7 #import <Foundation/Foundation.h>
11 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/strings/string_piece.h"
19 // Convert the supplied CFString into the specified encoding, and return it as
20 // an STL string of the template type. Returns an empty string on failure.
22 // Do not assert in this function since it is used by the asssertion code!
23 template<typename StringType>
24 static StringType CFStringToSTLStringWithEncodingT(CFStringRef cfstring,
25 CFStringEncoding encoding) {
26 CFIndex length = CFStringGetLength(cfstring);
30 CFRange whole_string = CFRangeMake(0, length);
32 CFIndex converted = CFStringGetBytes(cfstring,
36 false, // isExternalRepresentation
40 if (converted == 0 || out_size == 0)
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;
51 std::vector<typename StringType::value_type> out_buffer(elements);
52 converted = CFStringGetBytes(cfstring,
56 false, // isExternalRepresentation
57 reinterpret_cast<UInt8*>(&out_buffer[0]),
63 out_buffer[elements - 1] = '\0';
64 return StringType(&out_buffer[0], elements - 1);
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.
71 // Do not assert in this function since it is used by the asssertion code!
72 template<typename InStringType, typename OutStringType>
73 static OutStringType STLStringToSTLStringWithEncodingsT(
74 const InStringType& in,
75 CFStringEncoding in_encoding,
76 CFStringEncoding out_encoding) {
77 typename InStringType::size_type in_length = in.length();
79 return OutStringType();
81 base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
83 reinterpret_cast<const UInt8*>(in.data()),
84 in_length * sizeof(typename InStringType::value_type),
89 return OutStringType();
91 return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring,
95 // Given an STL string |in| with an encoding specified by |in_encoding|,
96 // return it as a CFStringRef. Returns NULL on failure.
97 template<typename StringType>
98 static CFStringRef STLStringToCFStringWithEncodingsT(
100 CFStringEncoding in_encoding) {
101 typename StringType::size_type in_length = in.length();
105 return CFStringCreateWithBytes(kCFAllocatorDefault,
106 reinterpret_cast<const UInt8*>(in.data()),
108 sizeof(typename StringType::value_type),
113 // Specify the byte ordering explicitly, otherwise CFString will be confused
114 // when strings don't carry BOMs, as they typically won't.
115 static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8;
116 #ifdef __BIG_ENDIAN__
117 static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16BE;
118 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE;
119 #elif defined(__LITTLE_ENDIAN__)
120 static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16LE;
121 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE;
122 #endif // __LITTLE_ENDIAN__
126 // Do not assert in this function since it is used by the asssertion code!
127 std::string SysWideToUTF8(const std::wstring& wide) {
128 return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>(
129 wide, kWideStringEncoding, kNarrowStringEncoding);
132 // Do not assert in this function since it is used by the asssertion code!
133 std::wstring SysUTF8ToWide(const StringPiece& utf8) {
134 return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>(
135 utf8, kNarrowStringEncoding, kWideStringEncoding);
138 std::string SysWideToNativeMB(const std::wstring& wide) {
139 return SysWideToUTF8(wide);
142 std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
143 return SysUTF8ToWide(native_mb);
146 CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) {
147 return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding);
150 CFStringRef SysUTF16ToCFStringRef(const string16& utf16) {
151 return STLStringToCFStringWithEncodingsT(utf16, kMediumStringEncoding);
154 NSString* SysUTF8ToNSString(const std::string& utf8) {
155 return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
156 SysUTF8ToCFStringRef(utf8));
159 NSString* SysUTF16ToNSString(const string16& utf16) {
160 return (NSString*)base::mac::CFTypeRefToNSObjectAutorelease(
161 SysUTF16ToCFStringRef(utf16));
164 std::string SysCFStringRefToUTF8(CFStringRef ref) {
165 return CFStringToSTLStringWithEncodingT<std::string>(ref,
166 kNarrowStringEncoding);
169 string16 SysCFStringRefToUTF16(CFStringRef ref) {
170 return CFStringToSTLStringWithEncodingT<string16>(ref,
171 kMediumStringEncoding);
174 std::string SysNSStringToUTF8(NSString* nsstring) {
176 return std::string();
177 return SysCFStringRefToUTF8(reinterpret_cast<CFStringRef>(nsstring));
180 string16 SysNSStringToUTF16(NSString* nsstring) {
183 return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring));