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 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 bool ContainsOnlyChars(const StringPiece& input, | 317 bool ContainsOnlyChars(const StringPiece& input, |
318 const StringPiece& characters) { | 318 const StringPiece& characters) { |
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 bool IsStringUTF8(const StringPiece& str) { |
| 328 const char *src = str.data(); |
| 329 int32 src_len = static_cast<int32>(str.length()); |
| 330 int32 char_index = 0; |
| 331 |
| 332 while (char_index < src_len) { |
| 333 int32 code_point; |
| 334 CBU8_NEXT(src, char_index, src_len, code_point); |
| 335 if (!IsValidCharacter(code_point)) |
| 336 return false; |
| 337 } |
| 338 return true; |
| 339 } |
328 | 340 |
329 template<class STR> | 341 template<class STR> |
330 static bool DoIsStringASCII(const STR& str) { | 342 static bool DoIsStringASCII(const STR& str) { |
331 for (size_t i = 0; i < str.length(); i++) { | 343 for (size_t i = 0; i < str.length(); i++) { |
332 typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i]; | 344 typename ToUnsigned<typename STR::value_type>::Unsigned c = str[i]; |
333 if (c > 0x7F) | 345 if (c > 0x7F) |
334 return false; | 346 return false; |
335 } | 347 } |
336 return true; | 348 return true; |
337 } | 349 } |
338 | 350 |
339 bool IsStringASCII(const base::StringPiece& str) { | 351 bool IsStringASCII(const StringPiece& str) { |
340 return DoIsStringASCII(str); | 352 return DoIsStringASCII(str); |
341 } | 353 } |
342 | 354 |
343 bool IsStringASCII(const base::string16& str) { | 355 bool IsStringASCII(const base::string16& str) { |
344 return DoIsStringASCII(str); | 356 return DoIsStringASCII(str); |
345 } | 357 } |
346 | 358 |
347 bool IsStringUTF8(const std::string& str) { | 359 } // namespace base |
348 const char *src = str.data(); | |
349 int32 src_len = static_cast<int32>(str.length()); | |
350 int32 char_index = 0; | |
351 | |
352 while (char_index < src_len) { | |
353 int32 code_point; | |
354 CBU8_NEXT(src, char_index, src_len, code_point); | |
355 if (!base::IsValidCharacter(code_point)) | |
356 return false; | |
357 } | |
358 return true; | |
359 } | |
360 | 360 |
361 template<typename Iter> | 361 template<typename Iter> |
362 static inline bool DoLowerCaseEqualsASCII(Iter a_begin, | 362 static inline bool DoLowerCaseEqualsASCII(Iter a_begin, |
363 Iter a_end, | 363 Iter a_end, |
364 const char* b) { | 364 const char* b) { |
365 for (Iter it = a_begin; it != a_end; ++it, ++b) { | 365 for (Iter it = a_begin; it != a_end; ++it, ++b) { |
366 if (!*b || base::ToLowerASCII(*it) != *b) | 366 if (!*b || base::ToLowerASCII(*it) != *b) |
367 return false; | 367 return false; |
368 } | 368 } |
369 return *b == 0; | 369 return *b == 0; |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 } | 887 } |
888 | 888 |
889 } // namespace | 889 } // namespace |
890 | 890 |
891 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) { |
892 return lcpyT<char>(dst, src, dst_size); | 892 return lcpyT<char>(dst, src, dst_size); |
893 } | 893 } |
894 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) { |
895 return lcpyT<wchar_t>(dst, src, dst_size); | 895 return lcpyT<wchar_t>(dst, src, dst_size); |
896 } | 896 } |
OLD | NEW |