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/utf_string_conversion_utils.h" | 9 #include "base/utf_string_conversion_utils.h" |
10 | 10 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 // UTF16 <-> UTF8 -------------------------------------------------------------- | 127 // UTF16 <-> UTF8 -------------------------------------------------------------- |
128 | 128 |
129 #if defined(WCHAR_T_IS_UTF32) | 129 #if defined(WCHAR_T_IS_UTF32) |
130 | 130 |
131 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 131 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
132 PrepareForUTF16Or32Output(src, src_len, output); | 132 PrepareForUTF16Or32Output(src, src_len, output); |
133 return ConvertUnicode(src, src_len, output); | 133 return ConvertUnicode(src, src_len, output); |
134 } | 134 } |
135 | 135 |
136 string16 UTF8ToUTF16(const std::string& utf8) { | 136 string16 UTF8ToUTF16(const base::StringPiece& utf8) { |
137 string16 ret; | 137 string16 ret; |
138 // 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 |
139 // invalid input, which is what we want here. | 139 // invalid input, which is what we want here. |
140 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); | 140 UTF8ToUTF16(utf8.data(), utf8.length(), &ret); |
141 return ret; | 141 return ret; |
142 } | 142 } |
143 | 143 |
144 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) { |
145 PrepareForUTF8Output(src, src_len, output); | 145 PrepareForUTF8Output(src, src_len, output); |
146 return ConvertUnicode(src, src_len, output); | 146 return ConvertUnicode(src, src_len, output); |
147 } | 147 } |
148 | 148 |
149 std::string UTF16ToUTF8(const string16& utf16) { | 149 std::string UTF16ToUTF8(const string16& utf16) { |
150 std::string ret; | 150 std::string ret; |
151 // 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 |
152 // invalid input, which is what we want here. | 152 // invalid input, which is what we want here. |
153 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); | 153 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); |
154 return ret; | 154 return ret; |
155 } | 155 } |
156 | 156 |
157 #elif defined(WCHAR_T_IS_UTF16) | 157 #elif defined(WCHAR_T_IS_UTF16) |
158 // 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. |
159 | 159 |
160 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { | 160 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { |
161 return UTF8ToWide(src, src_len, output); | 161 return UTF8ToWide(src, src_len, output); |
162 } | 162 } |
163 | 163 |
164 string16 UTF8ToUTF16(const std::string& utf8) { | 164 string16 UTF8ToUTF16(const base::StringPiece& utf8) { |
165 return UTF8ToWide(utf8); | 165 return UTF8ToWide(utf8); |
166 } | 166 } |
167 | 167 |
168 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) { |
169 return WideToUTF8(src, src_len, output); | 169 return WideToUTF8(src, src_len, output); |
170 } | 170 } |
171 | 171 |
172 std::string UTF16ToUTF8(const string16& utf16) { | 172 std::string UTF16ToUTF8(const string16& utf16) { |
173 return WideToUTF8(utf16); | 173 return WideToUTF8(utf16); |
174 } | 174 } |
175 | 175 |
176 #endif | 176 #endif |
177 | 177 |
178 std::wstring ASCIIToWide(const char* ascii) { | 178 std::wstring ASCIIToWide(const base::StringPiece& ascii) { |
179 DCHECK(IsStringASCII(ascii)) << ascii; | |
180 return std::wstring(ascii, &ascii[strlen(ascii)]); | |
181 } | |
182 | |
183 std::wstring ASCIIToWide(const std::string& ascii) { | |
184 DCHECK(IsStringASCII(ascii)) << ascii; | 179 DCHECK(IsStringASCII(ascii)) << ascii; |
185 return std::wstring(ascii.begin(), ascii.end()); | 180 return std::wstring(ascii.begin(), ascii.end()); |
186 } | 181 } |
187 | 182 |
188 string16 ASCIIToUTF16(const char* ascii) { | 183 string16 ASCIIToUTF16(const base::StringPiece& ascii) { |
189 DCHECK(IsStringASCII(ascii)) << ascii; | |
190 return string16(ascii, &ascii[strlen(ascii)]); | |
191 } | |
192 | |
193 string16 ASCIIToUTF16(const std::string& ascii) { | |
194 DCHECK(IsStringASCII(ascii)) << ascii; | 184 DCHECK(IsStringASCII(ascii)) << ascii; |
195 return string16(ascii.begin(), ascii.end()); | 185 return string16(ascii.begin(), ascii.end()); |
196 } | 186 } |
OLD | NEW |