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