| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/string_util.h" | 5 #include "base/string_util.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <ctype.h> | 9 #include <ctype.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 std::string CollapseWhitespaceASCII(const std::string& text, | 499 std::string CollapseWhitespaceASCII(const std::string& text, |
| 500 bool trim_sequences_with_line_breaks) { | 500 bool trim_sequences_with_line_breaks) { |
| 501 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); | 501 return CollapseWhitespaceT(text, trim_sequences_with_line_breaks); |
| 502 } | 502 } |
| 503 | 503 |
| 504 std::string WideToASCII(const std::wstring& wide) { | 504 std::string WideToASCII(const std::wstring& wide) { |
| 505 DCHECK(IsStringASCII(wide)) << wide; | 505 DCHECK(IsStringASCII(wide)) << wide; |
| 506 return std::string(wide.begin(), wide.end()); | 506 return std::string(wide.begin(), wide.end()); |
| 507 } | 507 } |
| 508 | 508 |
| 509 std::wstring ASCIIToWide(const StringPiece& ascii) { | 509 std::wstring ASCIIToWide(const base::StringPiece& ascii) { |
| 510 DCHECK(IsStringASCII(ascii)) << ascii; | 510 DCHECK(IsStringASCII(ascii)) << ascii; |
| 511 return std::wstring(ascii.begin(), ascii.end()); | 511 return std::wstring(ascii.begin(), ascii.end()); |
| 512 } | 512 } |
| 513 | 513 |
| 514 std::string UTF16ToASCII(const string16& utf16) { | 514 std::string UTF16ToASCII(const string16& utf16) { |
| 515 DCHECK(IsStringASCII(utf16)) << utf16; | 515 DCHECK(IsStringASCII(utf16)) << utf16; |
| 516 return std::string(utf16.begin(), utf16.end()); | 516 return std::string(utf16.begin(), utf16.end()); |
| 517 } | 517 } |
| 518 | 518 |
| 519 string16 ASCIIToUTF16(const StringPiece& ascii) { | 519 string16 ASCIIToUTF16(const base::StringPiece& ascii) { |
| 520 DCHECK(IsStringASCII(ascii)) << ascii; | 520 DCHECK(IsStringASCII(ascii)) << ascii; |
| 521 return string16(ascii.begin(), ascii.end()); | 521 return string16(ascii.begin(), ascii.end()); |
| 522 } | 522 } |
| 523 | 523 |
| 524 // Latin1 is just the low range of Unicode, so we can copy directly to convert. | 524 // Latin1 is just the low range of Unicode, so we can copy directly to convert. |
| 525 bool WideToLatin1(const std::wstring& wide, std::string* latin1) { | 525 bool WideToLatin1(const std::wstring& wide, std::string* latin1) { |
| 526 std::string output; | 526 std::string output; |
| 527 output.resize(wide.size()); | 527 output.resize(wide.size()); |
| 528 latin1->clear(); | 528 latin1->clear(); |
| 529 for (size_t i = 0; i < wide.size(); i++) { | 529 for (size_t i = 0; i < wide.size(); i++) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 556 bool IsStringASCII(const std::wstring& str) { | 556 bool IsStringASCII(const std::wstring& str) { |
| 557 return DoIsStringASCII(str); | 557 return DoIsStringASCII(str); |
| 558 } | 558 } |
| 559 | 559 |
| 560 #if !defined(WCHAR_T_IS_UTF16) | 560 #if !defined(WCHAR_T_IS_UTF16) |
| 561 bool IsStringASCII(const string16& str) { | 561 bool IsStringASCII(const string16& str) { |
| 562 return DoIsStringASCII(str); | 562 return DoIsStringASCII(str); |
| 563 } | 563 } |
| 564 #endif | 564 #endif |
| 565 | 565 |
| 566 bool IsStringASCII(const StringPiece& str) { | 566 bool IsStringASCII(const base::StringPiece& str) { |
| 567 return DoIsStringASCII(str); | 567 return DoIsStringASCII(str); |
| 568 } | 568 } |
| 569 | 569 |
| 570 // Helper functions that determine whether the given character begins a | 570 // Helper functions that determine whether the given character begins a |
| 571 // UTF-8 sequence of bytes with the given length. A character satisfies | 571 // UTF-8 sequence of bytes with the given length. A character satisfies |
| 572 // "IsInUTF8Sequence" if it is anything but the first byte in a multi-byte | 572 // "IsInUTF8Sequence" if it is anything but the first byte in a multi-byte |
| 573 // character. | 573 // character. |
| 574 static inline bool IsBegin2ByteUTF8(int c) { | 574 static inline bool IsBegin2ByteUTF8(int c) { |
| 575 return (c & 0xE0) == 0xC0; | 575 return (c & 0xE0) == 0xC0; |
| 576 } | 576 } |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 const char* a_end, | 738 const char* a_end, |
| 739 const char* b) { | 739 const char* b) { |
| 740 return DoLowerCaseEqualsASCII(a_begin, a_end, b); | 740 return DoLowerCaseEqualsASCII(a_begin, a_end, b); |
| 741 } | 741 } |
| 742 bool LowerCaseEqualsASCII(const wchar_t* a_begin, | 742 bool LowerCaseEqualsASCII(const wchar_t* a_begin, |
| 743 const wchar_t* a_end, | 743 const wchar_t* a_end, |
| 744 const char* b) { | 744 const char* b) { |
| 745 return DoLowerCaseEqualsASCII(a_begin, a_end, b); | 745 return DoLowerCaseEqualsASCII(a_begin, a_end, b); |
| 746 } | 746 } |
| 747 | 747 |
| 748 bool EqualsASCII(const string16& a, const StringPiece& b) { | 748 bool EqualsASCII(const string16& a, const base::StringPiece& b) { |
| 749 if (a.length() != b.length()) | 749 if (a.length() != b.length()) |
| 750 return false; | 750 return false; |
| 751 return std::equal(b.begin(), b.end(), a.begin()); | 751 return std::equal(b.begin(), b.end(), a.begin()); |
| 752 } | 752 } |
| 753 | 753 |
| 754 bool StartsWithASCII(const std::string& str, | 754 bool StartsWithASCII(const std::string& str, |
| 755 const std::string& search, | 755 const std::string& search, |
| 756 bool case_sensitive) { | 756 bool case_sensitive) { |
| 757 if (case_sensitive) | 757 if (case_sensitive) |
| 758 return str.compare(0, search.length(), search) == 0; | 758 return str.compare(0, search.length(), search) == 0; |
| (...skipping 922 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1681 // Each input byte creates two output hex characters. | 1681 // Each input byte creates two output hex characters. |
| 1682 std::string ret(size * 2, '\0'); | 1682 std::string ret(size * 2, '\0'); |
| 1683 | 1683 |
| 1684 for (size_t i = 0; i < size; ++i) { | 1684 for (size_t i = 0; i < size; ++i) { |
| 1685 char b = reinterpret_cast<const char*>(bytes)[i]; | 1685 char b = reinterpret_cast<const char*>(bytes)[i]; |
| 1686 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; | 1686 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; |
| 1687 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 1687 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
| 1688 } | 1688 } |
| 1689 return ret; | 1689 return ret; |
| 1690 } | 1690 } |
| OLD | NEW |