| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 | 6 |
| 7 #include "base/string_piece.h" | 7 #include "base/string_piece.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/strings/utf_string_conversion_utils.h" | 9 #include "base/strings/utf_string_conversion_utils.h" |
| 10 | 10 |
| 11 namespace base { | 11 using base::PrepareForUTF8Output; |
| 12 using base::PrepareForUTF16Or32Output; |
| 13 using base::ReadUnicodeCharacter; |
| 14 using base::WriteUnicodeCharacter; |
| 12 | 15 |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 15 // Generalized Unicode converter ----------------------------------------------- | 18 // Generalized Unicode converter ----------------------------------------------- |
| 16 | 19 |
| 17 // Converts the given source Unicode character type to the given destination | 20 // Converts the given source Unicode character type to the given destination |
| 18 // Unicode character type as a STL string. The given input buffer and size | 21 // Unicode character type as a STL string. The given input buffer and size |
| 19 // determine the source, and the given output STL string will be replaced by | 22 // determine the source, and the given output STL string will be replaced by |
| 20 // the result. | 23 // the result. |
| 21 template<typename SRC_CHAR, typename DEST_STRING> | 24 template<typename SRC_CHAR, typename DEST_STRING> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // invalid input, which is what we want here. | 56 // invalid input, which is what we want here. |
| 54 WideToUTF8(wide.data(), wide.length(), &ret); | 57 WideToUTF8(wide.data(), wide.length(), &ret); |
| 55 return ret; | 58 return ret; |
| 56 } | 59 } |
| 57 | 60 |
| 58 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { | 61 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { |
| 59 PrepareForUTF16Or32Output(src, src_len, output); | 62 PrepareForUTF16Or32Output(src, src_len, output); |
| 60 return ConvertUnicode(src, src_len, output); | 63 return ConvertUnicode(src, src_len, output); |
| 61 } | 64 } |
| 62 | 65 |
| 63 std::wstring UTF8ToWide(const StringPiece& utf8) { | 66 std::wstring UTF8ToWide(const base::StringPiece& utf8) { |
| 64 std::wstring ret; | 67 std::wstring ret; |
| 65 UTF8ToWide(utf8.data(), utf8.length(), &ret); | 68 UTF8ToWide(utf8.data(), utf8.length(), &ret); |
| 66 return ret; | 69 return ret; |
| 67 } | 70 } |
| 68 | 71 |
| 69 // UTF-16 <-> Wide ------------------------------------------------------------- | 72 // UTF-16 <-> Wide ------------------------------------------------------------- |
| 70 | 73 |
| 71 #if defined(WCHAR_T_IS_UTF16) | 74 #if defined(WCHAR_T_IS_UTF16) |
| 72 | 75 |
| 73 // When wide == UTF-16, then conversions are a NOP. | 76 // When wide == UTF-16, then conversions are a NOP. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 126 |
| 124 // UTF16 <-> UTF8 -------------------------------------------------------------- | 127 // UTF16 <-> UTF8 -------------------------------------------------------------- |
| 125 | 128 |
| 126 #if defined(WCHAR_T_IS_UTF32) | 129 #if defined(WCHAR_T_IS_UTF32) |
| 127 | 130 |
| 128 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 131 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
| 129 PrepareForUTF16Or32Output(src, src_len, output); | 132 PrepareForUTF16Or32Output(src, src_len, output); |
| 130 return ConvertUnicode(src, src_len, output); | 133 return ConvertUnicode(src, src_len, output); |
| 131 } | 134 } |
| 132 | 135 |
| 133 string16 UTF8ToUTF16(const StringPiece& utf8) { | 136 string16 UTF8ToUTF16(const base::StringPiece& utf8) { |
| 134 string16 ret; | 137 string16 ret; |
| 135 // Ignore the success flag of this call, it will do the best it can for | 138 // Ignore the success flag of this call, it will do the best it can for |
| 136 // invalid input, which is what we want here. | 139 // invalid input, which is what we want here. |
| 137 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); | 140 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); |
| 138 return ret; | 141 return ret; |
| 139 } | 142 } |
| 140 | 143 |
| 141 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 144 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
| 142 PrepareForUTF8Output(src, src_len, output); | 145 PrepareForUTF8Output(src, src_len, output); |
| 143 return ConvertUnicode(src, src_len, output); | 146 return ConvertUnicode(src, src_len, output); |
| 144 } | 147 } |
| 145 | 148 |
| 146 std::string UTF16ToUTF8(const string16& utf16) { | 149 std::string UTF16ToUTF8(const string16& utf16) { |
| 147 std::string ret; | 150 std::string ret; |
| 148 // Ignore the success flag of this call, it will do the best it can for | 151 // Ignore the success flag of this call, it will do the best it can for |
| 149 // invalid input, which is what we want here. | 152 // invalid input, which is what we want here. |
| 150 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); | 153 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); |
| 151 return ret; | 154 return ret; |
| 152 } | 155 } |
| 153 | 156 |
| 154 #elif defined(WCHAR_T_IS_UTF16) | 157 #elif defined(WCHAR_T_IS_UTF16) |
| 155 // Easy case since we can use the "wide" versions we already wrote above. | 158 // Easy case since we can use the "wide" versions we already wrote above. |
| 156 | 159 |
| 157 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 160 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
| 158 return UTF8ToWide(src, src_len, output); | 161 return UTF8ToWide(src, src_len, output); |
| 159 } | 162 } |
| 160 | 163 |
| 161 string16 UTF8ToUTF16(const StringPiece& utf8) { | 164 string16 UTF8ToUTF16(const base::StringPiece& utf8) { |
| 162 return UTF8ToWide(utf8); | 165 return UTF8ToWide(utf8); |
| 163 } | 166 } |
| 164 | 167 |
| 165 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { | 168 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { |
| 166 return WideToUTF8(src, src_len, output); | 169 return WideToUTF8(src, src_len, output); |
| 167 } | 170 } |
| 168 | 171 |
| 169 std::string UTF16ToUTF8(const string16& utf16) { | 172 std::string UTF16ToUTF8(const string16& utf16) { |
| 170 return WideToUTF8(utf16); | 173 return WideToUTF8(utf16); |
| 171 } | 174 } |
| 172 | 175 |
| 173 #endif | 176 #endif |
| 174 | 177 |
| 175 std::wstring ASCIIToWide(const StringPiece& ascii) { | 178 std::wstring ASCIIToWide(const base::StringPiece& ascii) { |
| 176 DCHECK(IsStringASCII(ascii)) << ascii; | 179 DCHECK(IsStringASCII(ascii)) << ascii; |
| 177 return std::wstring(ascii.begin(), ascii.end()); | 180 return std::wstring(ascii.begin(), ascii.end()); |
| 178 } | 181 } |
| 179 | 182 |
| 180 string16 ASCIIToUTF16(const StringPiece& ascii) { | 183 string16 ASCIIToUTF16(const base::StringPiece& ascii) { |
| 181 DCHECK(IsStringASCII(ascii)) << ascii; | 184 DCHECK(IsStringASCII(ascii)) << ascii; |
| 182 return string16(ascii.begin(), ascii.end()); | 185 return string16(ascii.begin(), ascii.end()); |
| 183 } | 186 } |
| 184 | |
| 185 } // namespace base | |
| OLD | NEW |