Home | History | Annotate | Download | only in base
      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/foundation_utils_mac.h"
     12 #include "base/mac/scoped_cftyperef.h"
     13 #include "base/string_piece.h"
     14 
     15 namespace base {
     16 
     17 namespace {
     18 
     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.
     21 //
     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);
     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);
     40   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';
     64   return StringType(&out_buffer[0], elements - 1);
     65 }
     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!
     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();
     78   if (in_length == 0)
     79     return OutStringType();
     80 
     81   base::mac::ScopedCFTypeRef<CFStringRef> cfstring(
     82       CFStringCreateWithBytesNoCopy(NULL,
     83                                     reinterpret_cast<const UInt8*>(in.data()),
     84                                     in_length *
     85                                       sizeof(typename InStringType::value_type),
     86                                     in_encoding,
     87                                     false,
     88                                     kCFAllocatorNull));
     89   if (!cfstring)
     90     return OutStringType();
     91 
     92   return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring,
     93                                                          out_encoding);
     94 }
     95 
     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 
    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__
    118 static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16BE;
    119 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE;
    120 #elif defined(__LITTLE_ENDIAN__)
    121 static const CFStringEncoding kMediumStringEncoding = kCFStringEncodingUTF16LE;
    122 static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE;
    123 #endif  // __LITTLE_ENDIAN__
    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) {
    129   return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>(
    130       wide, kWideStringEncoding, kNarrowStringEncoding);
    131 }
    132 
    133 // Do not assert in this function since it is used by the asssertion code!
    134 std::wstring SysUTF8ToWide(const StringPiece& utf8) {
    135   return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>(
    136       utf8, kNarrowStringEncoding, kWideStringEncoding);
    137 }
    138 
    139 std::string SysWideToNativeMB(const std::wstring& wide) {
    140   return SysWideToUTF8(wide);
    141 }
    142 
    143 std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
    144   return SysUTF8ToWide(native_mb);
    145 }
    146 
    147 CFStringRef SysUTF8ToCFStringRef(const std::string& utf8) {
    148   return STLStringToCFStringWithEncodingsT(utf8, kNarrowStringEncoding);
    149 }
    150 
    151 CFStringRef SysUTF16ToCFStringRef(const string16& utf16) {
    152   return STLStringToCFStringWithEncodingsT(utf16, kMediumStringEncoding);
    153 }
    154 
    155 CFStringRef SysWideToCFStringRef(const std::wstring& wide) {
    156   return STLStringToCFStringWithEncodingsT(wide, kWideStringEncoding);
    157 }
    158 
    159 NSString* SysUTF8ToNSString(const std::string& utf8) {
    160   return CFTypeRefToNSObjectAutorelease(SysUTF8ToCFStringRef(utf8));
    161 }
    162 
    163 NSString* SysUTF16ToNSString(const string16& utf16) {
    164   return CFTypeRefToNSObjectAutorelease(SysUTF16ToCFStringRef(utf16));
    165 }
    166 
    167 NSString* SysWideToNSString(const std::wstring& wide) {
    168   return CFTypeRefToNSObjectAutorelease(SysWideToCFStringRef(wide));
    169 }
    170 
    171 std::string SysCFStringRefToUTF8(CFStringRef ref) {
    172   return CFStringToSTLStringWithEncodingT<std::string>(ref,
    173                                                        kNarrowStringEncoding);
    174 }
    175 
    176 string16 SysCFStringRefToUTF16(CFStringRef ref) {
    177   return CFStringToSTLStringWithEncodingT<string16>(ref,
    178                                                     kMediumStringEncoding);
    179 }
    180 
    181 std::wstring SysCFStringRefToWide(CFStringRef ref) {
    182   return CFStringToSTLStringWithEncodingT<std::wstring>(ref,
    183                                                         kWideStringEncoding);
    184 }
    185 
    186 std::string SysNSStringToUTF8(NSString* nsstring) {
    187   if (!nsstring)
    188     return std::string();
    189   return SysCFStringRefToUTF8(reinterpret_cast<CFStringRef>(nsstring));
    190 }
    191 
    192 string16 SysNSStringToUTF16(NSString* nsstring) {
    193   if (!nsstring)
    194     return string16();
    195   return SysCFStringRefToUTF16(reinterpret_cast<CFStringRef>(nsstring));
    196 }
    197 
    198 std::wstring SysNSStringToWide(NSString* nsstring) {
    199   if (!nsstring)
    200     return std::wstring();
    201   return SysCFStringRefToWide(reinterpret_cast<CFStringRef>(nsstring));
    202 }
    203 
    204 }  // namespace base
    205