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

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

Issue 1182453004: Write new Starts/EndsWith and convert FilePath functions to StringPiece. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@string_util
Patch Set: default back Created 5 years, 6 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 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 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 const char* b) { 500 const char* b) {
501 return DoLowerCaseEqualsASCII(a_begin, a_end, b); 501 return DoLowerCaseEqualsASCII(a_begin, a_end, b);
502 } 502 }
503 503
504 bool EqualsASCII(const string16& a, const StringPiece& b) { 504 bool EqualsASCII(const string16& a, const StringPiece& b) {
505 if (a.length() != b.length()) 505 if (a.length() != b.length())
506 return false; 506 return false;
507 return std::equal(b.begin(), b.end(), a.begin()); 507 return std::equal(b.begin(), b.end(), a.begin());
508 } 508 }
509 509
510 bool StartsWithASCII(const std::string& str, 510 template<typename Str>
511 const std::string& search, 511 bool StartsWithT(BasicStringPiece<Str> str,
512 bool case_sensitive) { 512 BasicStringPiece<Str> search_for,
513 if (case_sensitive) 513 CompareCase case_sensitivity) {
514 return str.compare(0, search.length(), search) == 0; 514 if (search_for.size() > str.size())
515 else 515 return false;
516 return base::strncasecmp(str.c_str(), search.c_str(), search.length()) == 0; 516
517 BasicStringPiece<Str> source = str.substr(0, search_for.size());
518
519 switch (case_sensitivity) {
520 case CompareCase::SENSITIVE:
521 return source == search_for;
522
523 case CompareCase::INSENSITIVE_ASCII:
524 return std::equal(
525 search_for.begin(), search_for.end(),
526 source.begin(),
527 base::CaseInsensitiveCompareASCII<typename Str::value_type>());
528
529 default:
530 NOTREACHED();
531 return false;
532 }
533 }
534
535 bool StartsWith(StringPiece str,
536 StringPiece search_for,
537 CompareCase case_sensitivity) {
538 return StartsWithT<std::string>(str, search_for, case_sensitivity);
539 }
540
541 bool StartsWith(StringPiece16 str,
542 StringPiece16 search_for,
543 CompareCase case_sensitivity) {
544 return StartsWithT<string16>(str, search_for, case_sensitivity);
517 } 545 }
518 546
519 bool StartsWith(const string16& str, 547 bool StartsWith(const string16& str,
520 const string16& search, 548 const string16& search,
521 bool case_sensitive) { 549 bool case_sensitive) {
522 if (case_sensitive) { 550 if (!case_sensitive) {
523 return str.compare(0, search.length(), search) == 0; 551 // This function was originally written using the current locale functions
552 // for case-insensitive comparisons. Emulate this behavior until callers
553 // can be converted either to use the case-insensitive ASCII one (most
554 // callers) or ICU functions in base_i18n.
555 if (search.size() > str.size())
556 return false;
557 return std::equal(search.begin(), search.end(), str.begin(),
558 CaseInsensitiveCompare<char16>());
524 } 559 }
525 if (search.size() > str.size()) 560 return StartsWith(StringPiece16(str), StringPiece16(search),
526 return false; 561 CompareCase::SENSITIVE);
527 return std::equal(search.begin(), search.end(), str.begin(),
528 CaseInsensitiveCompare<char16>());
529 } 562 }
530 563
531 template <typename STR> 564 template <typename Str>
532 bool EndsWithT(const STR& str, const STR& search, bool case_sensitive) { 565 bool EndsWithT(BasicStringPiece<Str> str,
533 size_t str_length = str.length(); 566 BasicStringPiece<Str> search_for,
534 size_t search_length = search.length(); 567 CompareCase case_sensitivity) {
535 if (search_length > str_length) 568 if (search_for.size() > str.size())
536 return false; 569 return false;
537 if (case_sensitive) 570
538 return str.compare(str_length - search_length, search_length, search) == 0; 571 BasicStringPiece<Str> source = str.substr(str.size() - search_for.size(),
539 return std::equal(search.begin(), search.end(), 572 search_for.size());
540 str.begin() + (str_length - search_length), 573
541 base::CaseInsensitiveCompare<typename STR::value_type>()); 574 switch (case_sensitivity) {
575 case CompareCase::SENSITIVE:
576 return source == search_for;
577
578 case CompareCase::INSENSITIVE_ASCII:
579 return std::equal(
580 source.begin(), source.end(),
581 search_for.begin(),
582 base::CaseInsensitiveCompareASCII<typename Str::value_type>());
583
584 default:
585 NOTREACHED();
586 return false;
587 }
542 } 588 }
543 589
544 bool EndsWith(const std::string& str, const std::string& search, 590 bool EndsWith(StringPiece str,
545 bool case_sensitive) { 591 StringPiece search_for,
546 return EndsWithT(str, search, case_sensitive); 592 CompareCase case_sensitivity) {
593 return EndsWithT<std::string>(str, search_for, case_sensitivity);
547 } 594 }
548 595
549 bool EndsWith(const string16& str, const string16& search, 596 bool EndsWith(StringPiece16 str,
597 StringPiece16 search_for,
598 CompareCase case_sensitivity) {
599 return EndsWithT<string16>(str, search_for, case_sensitivity);
600 }
601
602 bool EndsWith(const string16& str,
603 const string16& search,
550 bool case_sensitive) { 604 bool case_sensitive) {
551 return EndsWithT(str, search, case_sensitive); 605 if (!case_sensitive) {
606 // This function was originally written using the current locale functions
607 // for case-insensitive comparisons. Emulate this behavior until callers
608 // can be converted either to use the case-insensitive ASCII one (most
609 // callers) or ICU functions in base_i18n.
610 if (search.size() > str.size())
611 return false;
612 return std::equal(search.begin(), search.end(),
613 str.begin() + (str.size() - search.size()),
614 CaseInsensitiveCompare<char16>());
615 }
616 return EndsWith(StringPiece16(str), StringPiece16(search),
617 CompareCase::SENSITIVE);
552 } 618 }
553 619
554 } // namespace base 620 } // namespace base
555 621
556 static const char* const kByteStringsUnlocalized[] = { 622 static const char* const kByteStringsUnlocalized[] = {
557 " B", 623 " B",
558 " kB", 624 " kB",
559 " MB", 625 " MB",
560 " GB", 626 " GB",
561 " TB", 627 " TB",
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 } 1107 }
1042 1108
1043 } // namespace 1109 } // namespace
1044 1110
1045 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) { 1111 size_t base::strlcpy(char* dst, const char* src, size_t dst_size) {
1046 return lcpyT<char>(dst, src, dst_size); 1112 return lcpyT<char>(dst, src, dst_size);
1047 } 1113 }
1048 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) { 1114 size_t base::wcslcpy(wchar_t* dst, const wchar_t* src, size_t dst_size) {
1049 return lcpyT<wchar_t>(dst, src, dst_size); 1115 return lcpyT<wchar_t>(dst, src, dst_size);
1050 } 1116 }
OLDNEW
« base/strings/string_util.h ('K') | « base/strings/string_util.h ('k') | base/sys_info.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698