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

Side by Side Diff: base/string_util.cc

Issue 274067: Add string16 support for LowerCaseEqualsASCII, StartsWith, and EndsWith.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 11 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « base/string_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 744
745 // Front-ends for LowerCaseEqualsASCII. 745 // Front-ends for LowerCaseEqualsASCII.
746 bool LowerCaseEqualsASCII(const std::string& a, const char* b) { 746 bool LowerCaseEqualsASCII(const std::string& a, const char* b) {
747 return DoLowerCaseEqualsASCII(a.begin(), a.end(), b); 747 return DoLowerCaseEqualsASCII(a.begin(), a.end(), b);
748 } 748 }
749 749
750 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b) { 750 bool LowerCaseEqualsASCII(const std::wstring& a, const char* b) {
751 return DoLowerCaseEqualsASCII(a.begin(), a.end(), b); 751 return DoLowerCaseEqualsASCII(a.begin(), a.end(), b);
752 } 752 }
753 753
754 #if !defined(WCHAR_T_IS_UTF16)
Matt Perry 2009/10/15 23:06:16 This confused me for a minute. Can you add a comme
darin (slow to review) 2009/10/15 23:13:15 This #if pattern appears throughout this file. It
Matt Perry 2009/10/15 23:18:47 I didn't realize it was a common pattern. In that
755 bool LowerCaseEqualsASCII(const string16& a, const char* b) {
756 return DoLowerCaseEqualsASCII(a.begin(), a.end(), b);
757 }
758 #endif
759
754 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin, 760 bool LowerCaseEqualsASCII(std::string::const_iterator a_begin,
755 std::string::const_iterator a_end, 761 std::string::const_iterator a_end,
756 const char* b) { 762 const char* b) {
757 return DoLowerCaseEqualsASCII(a_begin, a_end, b); 763 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
758 } 764 }
759 765
760 bool LowerCaseEqualsASCII(std::wstring::const_iterator a_begin, 766 bool LowerCaseEqualsASCII(std::wstring::const_iterator a_begin,
761 std::wstring::const_iterator a_end, 767 std::wstring::const_iterator a_end,
762 const char* b) { 768 const char* b) {
763 return DoLowerCaseEqualsASCII(a_begin, a_end, b); 769 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
764 } 770 }
771
772 #if !defined(WCHAR_T_IS_UTF16)
773 bool LowerCaseEqualsASCII(string16::const_iterator a_begin,
774 string16::const_iterator a_end,
775 const char* b) {
776 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
777 }
778 #endif
779
765 bool LowerCaseEqualsASCII(const char* a_begin, 780 bool LowerCaseEqualsASCII(const char* a_begin,
766 const char* a_end, 781 const char* a_end,
767 const char* b) { 782 const char* b) {
768 return DoLowerCaseEqualsASCII(a_begin, a_end, b); 783 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
769 } 784 }
785
770 bool LowerCaseEqualsASCII(const wchar_t* a_begin, 786 bool LowerCaseEqualsASCII(const wchar_t* a_begin,
771 const wchar_t* a_end, 787 const wchar_t* a_end,
772 const char* b) { 788 const char* b) {
773 return DoLowerCaseEqualsASCII(a_begin, a_end, b); 789 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
774 } 790 }
775 791
792 #if !defined(WCHAR_T_IS_UTF16)
793 bool LowerCaseEqualsASCII(const char16* a_begin,
794 const char16* a_end,
795 const char* b) {
796 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
797 }
798 #endif
799
776 bool EqualsASCII(const string16& a, const base::StringPiece& b) { 800 bool EqualsASCII(const string16& a, const base::StringPiece& b) {
777 if (a.length() != b.length()) 801 if (a.length() != b.length())
778 return false; 802 return false;
779 return std::equal(b.begin(), b.end(), a.begin()); 803 return std::equal(b.begin(), b.end(), a.begin());
780 } 804 }
781 805
782 bool StartsWithASCII(const std::string& str, 806 bool StartsWithASCII(const std::string& str,
783 const std::string& search, 807 const std::string& search,
784 bool case_sensitive) { 808 bool case_sensitive) {
785 if (case_sensitive) 809 if (case_sensitive)
786 return str.compare(0, search.length(), search) == 0; 810 return str.compare(0, search.length(), search) == 0;
787 else 811 else
788 return base::strncasecmp(str.c_str(), search.c_str(), search.length()) == 0; 812 return base::strncasecmp(str.c_str(), search.c_str(), search.length()) == 0;
789 } 813 }
790 814
791 bool StartsWith(const std::wstring& str, 815 template <typename STR>
792 const std::wstring& search, 816 bool StartsWithT(const STR& str, const STR& search, bool case_sensitive) {
793 bool case_sensitive) {
794 if (case_sensitive) 817 if (case_sensitive)
795 return str.compare(0, search.length(), search) == 0; 818 return str.compare(0, search.length(), search) == 0;
796 else { 819 else {
797 if (search.size() > str.size()) 820 if (search.size() > str.size())
798 return false; 821 return false;
799 return std::equal(search.begin(), search.end(), str.begin(), 822 return std::equal(search.begin(), search.end(), str.begin(),
800 CaseInsensitiveCompare<wchar_t>()); 823 CaseInsensitiveCompare<typename STR::value_type>());
801 } 824 }
802 } 825 }
803 826
804 bool EndsWith(const std::wstring& str, 827 bool StartsWith(const std::wstring& str, const std::wstring& search,
805 const std::wstring& search, 828 bool case_sensitive) {
806 bool case_sensitive) { 829 return StartsWithT(str, search, case_sensitive);
807 std::wstring::size_type str_length = str.length(); 830 }
808 std::wstring::size_type search_length = search.length(); 831
832 #if !defined(WCHAR_T_IS_UTF16)
833 bool StartsWith(const string16& str, const string16& search,
834 bool case_sensitive) {
835 return StartsWithT(str, search, case_sensitive);
836 }
837 #endif
838
839 template <typename STR>
840 bool EndsWithT(const STR& str, const STR& search, bool case_sensitive) {
841 typename STR::size_type str_length = str.length();
842 typename STR::size_type search_length = search.length();
809 if (search_length > str_length) 843 if (search_length > str_length)
810 return false; 844 return false;
811 if (case_sensitive) { 845 if (case_sensitive) {
812 return str.compare(str_length - search_length, search_length, search) == 0; 846 return str.compare(str_length - search_length, search_length, search) == 0;
813 } else { 847 } else {
814 return std::equal(search.begin(), search.end(), 848 return std::equal(search.begin(), search.end(),
815 str.begin() + (str_length - search_length), 849 str.begin() + (str_length - search_length),
816 CaseInsensitiveCompare<wchar_t>()); 850 CaseInsensitiveCompare<typename STR::value_type>());
817 } 851 }
818 } 852 }
819 853
854 bool EndsWith(const std::wstring& str, const std::wstring& search,
855 bool case_sensitive) {
856 return EndsWithT(str, search, case_sensitive);
857 }
858
859 #if !defined(WCHAR_T_IS_UTF16)
860 bool EndsWith(const string16& str, const string16& search,
861 bool case_sensitive) {
862 return EndsWithT(str, search, case_sensitive);
863 }
864 #endif
865
820 DataUnits GetByteDisplayUnits(int64 bytes) { 866 DataUnits GetByteDisplayUnits(int64 bytes) {
821 // The byte thresholds at which we display amounts. A byte count is displayed 867 // The byte thresholds at which we display amounts. A byte count is displayed
822 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. 868 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
823 // This must match the DataUnits enum. 869 // This must match the DataUnits enum.
824 static const int64 kUnitThresholds[] = { 870 static const int64 kUnitThresholds[] = {
825 0, // DATA_UNITS_BYTE, 871 0, // DATA_UNITS_BYTE,
826 3*1024, // DATA_UNITS_KILOBYTE, 872 3*1024, // DATA_UNITS_KILOBYTE,
827 2*1024*1024, // DATA_UNITS_MEGABYTE, 873 2*1024*1024, // DATA_UNITS_MEGABYTE,
828 1024*1024*1024 // DATA_UNITS_GIGABYTE, 874 1024*1024*1024 // DATA_UNITS_GIGABYTE,
829 }; 875 };
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after
1732 // Each input byte creates two output hex characters. 1778 // Each input byte creates two output hex characters.
1733 std::string ret(size * 2, '\0'); 1779 std::string ret(size * 2, '\0');
1734 1780
1735 for (size_t i = 0; i < size; ++i) { 1781 for (size_t i = 0; i < size; ++i) {
1736 char b = reinterpret_cast<const char*>(bytes)[i]; 1782 char b = reinterpret_cast<const char*>(bytes)[i];
1737 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf]; 1783 ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
1738 ret[(i * 2) + 1] = kHexChars[b & 0xf]; 1784 ret[(i * 2) + 1] = kHexChars[b & 0xf];
1739 } 1785 }
1740 return ret; 1786 return ret;
1741 } 1787 }
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698