sys_string_conversions_posix.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <stddef.h>
  6. #include <string.h>
  7. #include <wchar.h>
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. std::string SysWideToUTF8(const std::wstring& wide) {
  13. // In theory this should be using the system-provided conversion rather
  14. // than our ICU, but this will do for now.
  15. return WideToUTF8(wide);
  16. }
  17. std::wstring SysUTF8ToWide(StringPiece utf8) {
  18. // In theory this should be using the system-provided conversion rather
  19. // than our ICU, but this will do for now.
  20. std::wstring out;
  21. UTF8ToWide(utf8.data(), utf8.size(), &out);
  22. return out;
  23. }
  24. #if defined(SYSTEM_NATIVE_UTF8) || BUILDFLAG(IS_ANDROID)
  25. // TODO(port): Consider reverting the OS_ANDROID when we have wcrtomb()
  26. // support and a better understanding of what calls these routines.
  27. std::string SysWideToNativeMB(const std::wstring& wide) {
  28. return WideToUTF8(wide);
  29. }
  30. std::wstring SysNativeMBToWide(StringPiece native_mb) {
  31. return SysUTF8ToWide(native_mb);
  32. }
  33. #else
  34. std::string SysWideToNativeMB(const std::wstring& wide) {
  35. mbstate_t ps;
  36. // Calculate the number of multi-byte characters. We walk through the string
  37. // without writing the output, counting the number of multi-byte characters.
  38. size_t num_out_chars = 0;
  39. memset(&ps, 0, sizeof(ps));
  40. for (auto src : wide) {
  41. // Use a temp buffer since calling wcrtomb with an output of NULL does not
  42. // calculate the output length.
  43. char buf[16];
  44. // Skip NULLs to avoid wcrtomb's special handling of them.
  45. size_t res = src ? wcrtomb(buf, src, &ps) : 0;
  46. switch (res) {
  47. // Handle any errors and return an empty string.
  48. case static_cast<size_t>(-1):
  49. return std::string();
  50. case 0:
  51. // We hit an embedded null byte, keep going.
  52. ++num_out_chars;
  53. break;
  54. default:
  55. num_out_chars += res;
  56. break;
  57. }
  58. }
  59. if (num_out_chars == 0)
  60. return std::string();
  61. std::string out;
  62. out.resize(num_out_chars);
  63. // We walk the input string again, with |i| tracking the index of the
  64. // wide input, and |j| tracking the multi-byte output.
  65. memset(&ps, 0, sizeof(ps));
  66. for (size_t i = 0, j = 0; i < wide.size(); ++i) {
  67. const wchar_t src = wide[i];
  68. // We don't want wcrtomb to do its funkiness for embedded NULLs.
  69. size_t res = src ? wcrtomb(&out[j], src, &ps) : 0;
  70. switch (res) {
  71. // Handle any errors and return an empty string.
  72. case static_cast<size_t>(-1):
  73. return std::string();
  74. case 0:
  75. // We hit an embedded null byte, keep going.
  76. ++j; // Output is already zeroed.
  77. break;
  78. default:
  79. j += res;
  80. break;
  81. }
  82. }
  83. return out;
  84. }
  85. std::wstring SysNativeMBToWide(StringPiece native_mb) {
  86. mbstate_t ps;
  87. // Calculate the number of wide characters. We walk through the string
  88. // without writing the output, counting the number of wide characters.
  89. size_t num_out_chars = 0;
  90. memset(&ps, 0, sizeof(ps));
  91. for (size_t i = 0; i < native_mb.size(); ) {
  92. const char* src = native_mb.data() + i;
  93. size_t res = mbrtowc(nullptr, src, native_mb.size() - i, &ps);
  94. switch (res) {
  95. // Handle any errors and return an empty string.
  96. case static_cast<size_t>(-2):
  97. case static_cast<size_t>(-1):
  98. return std::wstring();
  99. case 0:
  100. // We hit an embedded null byte, keep going.
  101. i += 1;
  102. [[fallthrough]];
  103. default:
  104. i += res;
  105. ++num_out_chars;
  106. break;
  107. }
  108. }
  109. if (num_out_chars == 0)
  110. return std::wstring();
  111. std::wstring out;
  112. out.resize(num_out_chars);
  113. memset(&ps, 0, sizeof(ps)); // Clear the shift state.
  114. // We walk the input string again, with |i| tracking the index of the
  115. // multi-byte input, and |j| tracking the wide output.
  116. for (size_t i = 0, j = 0; i < native_mb.size(); ++j) {
  117. const char* src = native_mb.data() + i;
  118. wchar_t* dst = &out[j];
  119. size_t res = mbrtowc(dst, src, native_mb.size() - i, &ps);
  120. switch (res) {
  121. // Handle any errors and return an empty string.
  122. case static_cast<size_t>(-2):
  123. case static_cast<size_t>(-1):
  124. return std::wstring();
  125. case 0:
  126. i += 1; // Skip null byte.
  127. break;
  128. default:
  129. i += res;
  130. break;
  131. }
  132. }
  133. return out;
  134. }
  135. #endif // defined(SYSTEM_NATIVE_UTF8) || BUILDFLAG(IS_ANDROID)
  136. } // namespace base