sys_string_conversions_mac.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.
  4. #include "base/strings/sys_string_conversions.h"
  5. #import <Foundation/Foundation.h>
  6. #include <stddef.h>
  7. #include <vector>
  8. #include "base/mac/foundation_util.h"
  9. #include "base/mac/scoped_cftyperef.h"
  10. #include "base/numerics/safe_conversions.h"
  11. #include "base/strings/string_piece.h"
  12. namespace base {
  13. namespace {
  14. // Converts the supplied CFString into the specified encoding, and returns it as
  15. // a C++ library string of the template type. Returns an empty string on
  16. // failure.
  17. //
  18. // Do not assert in this function since it is used by the assertion code!
  19. template <typename StringType>
  20. StringType CFStringToStringWithEncodingT(CFStringRef cfstring,
  21. CFStringEncoding encoding) {
  22. CFIndex length = CFStringGetLength(cfstring);
  23. if (length == 0)
  24. return StringType();
  25. CFRange whole_string = CFRangeMake(0, length);
  26. CFIndex out_size;
  27. CFIndex converted = CFStringGetBytes(cfstring, whole_string, encoding,
  28. /*lossByte=*/0,
  29. /*isExternalRepresentation=*/false,
  30. /*buffer=*/nullptr,
  31. /*maxBufLen=*/0, &out_size);
  32. if (converted == 0 || out_size <= 0)
  33. return StringType();
  34. // `out_size` is the number of UInt8-sized units needed in the destination.
  35. // A buffer allocated as UInt8 units might not be properly aligned to
  36. // contain elements of StringType::value_type. Use a container for the
  37. // proper value_type, and convert `out_size` by figuring the number of
  38. // value_type elements per UInt8. Leave room for a NUL terminator.
  39. size_t elements = static_cast<size_t>(out_size) * sizeof(UInt8) /
  40. sizeof(typename StringType::value_type) +
  41. 1;
  42. std::vector<typename StringType::value_type> out_buffer(elements);
  43. converted =
  44. CFStringGetBytes(cfstring, whole_string, encoding,
  45. /*lossByte=*/0,
  46. /*isExternalRepresentation=*/false,
  47. reinterpret_cast<UInt8*>(&out_buffer[0]), out_size,
  48. /*usedBufLen=*/nullptr);
  49. if (converted == 0)
  50. return StringType();
  51. out_buffer[elements - 1] = '\0';
  52. return StringType(&out_buffer[0], elements - 1);
  53. }
  54. // Given a C++ library string `in` with an encoding specified by `in_encoding`,
  55. // converts it to `out_encoding` and returns it as a C++ library string of the
  56. // `OutStringType` template type. Returns an empty string on failure.
  57. //
  58. // Do not assert in this function since it is used by the assertion code!
  59. template <typename InStringType, typename OutStringType>
  60. OutStringType StringToStringWithEncodingsT(const InStringType& in,
  61. CFStringEncoding in_encoding,
  62. CFStringEncoding out_encoding) {
  63. typename InStringType::size_type in_length = in.length();
  64. if (in_length == 0)
  65. return OutStringType();
  66. base::ScopedCFTypeRef<CFStringRef> cfstring(CFStringCreateWithBytesNoCopy(
  67. kCFAllocatorDefault, reinterpret_cast<const UInt8*>(in.data()),
  68. checked_cast<CFIndex>(in_length *
  69. sizeof(typename InStringType::value_type)),
  70. in_encoding,
  71. /*isExternalRepresentation=*/false, kCFAllocatorNull));
  72. if (!cfstring)
  73. return OutStringType();
  74. return CFStringToStringWithEncodingT<OutStringType>(cfstring, out_encoding);
  75. }
  76. // Given a StringPiece `in` with an encoding specified by `in_encoding`, returns
  77. // it as a CFStringRef. Returns null on failure.
  78. template <typename CharT>
  79. ScopedCFTypeRef<CFStringRef> StringPieceToCFStringWithEncodingsT(
  80. BasicStringPiece<CharT> in,
  81. CFStringEncoding in_encoding) {
  82. const auto in_length = in.length();
  83. if (in_length == 0)
  84. return ScopedCFTypeRef<CFStringRef>(CFSTR(""), base::scoped_policy::RETAIN);
  85. return ScopedCFTypeRef<CFStringRef>(CFStringCreateWithBytes(
  86. kCFAllocatorDefault, reinterpret_cast<const UInt8*>(in.data()),
  87. checked_cast<CFIndex>(in_length * sizeof(CharT)), in_encoding, false));
  88. }
  89. } // namespace
  90. // The CFStringEncodings used below specify the byte ordering explicitly,
  91. // otherwise CFString will be confused when strings don't carry BOMs, as they
  92. // typically won't.
  93. // Do not assert in this function since it is used by the assertion code!
  94. std::string SysWideToUTF8(const std::wstring& wide) {
  95. return StringToStringWithEncodingsT<std::wstring, std::string>(
  96. wide, kCFStringEncodingUTF32LE, kCFStringEncodingUTF8);
  97. }
  98. // Do not assert in this function since it is used by the assertion code!
  99. std::wstring SysUTF8ToWide(StringPiece utf8) {
  100. return StringToStringWithEncodingsT<StringPiece, std::wstring>(
  101. utf8, kCFStringEncodingUTF8, kCFStringEncodingUTF32LE);
  102. }
  103. std::string SysWideToNativeMB(const std::wstring& wide) {
  104. return SysWideToUTF8(wide);
  105. }
  106. std::wstring SysNativeMBToWide(StringPiece native_mb) {
  107. return SysUTF8ToWide(native_mb);
  108. }
  109. ScopedCFTypeRef<CFStringRef> SysUTF8ToCFStringRef(StringPiece utf8) {
  110. return StringPieceToCFStringWithEncodingsT(utf8, kCFStringEncodingUTF8);
  111. }
  112. ScopedCFTypeRef<CFStringRef> SysUTF16ToCFStringRef(StringPiece16 utf16) {
  113. return StringPieceToCFStringWithEncodingsT(utf16, kCFStringEncodingUTF16LE);
  114. }
  115. NSString* SysUTF8ToNSString(StringPiece utf8) {
  116. return [mac::CFToNSCast(SysUTF8ToCFStringRef(utf8).release()) autorelease];
  117. }
  118. NSString* SysUTF16ToNSString(StringPiece16 utf16) {
  119. return [mac::CFToNSCast(SysUTF16ToCFStringRef(utf16).release()) autorelease];
  120. }
  121. std::string SysCFStringRefToUTF8(CFStringRef ref) {
  122. return CFStringToStringWithEncodingT<std::string>(ref, kCFStringEncodingUTF8);
  123. }
  124. std::u16string SysCFStringRefToUTF16(CFStringRef ref) {
  125. return CFStringToStringWithEncodingT<std::u16string>(
  126. ref, kCFStringEncodingUTF16LE);
  127. }
  128. std::string SysNSStringToUTF8(NSString* nsstring) {
  129. if (!nsstring)
  130. return std::string();
  131. return SysCFStringRefToUTF8(mac::NSToCFCast(nsstring));
  132. }
  133. std::u16string SysNSStringToUTF16(NSString* nsstring) {
  134. if (!nsstring)
  135. return std::u16string();
  136. return SysCFStringRefToUTF16(mac::NSToCFCast(nsstring));
  137. }
  138. } // namespace base