OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/string_util.h" | 5 #include "base/strings/string_util.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <math.h> | 9 #include <math.h> |
10 #include <stdarg.h> | 10 #include <stdarg.h> |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 return input.find_first_not_of(characters) == StringPiece::npos; | 319 return input.find_first_not_of(characters) == StringPiece::npos; |
320 } | 320 } |
321 | 321 |
322 bool ContainsOnlyChars(const StringPiece16& input, | 322 bool ContainsOnlyChars(const StringPiece16& input, |
323 const StringPiece16& characters) { | 323 const StringPiece16& characters) { |
324 return input.find_first_not_of(characters) == StringPiece16::npos; | 324 return input.find_first_not_of(characters) == StringPiece16::npos; |
325 } | 325 } |
326 | 326 |
327 } // namespace base | 327 } // namespace base |
328 | 328 |
329 #if !defined(WCHAR_T_IS_UTF16) | |
330 bool IsStringASCII(const std::wstring& str); | |
331 #endif | |
332 | |
333 std::string WideToASCII(const std::wstring& wide) { | |
334 DCHECK(IsStringASCII(wide)) << wide; | |
335 return std::string(wide.begin(), wide.end()); | |
336 } | |
337 | |
338 std::string UTF16ToASCII(const string16& utf16) { | |
339 DCHECK(IsStringASCII(utf16)) << utf16; | |
340 return std::string(utf16.begin(), utf16.end()); | |
341 } | |
342 | |
343 template<class STR> | 329 template<class STR> |
344 static bool DoIsStringASCII(const STR& str) { | 330 static bool DoIsStringASCII(const STR& str) { |
345 for (size_t i = 0; i < str.length(); i++) { | 331 for (size_t i = 0; i < str.length(); i++) { |
346 typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i]; | 332 typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i]; |
347 if (c > 0x7F) | 333 if (c > 0x7F) |
348 return false; | 334 return false; |
349 } | 335 } |
350 return true; | 336 return true; |
351 } | 337 } |
352 | 338 |
353 #if !defined(WCHAR_T_IS_UTF16) | |
354 bool IsStringASCII(const std::wstring& str) { | |
355 return DoIsStringASCII(str); | |
356 } | |
357 #endif | |
358 | |
359 bool IsStringASCII(const string16& str) { | |
360 return DoIsStringASCII(str); | |
361 } | |
362 | |
363 bool IsStringASCII(const base::StringPiece& str) { | 339 bool IsStringASCII(const base::StringPiece& str) { |
364 return DoIsStringASCII(str); | 340 return DoIsStringASCII(str); |
365 } | 341 } |
366 | 342 |
| 343 bool IsStringASCII(const base::string16& str) { |
| 344 return DoIsStringASCII(str); |
| 345 } |
| 346 |
367 bool IsStringUTF8(const std::string& str) { | 347 bool IsStringUTF8(const std::string& str) { |
368 const char *src = str.data(); | 348 const char *src = str.data(); |
369 int32 src_len = static_cast<int32>(str.length()); | 349 int32 src_len = static_cast<int32>(str.length()); |
370 int32 char_index = 0; | 350 int32 char_index = 0; |
371 | 351 |
372 while (char_index < src_len) { | 352 while (char_index < src_len) { |
373 int32 code_point; | 353 int32 code_point; |
374 CBU8_NEXT(src, char_index, src_len, code_point); | 354 CBU8_NEXT(src, char_index, src_len, code_point); |
375 if (!base::IsValidCharacter(code_point)) | 355 if (!base::IsValidCharacter(code_point)) |
376 return false; | 356 return false; |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 } | 887 } |
908 | 888 |
909 } // namespace | 889 } // namespace |
910 | 890 |
911 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { | 891 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { |
912 return lcpyT<char>(dst, src, dst_size); | 892 return lcpyT<char>(dst, src, dst_size); |
913 } | 893 } |
914 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { | 894 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { |
915 return lcpyT<wchar_t>(dst, src, dst_size); | 895 return lcpyT<wchar_t>(dst, src, dst_size); |
916 } | 896 } |
OLD | NEW |