Skip to content

Commit

Permalink
[base] Remove BasicStringPiece::ends_with()
Browse files Browse the repository at this point in the history
In order to make BasicStringPiece API compatible with C++17's
std::basic_string_view this change removes the ends_with() API and
updates call sites to use base::EndsWith instead.

In order to keep this callsites concise, base::EndsWith's
case_sensitivity parameter now defaults to CompareCase::SENSITIVE.

TBR=dcheng

Bug: 1049498
Change-Id: Ie10332e84ae8cc96524c4bf47e5f472a6215eaa4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2358831
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798820}
  • Loading branch information
jdoerrie authored and Commit Bot committed Aug 17, 2020
1 parent e7d7854 commit 4fdd27a
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 80 deletions.
3 changes: 2 additions & 1 deletion base/os_compat_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <math.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "base/strings/string_util.h"

#if !defined(__LP64__)
#include <time64.h>
Expand Down Expand Up @@ -124,7 +125,7 @@ char* mkdtemp(char* path) {
// The last six characters of 'path' must be XXXXXX.
const base::StringPiece kSuffix("XXXXXX");
const int kSuffixLen = kSuffix.length();
if (!base::StringPiece(path, path_len).ends_with(kSuffix)) {
if (!base::EndsWith(base::StringPiece(path, path_len), kSuffix)) {
errno = EINVAL;
return nullptr;
}
Expand Down
8 changes: 0 additions & 8 deletions base/strings/string_piece.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,6 @@ template <typename STRING_TYPE> class BasicStringPiece {
(CharTraits<value_type>::compare(this->ptr_, x.ptr_, x.length_) == 0));
}

// Does "this" end with "x"
constexpr bool ends_with(BasicStringPiece x) const noexcept {
return ((this->length_ >= x.length_) &&
(CharTraits<value_type>::compare(
this->ptr_ + (this->length_ - x.length_), x.ptr_, x.length_) ==
0));
}

// find: Search for a character or substring at a given offset.
size_type find(const BasicStringPiece<STRING_TYPE>& s,
size_type pos = 0) const {
Expand Down
31 changes: 0 additions & 31 deletions base/strings/string_piece_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,18 +555,6 @@ TEST(StringPieceTest, CheckCustom) {
ASSERT_TRUE(!b.starts_with(a));
ASSERT_TRUE(!e.starts_with(a));

// ends with
ASSERT_TRUE(a.ends_with(a));
ASSERT_TRUE(a.ends_with("bar"));
ASSERT_TRUE(a.ends_with(e));
ASSERT_TRUE(b.ends_with(s1));
ASSERT_TRUE(b.ends_with(b));
ASSERT_TRUE(b.ends_with(e));
ASSERT_TRUE(e.ends_with(""));
ASSERT_TRUE(!a.ends_with(b));
ASSERT_TRUE(!b.ends_with(a));
ASSERT_TRUE(!e.ends_with(a));

StringPiece c;
c = {"foobar", 6};
ASSERT_EQ(c, a);
Expand Down Expand Up @@ -615,11 +603,6 @@ TEST(StringPieceTest, CheckComparisons2) {
ASSERT_TRUE(abc.starts_with(abc));
ASSERT_TRUE(abc.starts_with("abcdefghijklm"));
ASSERT_TRUE(!abc.starts_with("abcdefguvwxyz"));

// ends_with
ASSERT_TRUE(abc.ends_with(abc));
ASSERT_TRUE(!abc.ends_with("abcdefguvwxyz"));
ASSERT_TRUE(abc.ends_with("nopqrstuvwxyz"));
}

TYPED_TEST(CommonStringPieceTest, StringCompareNotAmbiguous) {
Expand Down Expand Up @@ -805,20 +788,6 @@ TEST(StringPieceTest, StartsWith) {
static_assert(!piece.starts_with("abcd"), "");
}

TEST(StringPieceTest, EndsWith) {
constexpr StringPiece piece("abc");

static_assert(piece.ends_with(""), "");
static_assert(piece.ends_with("c"), "");
static_assert(piece.ends_with("bc"), "");
static_assert(piece.ends_with("abc"), "");

static_assert(!piece.ends_with("a"), "");
static_assert(!piece.ends_with("ab"), "");

static_assert(!piece.ends_with("abcd"), "");
}

TEST(StringPieceTest, Substr) {
constexpr StringPiece piece = "abcdefghijklmnopqrstuvwxyz";

Expand Down
14 changes: 8 additions & 6 deletions base/strings/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@ BASE_EXPORT bool StartsWith(StringPiece str,
BASE_EXPORT bool StartsWith(StringPiece16 str,
StringPiece16 search_for,
CompareCase case_sensitivity);
BASE_EXPORT bool EndsWith(StringPiece str,
StringPiece search_for,
CompareCase case_sensitivity);
BASE_EXPORT bool EndsWith(StringPiece16 str,
StringPiece16 search_for,
CompareCase case_sensitivity);
BASE_EXPORT bool EndsWith(
StringPiece str,
StringPiece search_for,
CompareCase case_sensitivity = CompareCase::SENSITIVE);
BASE_EXPORT bool EndsWith(
StringPiece16 str,
StringPiece16 search_for,
CompareCase case_sensitivity = CompareCase::SENSITIVE);

// Determines the type of ASCII character, independent of locale (the C
// library versions will change based on locale).
Expand Down
3 changes: 2 additions & 1 deletion base/vlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"

namespace logging {

Expand Down Expand Up @@ -95,7 +96,7 @@ base::StringPiece GetModule(const base::StringPiece& file) {
module = module.substr(0, extension_start);
static const char kInlSuffix[] = "-inl";
static const int kInlSuffixLen = base::size(kInlSuffix) - 1;
if (module.ends_with(kInlSuffix))
if (base::EndsWith(module, kInlSuffix))
module.remove_suffix(kInlSuffixLen);
return module;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ParsedMetadata {
pdl = value.as_string();
} else if (key == "product") {
// Strip parens; ignore anything not enclosed in parens as malformed.
if (value.starts_with("(") && value.ends_with(")")) {
if (value.starts_with("(") && base::EndsWith(value, ")")) {
product = value.substr(1, value.size() - 2).as_string();
}
} else if (key == "rp") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::string ToJSONString(const base::Value& value) {
// Returns UUID if |sink_id| is in the format of "cast:<UUID>" or "dial:<UUID>";
// otherwise returns |sink_id| as UUID.
base::StringPiece ExtractUUID(const base::StringPiece& sink_id) {
if (!sink_id.ends_with(">"))
if (!base::EndsWith(sink_id, ">"))
return sink_id;

size_t prefix_length = 0;
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/profiles/profile_shortcut_manager_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ ShortcutFilenameMatcher::ShortcutFilenameMatcher(
: profile_shortcut_filename_(GetShortcutFilenameForProfile(profile_name)),
lnk_ext_(installer::kLnkExt),
profile_shortcut_name_(profile_shortcut_filename_) {
DCHECK(profile_shortcut_name_.ends_with(lnk_ext_));
DCHECK(base::EndsWith(profile_shortcut_name_, lnk_ext_));
profile_shortcut_name_.remove_suffix(lnk_ext_.size());
}

Expand All @@ -684,12 +684,12 @@ bool ShortcutFilenameMatcher::IsCanonical(
return false;
shortcut_suffix.remove_prefix(profile_shortcut_name_.size());

if (!shortcut_suffix.ends_with(lnk_ext_))
if (!base::EndsWith(shortcut_suffix, lnk_ext_))
return false;
shortcut_suffix.remove_suffix(lnk_ext_.size());

if (shortcut_suffix.size() < 4 || !shortcut_suffix.starts_with(L" (") ||
!shortcut_suffix.ends_with(L")")) {
!base::EndsWith(shortcut_suffix, L")")) {
return false;
}
return std::all_of(shortcut_suffix.begin() + 2, shortcut_suffix.end() - 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
Expand Down Expand Up @@ -1111,8 +1112,8 @@ ChromePasswordProtectionService::GetPlaceholdersForSavedPasswordWarningText()
// 2. if "," + the current priority is a suffix of the
// matching domain The second case covers eTLD+1.
return (domain == *priority_domain_iter) ||
domainStringPiece.ends_with(
"." + *priority_domain_iter);
base::EndsWith(domainStringPiece,
"." + *priority_domain_iter);
}) != matching_domains.end()) {
placeholders.push_back(base::UTF8ToUTF16(matching_domain));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ bool ChromePermissionMessageProvider::IsHostPrivilegeIncrease(
// because we consider having access to "*.domain.com" as
// granting access to "domain.com" then compare the string
// with both the "*" and the "." removed.
if (unmatched.ends_with(stripped_granted) ||
if (base::EndsWith(unmatched, stripped_granted) ||
unmatched == stripped_granted.substr(1)) {
host_matched = true;
break;
Expand Down
2 changes: 1 addition & 1 deletion components/client_update_protocol/ecdsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool ParseETagHeader(const base::StringPiece& etag_header_value_in,
if (etag_header_value.starts_with(kWeakETagPrefix))
etag_header_value.remove_prefix(base::size(kWeakETagPrefix) - 1);
if (etag_header_value.size() >= 2 && etag_header_value.starts_with("\"") &&
etag_header_value.ends_with("\"")) {
base::EndsWith(etag_header_value, "\"")) {
etag_header_value.remove_prefix(1);
etag_header_value.remove_suffix(1);
}
Expand Down
5 changes: 2 additions & 3 deletions components/download/internal/common/download_item_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1604,9 +1604,8 @@ void DownloadItemImpl::Start(
}
RecordDownloadMimeType(mime_type_);
DownloadContent file_type = DownloadContentFromMimeType(mime_type_, false);
bool is_same_host_download =
base::StringPiece(new_create_info.url().host())
.ends_with(new_create_info.site_url.host());
bool is_same_host_download = base::EndsWith(
new_create_info.url().host(), new_create_info.site_url.host());
DownloadConnectionSecurity state = CheckDownloadConnectionSecurity(
new_create_info.url(), new_create_info.url_chain);
DownloadUkmHelper::RecordDownloadStarted(
Expand Down
2 changes: 1 addition & 1 deletion components/flags_ui/flags_test_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ bool IsValidLookingOwner(base::StringPiece owner) {
// file. Instead, just assume any file path ending in 'OWNERS' is valid.
// This doesn't check that the entire filename part of the path is 'OWNERS'
// because sometimes it is instead 'IPC_OWNERS' or similar.
return owner.ends_with("OWNERS");
return base::EndsWith(owner, "OWNERS");
}

// Otherwise, look for something that seems like the username part of an
Expand Down
2 changes: 1 addition & 1 deletion components/google/core/common/google_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool IsPathHomePageBase(base::StringPiece path) {

// Removes a single trailing dot if present in |host|.
void StripTrailingDot(base::StringPiece* host) {
if (host->ends_with("."))
if (base::EndsWith(*host, "."))
host->remove_suffix(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"

namespace password_manager {

Expand Down Expand Up @@ -34,7 +35,7 @@ bool CSVFieldParser::NextField(base::StringPiece* field_contents) {
base::StringPiece(row_.data() + start, position_ - start - 1);

if (field_contents->starts_with("\"")) {
DCHECK(field_contents->ends_with("\"")) << *field_contents;
DCHECK(base::EndsWith(*field_contents, "\"")) << *field_contents;
DCHECK_GE(field_contents->size(), 2u);
field_contents->remove_prefix(1);
field_contents->remove_suffix(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "components/password_manager/core/browser/well_known_change_password_util.h"

#include "base/logging.h"
#include "base/strings/string_util.h"
#include "url/gurl.h"

namespace password_manager {
Expand All @@ -22,7 +23,7 @@ bool IsWellKnownChangePasswordUrl(const GURL& url) {
return false;
base::StringPiece path = url.PathForRequestPiece();
// remove trailing slash if there
if (path.ends_with("/"))
if (base::EndsWith(path, "/"))
path = path.substr(0, path.size() - 1);
return path == kWellKnownChangePasswordPath;
}
Expand Down
6 changes: 3 additions & 3 deletions components/search_engines/template_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ bool TemplateURLRef::ExtractSearchTermsFromURL(
if (source.size() < (search_term_value_prefix_.size() +
search_term_value_suffix_.size()) ||
!source.starts_with(search_term_value_prefix_) ||
!source.ends_with(search_term_value_suffix_))
!base::EndsWith(source, search_term_value_suffix_))
return false;
position =
url::MakeRange(search_term_value_prefix_.size(),
Expand Down Expand Up @@ -579,7 +579,7 @@ bool TemplateURLRef::ExtractSearchTermsFromURL(
if (search_term.size() < (search_term_value_prefix_.size() +
search_term_value_suffix_.size()) ||
!search_term.starts_with(search_term_value_prefix_) ||
!search_term.ends_with(search_term_value_suffix_))
!base::EndsWith(search_term, search_term_value_suffix_))
continue;

key_found = true;
Expand Down Expand Up @@ -863,7 +863,7 @@ bool TemplateURLRef::PathIsEqual(const GURL& url) const {
if (!path_wildcard_present_)
return path == path_prefix_;
return ((path.length() >= path_prefix_.length() + path_suffix_.length()) &&
path.starts_with(path_prefix_) && path.ends_with(path_suffix_));
path.starts_with(path_prefix_) && base::EndsWith(path, path_suffix_));
}

void TemplateURLRef::ParseHostAndSearchTermKey(
Expand Down
2 changes: 1 addition & 1 deletion components/viz/service/display/shader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
constexpr base::StringPiece StripLambda(base::StringPiece shader) {
// Must contain at least "[]() {}".
DCHECK(shader.starts_with("[]() {"));
DCHECK(shader.ends_with("}"));
DCHECK_EQ(shader.back(), '}');
shader.remove_prefix(6);
shader.remove_suffix(1);
return shader;
Expand Down
2 changes: 1 addition & 1 deletion content/common/unique_name_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ constexpr char kDynamicFrameMarker[] = "<!--dynamicFrame";
constexpr size_t kMaxRequestedNameSize = 80;

bool IsNameWithFramePath(base::StringPiece name) {
return name.starts_with(kFramePathPrefix) && name.ends_with("-->") &&
return name.starts_with(kFramePathPrefix) && base::EndsWith(name, "-->") &&
(kFramePathPrefixLength + kFramePathSuffixLength) < name.size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool IsSensitiveGoogleClientUrl(const extensions::WebRequestInfo& request) {

base::StringPiece host = url.host_piece();

while (host.ends_with("."))
while (base::EndsWith(host, "."))
host.remove_suffix(1u);

// Check for "clients[0-9]*.google.com" hosts.
Expand Down
8 changes: 4 additions & 4 deletions extensions/common/url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ bool IsValidPortForScheme(base::StringPiece scheme, base::StringPiece port) {
// out overlap much easier. It seems like there is probably a computer-sciency
// way to solve the general case, but we don't need that yet.
base::StringPiece StripTrailingWildcard(base::StringPiece path) {
if (path.ends_with("*"))
if (base::EndsWith(path, "*"))
path.remove_suffix(1);
return path;
}

// Removes trailing dot from |host_piece| if any.
base::StringPiece CanonicalizeHostForMatching(base::StringPiece host_piece) {
if (host_piece.ends_with("."))
if (base::EndsWith(host_piece, "."))
host_piece.remove_suffix(1);
return host_piece;
}
Expand Down Expand Up @@ -513,7 +513,7 @@ bool URLPattern::MatchesHost(const GURL& test) const {
if (test_host.length() <= (pattern_host.length() + 1))
return false;

if (!test_host.ends_with(pattern_host))
if (!base::EndsWith(test_host, pattern_host))
return false;

return test_host[test_host.length() - pattern_host.length() - 1] == '.';
Expand Down Expand Up @@ -561,7 +561,7 @@ bool URLPattern::MatchesPath(base::StringPiece test) const {
if (path_escaped_.length() == test.length() + 2 &&
base::StartsWith(path_escaped_.c_str(), test,
base::CompareCase::SENSITIVE) &&
base::EndsWith(path_escaped_, "/*", base::CompareCase::SENSITIVE)) {
base::EndsWith(path_escaped_, "/*")) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion headless/lib/browser/headless_print_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/printing/browser/print_manager_utils.h"
#include "components/printing/common/print.mojom.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ HeadlessPrintManager::PageRangeTextToPages(base::StringPiece page_range_text,
range.from = 1;
if (!base::StringToInt(range_string.substr(1), &range.to))
return SYNTAX_ERROR;
} else if (range_string.ends_with("-")) {
} else if (base::EndsWith(range_string, "-")) {
range.to = pages_count;
if (!base::StringToInt(range_string.substr(0, range_string.length() - 1),
&range.from))
Expand Down
6 changes: 3 additions & 3 deletions net/base/url_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool IsHostCharAlphanumeric(char c) {
}

bool IsNormalizedLocalhostTLD(const std::string& host) {
return base::EndsWith(host, ".localhost", base::CompareCase::SENSITIVE);
return base::EndsWith(host, ".localhost");
}

// Helper function used by GetIdentityFromURL. If |escaped_text| can be "safely
Expand Down Expand Up @@ -274,7 +274,7 @@ bool IsSubdomainOf(base::StringPiece subdomain, base::StringPiece superdomain) {

// Superdomain must be suffix of subdomain, and the last character not
// included in the matching substring must be a dot.
if (!subdomain.ends_with(superdomain))
if (!base::EndsWith(subdomain, superdomain))
return false;
subdomain.remove_suffix(superdomain.length());
return subdomain.back() == '.';
Expand Down Expand Up @@ -433,7 +433,7 @@ bool IsGoogleHost(base::StringPiece host) {
// Here it's possible to get away with faster case-sensitive comparisons
// because the list above is all lowercase, and a GURL's host name will
// always be canonicalized to lowercase as well.
if (base::EndsWith(host, suffix, base::CompareCase::SENSITIVE))
if (base::EndsWith(host, suffix))
return true;
}
return false;
Expand Down
Loading

0 comments on commit 4fdd27a

Please sign in to comment.