|
1 // Copyright (c) 2006-2008 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. |
|
4 |
|
5 #include "base/sys_string_conversions.h" |
|
6 |
|
7 #import <Foundation/Foundation.h> |
|
8 |
|
9 #include <vector> |
|
10 |
|
11 #include "base/scoped_cftyperef.h" |
|
12 #include "base/string_piece.h" |
|
13 |
|
14 namespace base { |
|
15 |
|
16 namespace { |
|
17 |
|
18 // Convert the supplied CFString into the specified encoding, and return it as |
|
19 // an STL string of the template type. Returns an empty string on failure. |
|
20 // |
|
21 // Do not assert in this function since it is used by the asssertion code! |
|
22 template<typename StringType> |
|
23 static StringType CFStringToSTLStringWithEncodingT(CFStringRef cfstring, |
|
24 CFStringEncoding encoding) { |
|
25 CFIndex length = CFStringGetLength(cfstring); |
|
26 if (length == 0) |
|
27 return StringType(); |
|
28 |
|
29 CFRange whole_string = CFRangeMake(0, length); |
|
30 CFIndex out_size; |
|
31 CFIndex converted = CFStringGetBytes(cfstring, |
|
32 whole_string, |
|
33 encoding, |
|
34 0, // lossByte |
|
35 false, // isExternalRepresentation |
|
36 NULL, // buffer |
|
37 0, // maxBufLen |
|
38 &out_size); |
|
39 if (converted == 0 || out_size == 0) |
|
40 return StringType(); |
|
41 |
|
42 // out_size is the number of UInt8-sized units needed in the destination. |
|
43 // A buffer allocated as UInt8 units might not be properly aligned to |
|
44 // contain elements of StringType::value_type. Use a container for the |
|
45 // proper value_type, and convert out_size by figuring the number of |
|
46 // value_type elements per UInt8. Leave room for a NUL terminator. |
|
47 typename StringType::size_type elements = |
|
48 out_size * sizeof(UInt8) / sizeof(typename StringType::value_type) + 1; |
|
49 |
|
50 std::vector<typename StringType::value_type> out_buffer(elements); |
|
51 converted = CFStringGetBytes(cfstring, |
|
52 whole_string, |
|
53 encoding, |
|
54 0, // lossByte |
|
55 false, // isExternalRepresentation |
|
56 reinterpret_cast<UInt8*>(&out_buffer[0]), |
|
57 out_size, |
|
58 NULL); // usedBufLen |
|
59 if (converted == 0) |
|
60 return StringType(); |
|
61 |
|
62 out_buffer[elements - 1] = '\0'; |
|
63 return StringType(&out_buffer[0], elements - 1); |
|
64 } |
|
65 |
|
66 // Given an STL string |in| with an encoding specified by |in_encoding|, |
|
67 // convert it to |out_encoding| and return it as an STL string of the |
|
68 // |OutStringType| template type. Returns an empty string on failure. |
|
69 // |
|
70 // Do not assert in this function since it is used by the asssertion code! |
|
71 template<typename InStringType, typename OutStringType> |
|
72 static OutStringType STLStringToSTLStringWithEncodingsT( |
|
73 const InStringType& in, |
|
74 CFStringEncoding in_encoding, |
|
75 CFStringEncoding out_encoding) { |
|
76 typename InStringType::size_type in_length = in.length(); |
|
77 if (in_length == 0) |
|
78 return OutStringType(); |
|
79 |
|
80 scoped_cftyperef<CFStringRef> cfstring( |
|
81 CFStringCreateWithBytesNoCopy(NULL, |
|
82 reinterpret_cast<const UInt8*>(in.data()), |
|
83 in_length * |
|
84 sizeof(typename InStringType::value_type), |
|
85 in_encoding, |
|
86 false, |
|
87 kCFAllocatorNull)); |
|
88 if (!cfstring) |
|
89 return OutStringType(); |
|
90 |
|
91 return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring, |
|
92 out_encoding); |
|
93 } |
|
94 |
|
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( |
|
99 const StringType& in, |
|
100 CFStringEncoding in_encoding) { |
|
101 typename StringType::size_type in_length = in.length(); |
|
102 if (in_length == 0) |
|
103 return CFSTR(""); |
|
104 |
|
105 return CFStringCreateWithBytes(kCFAllocatorDefault, |
|
106 reinterpret_cast<const UInt8*>(in.data()), |
|
107 in_length * |
|
108 sizeof(typename StringType::value_type), |
|
109 in_encoding, |
|
110 false); |
|
111 } |
|
112 |
|
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 |
|
117 #ifdef __BIG_ENDIAN__ |
|
118 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE; |
|
119 #elif defined(__LITTLE_ENDIAN__) |
|
120 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE; |
|
121 #endif // __LITTLE_ENDIAN__ |
|
122 |
|
123 } // namespace |
|
124 |
|
125 // Do not assert in this function since it is used by the asssertion code! |
|
126 std::string SysWideToUTF8(const std::wstring& wide) { |
|
127 return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>( |
|
128 wide, kWideStringEncoding, kNarrowStringEncoding); |
|
129 } |
|
130 |
|
131 // Do not assert in this function since it is used by the asssertion code! |
|
132 std::wstring SysUTF8ToWide(const StringPiece& utf8) { |
|
133 return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>( |
|
134 utf8, kNarrowStringEncoding, kWideStringEncoding); |
|
135 } |
|
136 |
|
137 std::string SysWideToNativeMB(const std::wstring& wide) { |
|
138 return SysWideToUTF8(wide); |
|
139 } |
|
140 |
|
141 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { |
|
142 return SysUTF8ToWide(native_mb); |
|
143 } |
|
144 |
|
145 } // namespace base |