| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/sys_string_conversions.h" | 5 #include "base/sys_string_conversions.h" |
| 6 | 6 |
| 7 #include <wchar.h> | |
| 8 | |
| 9 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
| 10 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 11 | 9 |
| 12 namespace base { | 10 namespace base { |
| 13 | 11 |
| 14 std::string SysWideToUTF8(const std::wstring& wide) { | 12 std::string SysWideToUTF8(const std::wstring& wide) { |
| 15 // In theory this should be using the system-provided conversion rather | 13 // In theory this should be using the system-provided conversion rather |
| 16 // than our ICU, but this will do for now. | 14 // than our ICU, but this will do for now. |
| 17 return WideToUTF8(wide); | 15 return WideToUTF8(wide); |
| 18 } | 16 } |
| 19 std::wstring SysUTF8ToWide(const StringPiece& utf8) { | 17 std::wstring SysUTF8ToWide(const StringPiece& utf8) { |
| 20 // In theory this should be using the system-provided conversion rather | 18 // In theory this should be using the system-provided conversion rather |
| 21 // than our ICU, but this will do for now. | 19 // than our ICU, but this will do for now. |
| 22 std::wstring out; | 20 std::wstring out; |
| 23 UTF8ToWide(utf8.data(), utf8.size(), &out); | 21 UTF8ToWide(utf8.data(), utf8.size(), &out); |
| 24 return out; | 22 return out; |
| 25 } | 23 } |
| 26 | 24 |
| 27 std::string SysWideToNativeMB(const std::wstring& wide) { | 25 std::string SysWideToNativeMB(const std::wstring& wide) { |
| 28 mbstate_t ps; | 26 // TODO(evanm): we can't assume Linux is UTF-8. |
| 29 | 27 return SysWideToUTF8(wide); |
| 30 // Calculate the number of multi-byte characters. We walk through the string | |
| 31 // without writing the output, counting the number of multi-byte characters. | |
| 32 size_t num_out_chars = 0; | |
| 33 memset(&ps, 0, sizeof(ps)); | |
| 34 for (size_t i = 0; i < wide.size(); ++i) { | |
| 35 const wchar_t src = wide[i]; | |
| 36 // Use a temp buf since a output of NULL does not do what we want. | |
| 37 char buf[16]; | |
| 38 // We don't want wcrtomb to do it's funkiness for embedded NULLs. | |
| 39 size_t res = src ? wcrtomb(buf, src, &ps) : 0; | |
| 40 switch (res) { | |
| 41 // Handle any errors and return an empty string. | |
| 42 case -1: | |
| 43 return std::string(); | |
| 44 break; | |
| 45 case 0: | |
| 46 // We hit an embedded null byte, keep going. | |
| 47 ++num_out_chars; | |
| 48 default: | |
| 49 num_out_chars += res; | |
| 50 break; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 if (num_out_chars == 0) | |
| 55 return std::string(); | |
| 56 | |
| 57 std::string out; | |
| 58 out.resize(num_out_chars); | |
| 59 | |
| 60 // We walk the input string again, with |i| tracking the index of the | |
| 61 // wide input, and |j| tracking the multi-byte output. | |
| 62 memset(&ps, 0, sizeof(ps)); | |
| 63 for (size_t i = 0, j = 0; i < wide.size(); ++i) { | |
| 64 const wchar_t src = wide[i]; | |
| 65 // We don't want wcrtomb to do it's funkiness for embedded NULLs. | |
| 66 size_t res = src ? wcrtomb(&out[j], src, &ps) : 0; | |
| 67 switch (res) { | |
| 68 // Handle any errors and return an empty string. | |
| 69 case -1: | |
| 70 return std::string(); | |
| 71 break; | |
| 72 case 0: | |
| 73 // We hit an embedded null byte, keep going. | |
| 74 ++j; // Output is already 0. | |
| 75 default: | |
| 76 j += res; | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 return out; | |
| 82 } | 28 } |
| 83 | 29 |
| 84 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { | 30 std::wstring SysNativeMBToWide(const StringPiece& native_mb) { |
| 85 mbstate_t ps; | 31 // TODO(evanm): we can't assume Linux is UTF-8. |
| 86 | 32 return SysUTF8ToWide(native_mb); |
| 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(NULL, src, native_mb.size() - i, &ps); | |
| 94 switch (res) { | |
| 95 // Handle any errors and return an empty string. | |
| 96 case -2: | |
| 97 case -1: | |
| 98 return std::wstring(); | |
| 99 break; | |
| 100 case 0: | |
| 101 // We hit an embedded null byte, keep going. | |
| 102 i += 1; // Fall through. | |
| 103 default: | |
| 104 i += res; | |
| 105 ++num_out_chars; | |
| 106 break; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 if (num_out_chars == 0) | |
| 111 return std::wstring(); | |
| 112 | |
| 113 std::wstring out; | |
| 114 out.resize(num_out_chars); | |
| 115 | |
| 116 memset(&ps, 0, sizeof(ps)); // Clear the shift state. | |
| 117 // We walk the input string again, with |i| tracking the index of the | |
| 118 // multi-byte input, and |j| tracking the wide output. | |
| 119 for (size_t i = 0, j = 0; i < native_mb.size(); ++j) { | |
| 120 const char* src = native_mb.data() + i; | |
| 121 wchar_t* dst = &out[j]; | |
| 122 size_t res = mbrtowc(dst, src, native_mb.size() - i, &ps); | |
| 123 switch (res) { | |
| 124 // Handle any errors and return an empty string. | |
| 125 case -2: | |
| 126 case -1: | |
| 127 return std::wstring(); | |
| 128 break; | |
| 129 case 0: | |
| 130 i += 1; // Skip null, fall through. | |
| 131 default: | |
| 132 i += res; | |
| 133 break; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 return out; | |
| 138 } | 33 } |
| 139 | 34 |
| 140 } // namespace base | 35 } // namespace base |
| OLD | NEW |