Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: base/strings/utf_string_conversions.cc

Issue 1259673002: Make UTF16ToASCII and UTF16TOUTF8 take a StringPiece (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/utf_string_conversions.h" 5 #include "base/strings/utf_string_conversions.h"
6 6
7 #include "base/strings/string_piece.h" 7 #include "base/strings/string_piece.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversion_utils.h" 9 #include "base/strings/utf_string_conversion_utils.h"
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) { 66 bool UTF8ToWide(const char* src, size_t src_len, std::wstring* output) {
67 if (IsStringASCII(StringPiece(src, src_len))) { 67 if (IsStringASCII(StringPiece(src, src_len))) {
68 output->assign(src, src + src_len); 68 output->assign(src, src + src_len);
69 return true; 69 return true;
70 } else { 70 } else {
71 PrepareForUTF16Or32Output(src, src_len, output); 71 PrepareForUTF16Or32Output(src, src_len, output);
72 return ConvertUnicode(src, src_len, output); 72 return ConvertUnicode(src, src_len, output);
73 } 73 }
74 } 74 }
75 75
76 std::wstring UTF8ToWide(const StringPiece& utf8) { 76 std::wstring UTF8ToWide(StringPiece utf8) {
77 if (IsStringASCII(utf8)) { 77 if (IsStringASCII(utf8)) {
78 return std::wstring(utf8.begin(), utf8.end()); 78 return std::wstring(utf8.begin(), utf8.end());
79 } 79 }
80 80
81 std::wstring ret; 81 std::wstring ret;
82 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret); 82 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
83 ConvertUnicode(utf8.data(), utf8.length(), &ret); 83 ConvertUnicode(utf8.data(), utf8.length(), &ret);
84 return ret; 84 return ret;
85 } 85 }
86 86
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { 146 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
147 if (IsStringASCII(StringPiece(src, src_len))) { 147 if (IsStringASCII(StringPiece(src, src_len))) {
148 output->assign(src, src + src_len); 148 output->assign(src, src + src_len);
149 return true; 149 return true;
150 } else { 150 } else {
151 PrepareForUTF16Or32Output(src, src_len, output); 151 PrepareForUTF16Or32Output(src, src_len, output);
152 return ConvertUnicode(src, src_len, output); 152 return ConvertUnicode(src, src_len, output);
153 } 153 }
154 } 154 }
155 155
156 string16 UTF8ToUTF16(const StringPiece& utf8) { 156 string16 UTF8ToUTF16(StringPiece utf8) {
157 if (IsStringASCII(utf8)) { 157 if (IsStringASCII(utf8)) {
158 return string16(utf8.begin(), utf8.end()); 158 return string16(utf8.begin(), utf8.end());
159 } 159 }
160 160
161 string16 ret; 161 string16 ret;
162 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret); 162 PrepareForUTF16Or32Output(utf8.data(), utf8.length(), &ret);
163 // Ignore the success flag of this call, it will do the best it can for 163 // Ignore the success flag of this call, it will do the best it can for
164 // invalid input, which is what we want here. 164 // invalid input, which is what we want here.
165 ConvertUnicode(utf8.data(), utf8.length(), &ret); 165 ConvertUnicode(utf8.data(), utf8.length(), &ret);
166 return ret; 166 return ret;
167 } 167 }
168 168
169 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { 169 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
170 if (IsStringASCII(StringPiece16(src, src_len))) { 170 if (IsStringASCII(StringPiece16(src, src_len))) {
171 output->assign(src, src + src_len); 171 output->assign(src, src + src_len);
172 return true; 172 return true;
173 } else { 173 } else {
174 PrepareForUTF8Output(src, src_len, output); 174 PrepareForUTF8Output(src, src_len, output);
175 return ConvertUnicode(src, src_len, output); 175 return ConvertUnicode(src, src_len, output);
176 } 176 }
177 } 177 }
178 178
179 std::string UTF16ToUTF8(const string16& utf16) { 179 std::string UTF16ToUTF8(StringPiece16 utf16) {
180 if (IsStringASCII(utf16)) { 180 if (IsStringASCII(utf16)) {
181 return std::string(utf16.begin(), utf16.end()); 181 return std::string(utf16.begin(), utf16.end());
182 } 182 }
183 183
184 std::string ret; 184 std::string ret;
185 // Ignore the success flag of this call, it will do the best it can for 185 // Ignore the success flag of this call, it will do the best it can for
186 // invalid input, which is what we want here. 186 // invalid input, which is what we want here.
187 UTF16ToUTF8(utf16.data(), utf16.length(), &ret); 187 UTF16ToUTF8(utf16.data(), utf16.length(), &ret);
188 return ret; 188 return ret;
189 } 189 }
190 190
191 #elif defined(WCHAR_T_IS_UTF16) 191 #elif defined(WCHAR_T_IS_UTF16)
192 // Easy case since we can use the "wide" versions we already wrote above. 192 // Easy case since we can use the "wide" versions we already wrote above.
193 193
194 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) { 194 bool UTF8ToUTF16(const char* src, size_t src_len, string16* output) {
195 return UTF8ToWide(src, src_len, output); 195 return UTF8ToWide(src, src_len, output);
196 } 196 }
197 197
198 string16 UTF8ToUTF16(const StringPiece& utf8) { 198 string16 UTF8ToUTF16(StringPiece utf8) {
199 return UTF8ToWide(utf8); 199 return UTF8ToWide(utf8);
200 } 200 }
201 201
202 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) { 202 bool UTF16ToUTF8(const char16* src, size_t src_len, std::string* output) {
203 return WideToUTF8(src, src_len, output); 203 return WideToUTF8(src, src_len, output);
204 } 204 }
205 205
206 std::string UTF16ToUTF8(const string16& utf16) { 206 std::string UTF16ToUTF8(StringPiece16 utf16) {
207 return WideToUTF8(utf16); 207 if (IsStringASCII(utf16))
208 return std::string(utf16.data(), utf16.data() + utf16.length());
209
210 std::string ret;
211 PrepareForUTF8Output(utf16.data(), utf16.length(), &ret);
212 ConvertUnicode(utf16.data(), utf16.length(), &ret);
213 return ret;
208 } 214 }
209 215
210 #endif 216 #endif
211 217
212 string16 ASCIIToUTF16(const StringPiece& ascii) { 218 string16 ASCIIToUTF16(StringPiece ascii) {
213 DCHECK(IsStringASCII(ascii)) << ascii; 219 DCHECK(IsStringASCII(ascii)) << ascii;
214 return string16(ascii.begin(), ascii.end()); 220 return string16(ascii.begin(), ascii.end());
215 } 221 }
216 222
217 std::string UTF16ToASCII(const string16& utf16) { 223 std::string UTF16ToASCII(StringPiece16 utf16) {
218 DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16); 224 DCHECK(IsStringASCII(utf16)) << UTF16ToUTF8(utf16);
219 return std::string(utf16.begin(), utf16.end()); 225 return std::string(utf16.begin(), utf16.end());
220 } 226 }
221 227
222 } // namespace base 228 } // namespace base
OLDNEW
« no previous file with comments | « base/strings/utf_string_conversions.h ('k') | components/autofill/content/renderer/form_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698