| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> | 7 #include <wchar.h> |
| 8 | 8 |
| 9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 return std::string(); | 72 return std::string(); |
| 73 | 73 |
| 74 std::string out; | 74 std::string out; |
| 75 out.resize(num_out_chars); | 75 out.resize(num_out_chars); |
| 76 | 76 |
| 77 // We walk the input string again, with |i| tracking the index of the | 77 // We walk the input string again, with |i| tracking the index of the |
| 78 // wide input, and |j| tracking the multi-byte output. | 78 // wide input, and |j| tracking the multi-byte output. |
| 79 memset(&ps, 0, sizeof(ps)); | 79 memset(&ps, 0, sizeof(ps)); |
| 80 for (size_t i = 0, j = 0; i < wide.size(); ++i) { | 80 for (size_t i = 0, j = 0; i < wide.size(); ++i) { |
| 81 const wchar_t src = wide[i]; | 81 const wchar_t src = wide[i]; |
| 82 // We don't want wcrtomb to do it's funkiness for embedded NULLs. | 82 // We don't want wcrtomb to do its funkiness for embedded NULLs. |
| 83 size_t res = src ? wcrtomb(&out[j], src, &ps) : 0; | 83 size_t res = src ? wcrtomb(&out[j], src, &ps) : 0; |
| 84 switch (res) { | 84 switch (res) { |
| 85 // Handle any errors and return an empty string. | 85 // Handle any errors and return an empty string. |
| 86 case static_cast<size_t>(-1): | 86 case static_cast<size_t>(-1): |
| 87 return std::string(); | 87 return std::string(); |
| 88 break; | 88 break; |
| 89 case 0: | 89 case 0: |
| 90 // We hit an embedded null byte, keep going. | 90 // We hit an embedded null byte, keep going. |
| 91 ++j; // Output is already zeroed. | 91 ++j; // Output is already zeroed. |
| 92 break; | 92 break; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 break; | 152 break; |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 | 155 |
| 156 return out; | 156 return out; |
| 157 } | 157 } |
| 158 | 158 |
| 159 #endif // OS_CHROMEOS | 159 #endif // OS_CHROMEOS |
| 160 | 160 |
| 161 } // namespace base | 161 } // namespace base |
| OLD | NEW |