Skip to content

Commit

Permalink
[base] Remove base::Erase()/base::EraseIf() overloads for std::string
Browse files Browse the repository at this point in the history
They're now replaced with C++20 std::erase()/std::erase_if().

Bug: 1414639
Change-Id: I89e190fb1f1d0e08ffccd34e242dcf812021f268
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5050051
Owners-Override: Kyle Charbonneau <kylechar@chromium.org>
Reviewed-by: Kyle Charbonneau <kylechar@chromium.org>
Commit-Queue: Andrew Rayskiy <greengrape@google.com>
Cr-Commit-Position: refs/heads/main@{#1228504}
  • Loading branch information
GrapeGreen authored and Chromium LUCI CQ committed Nov 23, 2023
1 parent 9900d27 commit 3b49263
Show file tree
Hide file tree
Showing 21 changed files with 36 additions and 116 deletions.
1 change: 0 additions & 1 deletion base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ component("base") {
"containers/cxx20_erase_internal.h",
"containers/cxx20_erase_map.h",
"containers/cxx20_erase_set.h",
"containers/cxx20_erase_string.h",
"containers/cxx20_erase_vector.h",
"containers/enum_set.h",
"containers/extend.h",
Expand Down
1 change: 0 additions & 1 deletion base/containers/cxx20_erase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "base/containers/cxx20_erase_map.h"
#include "base/containers/cxx20_erase_set.h"
#include "base/containers/cxx20_erase_string.h"
#include "base/containers/cxx20_erase_vector.h"

// Erase/EraseIf are based on C++20's uniform container erasure API:
Expand Down
43 changes: 0 additions & 43 deletions base/containers/cxx20_erase_string.h

This file was deleted.

37 changes: 0 additions & 37 deletions base/containers/erase_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,6 @@ struct HashByFirst {
namespace base {
namespace {

TEST(Erase, String) {
const std::pair<std::string, std::string> test_data[] = {
{"", ""},
{"abc", "bc"},
{"abca", "bc"},
};

for (auto test_case : test_data) {
Erase(test_case.first, 'a');
EXPECT_EQ(test_case.second, test_case.first);
}

for (auto test_case : test_data) {
EraseIf(test_case.first, [](char elem) { return elem < 'b'; });
EXPECT_EQ(test_case.second, test_case.first);
}
}

TEST(Erase, String16) {
std::pair<std::u16string, std::u16string> test_data[] = {
{std::u16string(), std::u16string()},
{u"abc", u"bc"},
{u"abca", u"bc"},
};

const std::u16string letters = u"ab";
for (auto test_case : test_data) {
Erase(test_case.first, letters[0]);
EXPECT_EQ(test_case.second, test_case.first);
}

for (auto test_case : test_data) {
EraseIf(test_case.first, [&](short elem) { return elem < letters[1]; });
EXPECT_EQ(test_case.second, test_case.first);
}
}

TEST(Erase, Vector) {
RunEraseTest<std::vector<int>>();
RunEraseIfTest<std::vector<std::pair<int, int>>>();
Expand Down
5 changes: 3 additions & 2 deletions cc/input/main_thread_scrolling_reason.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#include "cc/input/main_thread_scrolling_reason.h"

#include "base/containers/cxx20_erase.h"
#include <string>

#include "base/strings/string_util.h"
#include "base/trace_event/traced_value.h"

Expand All @@ -19,7 +20,7 @@ std::string MainThreadScrollingReason::AsText(uint32_t reasons) {
size_t array_end_pos = result.find(']');
result =
result.substr(array_start_pos + 1, array_end_pos - array_start_pos - 1);
base::Erase(result, '\"');
std::erase(result, '\"');
// Add spaces after all commas.
base::ReplaceChars(result, ",", ", ", &result);
return result;
Expand Down
5 changes: 3 additions & 2 deletions chrome/browser/headless/headless_mode_command_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <algorithm>
#include <memory>
#include <string>
#include <vector>

#include "base/command_line.h"
Expand Down Expand Up @@ -213,7 +214,7 @@ IN_PROC_BROWSER_TEST_F(HeadlessModeDumpDomCommandBrowserTestWithTimeout,

capture_stdout_.StopCapture();
std::string captured_stdout = capture_stdout_.TakeCapturedData();
base::EraseIf(captured_stdout, isspace);
std::erase_if(captured_stdout, isspace);

// Expect about:blank page DOM.
EXPECT_THAT(captured_stdout,
Expand Down Expand Up @@ -290,7 +291,7 @@ IN_PROC_BROWSER_TEST_P(

capture_stdout_.StopCapture();
std::string captured_stdout = capture_stdout_.TakeCapturedData();
base::EraseIf(captured_stdout, isspace);
std::erase_if(captured_stdout, isspace);

if (delay_response()) {
EXPECT_THAT(result,
Expand Down
5 changes: 3 additions & 2 deletions chrome/browser/pdf/pdf_extension_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#include "chrome/browser/pdf/pdf_extension_util.h"

#include "base/containers/cxx20_erase.h"
#include <string>

#include "base/feature_list.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
Expand Down Expand Up @@ -171,7 +172,7 @@ void AddPdfViewerStrings(base::Value::Dict* dict) {

#if BUILDFLAG(IS_CHROMEOS_ASH)
std::u16string edit_string = l10n_util::GetStringUTF16(IDS_EDIT);
base::Erase(edit_string, '&');
std::erase(edit_string, '&');
dict->Set("editButton", edit_string);
#endif

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/commander/simple_command_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "chrome/browser/ui/commander/simple_command_source.h"

#include <string>
#include <vector>

#include "base/containers/cxx20_erase.h"
#include "base/functional/bind.h"
#include "base/i18n/case_conversion.h"
#include "base/no_destructor.h"
Expand Down Expand Up @@ -84,7 +84,7 @@ CommandSource::CommandResults SimpleCommandSource::GetCommands(
if (!chrome::IsCommandEnabled(browser, command_spec.id))
continue;
std::u16string title = command_spec.title;
base::Erase(title, '&');
std::erase(title, '&');
double score = finder.Find(title, &ranges);
if (score == 0)
continue;
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/ui/commander/tab_command_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <numeric>
#include <string>

#include "base/containers/cxx20_erase.h"
#include "base/functional/bind.h"
#include "base/ranges/algorithm.h"
#include "chrome/app/chrome_command_ids.h"
Expand Down Expand Up @@ -285,7 +284,7 @@ CommandSource::CommandResults MoveTabsToWindowCommandsForWindowsMatching(
CommandSource::CommandResults results;
// Add "New Window", if appropriate. It should score highest with no input.
std::u16string new_window_title = l10n_util::GetStringUTF16(IDS_NEW_WINDOW);
base::Erase(new_window_title, '&');
std::erase(new_window_title, '&');
std::unique_ptr<CommandItem> item;
if (input.empty()) {
item = std::make_unique<CommandItem>(new_window_title, .99,
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/ui/webui/bookmarks/bookmarks_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <string>
#include <utility>

#include "base/containers/cxx20_erase.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/bookmarks/bookmarks_message_handler.h"
Expand Down Expand Up @@ -40,7 +39,7 @@ void AddLocalizedString(content::WebUIDataSource* source,
const std::string& message,
int id) {
std::u16string str = l10n_util::GetStringUTF16(id);
base::Erase(str, '&');
std::erase(str, '&');
source->AddString(message, str);
}

Expand Down
5 changes: 3 additions & 2 deletions chrome/browser/ui/webui/webui_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#include "chrome/browser/ui/webui/webui_util.h"

#include "base/containers/cxx20_erase.h"
#include <string>

#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/web_ui_data_source.h"
Expand Down Expand Up @@ -82,7 +83,7 @@ void AddLocalizedString(content::WebUIDataSource* source,
const std::string& message,
int id) {
std::u16string str = l10n_util::GetStringUTF16(id);
base::Erase(str, '&');
std::erase(str, '&');
source->AddString(message, str);
}

Expand Down
5 changes: 4 additions & 1 deletion chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3082,7 +3082,10 @@ if (!is_android) {
sources += [
"../browser/devtools/protocol/devtools_printtopdf_browsertest.cc",
]
deps += [ "//components/printing/browser" ]
deps += [
"//components/printing/browser",
"//pdf",
]
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions chrome/updater/util/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "base/base_paths.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/cxx20_erase.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
Expand Down Expand Up @@ -302,7 +301,7 @@ GURL AppendQueryParameter(const GURL& url,

std::wstring GetTaskNamePrefix(UpdaterScope scope) {
std::wstring task_name = GetTaskDisplayName(scope);
base::EraseIf(task_name, base::IsAsciiWhitespace<wchar_t>);
std::erase_if(task_name, base::IsAsciiWhitespace<wchar_t>);
return task_name;
}

Expand Down
3 changes: 1 addition & 2 deletions chrome/updater/util/win_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "base/check.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/cxx20_erase.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/debug/alias.h"
Expand Down Expand Up @@ -460,7 +459,7 @@ std::string GetUACState() {

std::wstring GetServiceName(bool is_internal_service) {
std::wstring service_name = GetServiceDisplayName(is_internal_service);
base::EraseIf(service_name, base::IsAsciiWhitespace<wchar_t>);
std::erase_if(service_name, base::IsAsciiWhitespace<wchar_t>);
return service_name;
}

Expand Down
4 changes: 2 additions & 2 deletions components/gcm_driver/instance_id/instance_id_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#include <algorithm>
#include <memory>
#include <string>

#include "base/base64.h"
#include "base/containers/cxx20_erase.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
Expand Down Expand Up @@ -249,7 +249,7 @@ void InstanceIDImpl::EnsureIDGenerated() {
&id_);
std::replace(id_.begin(), id_.end(), '+', '-');
std::replace(id_.begin(), id_.end(), '/', '_');
base::Erase(id_, '=');
std::erase(id_, '=');

creation_time_ = base::Time::Now();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <algorithm>
#include <memory>
#include <string>
#include "base/containers/cxx20_erase.h"

#include "base/logging.h"
#include "base/strings/string_util.h"
#include "third_party/icu/source/common/unicode/uniset.h"
Expand Down Expand Up @@ -783,7 +783,7 @@ std::string ChineseScriptClassifier::Classify(const std::string& input) const {
base::TruncateUTF8ToByteSize(input, 500, &input_subset);

// Remove whitespace since transliterators may not preserve it.
base::EraseIf(input_subset, base::IsAsciiWhitespace<char>);
std::erase_if(input_subset, base::IsAsciiWhitespace<char>);

// Convert the input to icu::UnicodeString so we can iterate over codepoints.
icu::UnicodeString input_codepoints =
Expand Down
5 changes: 2 additions & 3 deletions headless/test/headless_command_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// found in the LICENSE file.

#include <cmath>
#include <optional>
#include <string>

#include <optional>
#include "base/command_line.h"
#include "base/containers/cxx20_erase.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
Expand Down Expand Up @@ -355,7 +354,7 @@ IN_PROC_BROWSER_TEST_P(HeadlessDumpDomSubResourceTimeoutCommandBrowserTest,
capture_stdout.StopCapture();

std::string captured_stdout_data = capture_stdout.TakeCapturedData();
base::EraseIf(captured_stdout_data, isspace);
std::erase_if(captured_stdout_data, isspace);

if (delay_response()) {
EXPECT_THAT(result(),
Expand Down
3 changes: 1 addition & 2 deletions media/test/pipeline_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <utility>

#include "base/command_line.h"
#include "base/containers/cxx20_erase.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
Expand Down Expand Up @@ -525,7 +524,7 @@ class MSEChangeTypeTest
std::string s = base::StrCat({std::get<0>(info.param).filename, "_AND_",
std::get<1>(info.param).filename});
// Strip out invalid param name characters.
base::EraseIf(s, [](char c) {
std::erase_if(s, [](char c) {
return !absl::ascii_isalnum(static_cast<unsigned char>(c)) && c != '_';
});
return s;
Expand Down
4 changes: 2 additions & 2 deletions net/base/data_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "net/base/data_url.h"

#include <string>
#include <string_view>

#include "base/base64.h"
#include "base/containers/cxx20_erase.h"
#include "base/feature_list.h"
#include "base/features.h"
#include "base/ranges/algorithm.h"
Expand Down Expand Up @@ -153,7 +153,7 @@ bool DataURL::Parse(const GURL& url,
if (!(mime_type_value.compare(0, 5, "text/") == 0 ||
mime_type_value.find("xml") != std::string::npos)) {
temp = std::string(raw_body);
base::EraseIf(temp, base::IsAsciiWhitespace<char>);
std::erase_if(temp, base::IsAsciiWhitespace<char>);
raw_body = temp;
}

Expand Down
Loading

0 comments on commit 3b49263

Please sign in to comment.