Skip to content

Commit

Permalink
Rewrite std::string("") to std::string(), Linux edition.
Browse files Browse the repository at this point in the history
This patch was generated by running the empty_string clang tool
across the Chromium Linux compilation database. Implicitly or
explicitly constructing std::string() with a "" argument is
inefficient as the caller needs to emit extra instructions to
pass an argument, and the constructor needlessly copies a byte
into internal storage. Rewriting these instances to simply call
the default constructor appears to save ~14-18 kilobytes on an
optimized release build.

BUG=none

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=193020

Review URL: https://codereview.chromium.org/13145003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@193040 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dcheng@chromium.org committed Apr 9, 2013
1 parent 2bde7e9 commit 007b3f8
Show file tree
Hide file tree
Showing 664 changed files with 5,049 additions and 3,730 deletions.
5 changes: 3 additions & 2 deletions base/debug/trace_event_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ TEST_F(TraceEventTestFixture, TraceCategoriesAfterNestedEnable) {
trace_log->SetEnabled(std::string("foo2"), TraceLog::RECORD_UNTIL_FULL);
EXPECT_TRUE(*trace_log->GetCategoryEnabled("foo2"));
EXPECT_FALSE(*trace_log->GetCategoryEnabled("baz"));
trace_log->SetEnabled(std::string(""), TraceLog::RECORD_UNTIL_FULL);
trace_log->SetEnabled(std::string(), TraceLog::RECORD_UNTIL_FULL);
EXPECT_TRUE(*trace_log->GetCategoryEnabled("foo"));
EXPECT_TRUE(*trace_log->GetCategoryEnabled("baz"));
trace_log->SetDisabled();
Expand All @@ -1558,7 +1558,8 @@ TEST_F(TraceEventTestFixture, TraceCategoriesAfterNestedEnable) {
TEST_F(TraceEventTestFixture, TraceOptionsParsing) {
ManualTestSetUp();

EXPECT_EQ(TraceLog::RECORD_UNTIL_FULL, TraceLog::TraceOptionsFromString(""));
EXPECT_EQ(TraceLog::RECORD_UNTIL_FULL,
TraceLog::TraceOptionsFromString(std::string()));

EXPECT_EQ(TraceLog::RECORD_UNTIL_FULL,
TraceLog::TraceOptionsFromString("record-until-full"));
Expand Down
3 changes: 2 additions & 1 deletion base/file_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ bool CreateDirectory(const FilePath& full_path) {
base::FilePath MakeUniqueDirectory(const base::FilePath& path) {
const int kMaxAttempts = 20;
for (int attempts = 0; attempts < kMaxAttempts; attempts++) {
int uniquifier = GetUniquePathNumber(path, FILE_PATH_LITERAL(""));
int uniquifier =
GetUniquePathNumber(path, base::FilePath::StringType());
if (uniquifier < 0)
break;
base::FilePath test_path = (uniquifier == 0) ? path :
Expand Down
13 changes: 6 additions & 7 deletions base/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1846,15 +1846,14 @@ TEST_F(FileUtilTest, FileEnumeratorTest) {

// create the files
FilePath dir2file = dir2.Append(FILE_PATH_LITERAL("dir2file.txt"));
CreateTextFile(dir2file, L"");
CreateTextFile(dir2file, std::wstring());
FilePath dir2innerfile = dir2inner.Append(FILE_PATH_LITERAL("innerfile.txt"));
CreateTextFile(dir2innerfile, L"");
CreateTextFile(dir2innerfile, std::wstring());
FilePath file1 = temp_dir_.path().Append(FILE_PATH_LITERAL("file1.txt"));
CreateTextFile(file1, L"");
FilePath file2_rel =
dir2.Append(FilePath::kParentDirectory)
.Append(FILE_PATH_LITERAL("file2.txt"));
CreateTextFile(file2_rel, L"");
CreateTextFile(file1, std::wstring());
FilePath file2_rel = dir2.Append(FilePath::kParentDirectory)
.Append(FILE_PATH_LITERAL("file2.txt"));
CreateTextFile(file2_rel, std::wstring());
FilePath file2_abs = temp_dir_.path().Append(FILE_PATH_LITERAL("file2.txt"));

// Only enumerate files.
Expand Down
2 changes: 1 addition & 1 deletion base/files/file_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ string16 FilePath::LossyDisplayName() const {
std::string FilePath::MaybeAsASCII() const {
if (IsStringASCII(path_))
return path_;
return "";
return std::string();
}

std::string FilePath::AsUTF8Unsafe() const {
Expand Down
2 changes: 1 addition & 1 deletion base/i18n/char_iterator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace base {
namespace i18n {

TEST(CharIteratorsTest, TestUTF8) {
std::string empty("");
std::string empty;
UTF8CharIterator empty_iter(&empty);
ASSERT_TRUE(empty_iter.end());
ASSERT_EQ(0, empty_iter.array_pos());
Expand Down
2 changes: 1 addition & 1 deletion base/json/json_value_serializer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ TEST(JSONValueSerializerTest, Roundtrip) {
// initialized with a const string.
ASSERT_FALSE(serializer.Serialize(*root_dict));

std::string test_serialization = "";
std::string test_serialization;
JSONStringValueSerializer mutable_serializer(&test_serialization);
ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
ASSERT_EQ(original_serialization, test_serialization);
Expand Down
2 changes: 1 addition & 1 deletion base/metrics/field_trial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ std::string FieldTrialList::FindFullName(const std::string& name) {
FieldTrial* field_trial = Find(name);
if (field_trial)
return field_trial->group_name();
return "";
return std::string();
}

// static
Expand Down
4 changes: 2 additions & 2 deletions base/metrics/field_trial_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ TEST_F(FieldTrialTest, Registration) {
EXPECT_EQ(name1, trial1->trial_name());
EXPECT_EQ("", trial1->group_name_internal());

trial1->AppendGroup("", 7);
trial1->AppendGroup(std::string(), 7);

EXPECT_EQ(trial1, FieldTrialList::Find(name1));
EXPECT_FALSE(FieldTrialList::Find(name2));
Expand Down Expand Up @@ -221,7 +221,7 @@ TEST_F(FieldTrialTest, OneWinner) {
std::string winner_name;

for (int i = 1; i <= group_count; ++i) {
int might_win = trial->AppendGroup("", 1);
int might_win = trial->AppendGroup(std::string(), 1);

// Because we keep appending groups, we want to see if the last group that
// was added has been assigned or not.
Expand Down
2 changes: 1 addition & 1 deletion base/metrics/statistics_recorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ StatisticsRecorder::~StatisticsRecorder() {
DCHECK(histograms_ && ranges_ && lock_);
if (dump_on_exit_) {
string output;
WriteGraph("", &output);
WriteGraph(std::string(), &output);
DLOG(INFO) << output;
}

Expand Down
2 changes: 1 addition & 1 deletion base/metrics/stats_counters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int* StatsCounter::GetPtr() {
if (counter_id_ == -1) {
counter_id_ = table->FindCounter(name_);
if (table->GetSlot() == 0) {
if (!table->RegisterThread("")) {
if (!table->RegisterThread(std::string())) {
// There is no room for this thread. This thread
// cannot use counters.
counter_id_ = 0;
Expand Down
4 changes: 2 additions & 2 deletions base/metrics/stats_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ int* StatsTable::FindLocation(const char* name) {
// Get the slot for this thread. Try to register
// it if none exists.
int slot = table->GetSlot();
if (!slot && !(slot = table->RegisterThread("")))
return NULL;
if (!slot && !(slot = table->RegisterThread(std::string())))
return NULL;

// Find the counter id for the counter.
std::string str_name(name);
Expand Down
4 changes: 2 additions & 2 deletions base/nix/mime_util_xdg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ size_t IconTheme::MatchesSize(SubDirInfo* info, size_t size) {

std::string IconTheme::ReadLine(FILE* fp) {
if (!fp)
return "";
return std::string();

std::string result = "";
std::string result;
const size_t kBufferSize = 100;
char buffer[kBufferSize];
while ((fgets(buffer, kBufferSize - 1, fp)) != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions base/pickle_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ TEST(PickleTest, UnalignedSize) {

TEST(PickleTest, ZeroLenStr) {
Pickle pickle;
EXPECT_TRUE(pickle.WriteString(""));
EXPECT_TRUE(pickle.WriteString(std::string()));

PickleIterator iter(pickle);
std::string outstr;
Expand All @@ -144,7 +144,7 @@ TEST(PickleTest, ZeroLenStr) {

TEST(PickleTest, ZeroLenWStr) {
Pickle pickle;
EXPECT_TRUE(pickle.WriteWString(L""));
EXPECT_TRUE(pickle.WriteWString(std::wstring()));

PickleIterator iter(pickle);
std::string outstr;
Expand Down
2 changes: 1 addition & 1 deletion base/prefs/pref_change_registrar_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ObserveSetOfPreferencesTest : public testing::Test {
PrefRegistrySimple* registry = pref_service_->registry();
registry->RegisterStringPref(kHomePage, "http://google.com");
registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
registry->RegisterStringPref(kApplicationLocale, "");
registry->RegisterStringPref(kApplicationLocale, std::string());
}

PrefChangeRegistrar* CreatePrefChangeRegistrar() {
Expand Down
2 changes: 1 addition & 1 deletion base/prefs/pref_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST(PrefServiceTest, NoObserverFire) {
Mock::VerifyAndClearExpectations(&obs);

// Clearing the pref should cause the pref to fire.
const StringValue expected_default_value("");
const StringValue expected_default_value((std::string()));
obs.Expect(pref_name, &expected_default_value);
prefs.ClearPref(pref_name);
Mock::VerifyAndClearExpectations(&obs);
Expand Down
2 changes: 1 addition & 1 deletion base/process_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ std::string GetProcStatsFieldAsString(
ProcStatsFields field_num) {
if (field_num < VM_COMM || field_num > VM_STATE) {
NOTREACHED();
return "";
return std::string();
}

if (proc_stats.size() > static_cast<size_t>(field_num))
Expand Down
8 changes: 4 additions & 4 deletions base/process_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,8 @@ TEST_F(ProcessUtilTest, LaunchProcess) {
EXPECT_EQ(0, setenv("BASE_TEST", "testing", 1 /* override */));
EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags));

env_changes.push_back(std::make_pair(std::string("BASE_TEST"),
std::string("")));
env_changes.push_back(
std::make_pair(std::string("BASE_TEST"), std::string()));
EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags));

env_changes[0].second = "foo";
Expand Down Expand Up @@ -800,7 +800,7 @@ TEST_F(ProcessUtilTest, AlterEnvironment) {
delete[] e;

changes.clear();
changes.push_back(std::make_pair(std::string("A"), std::string("")));
changes.push_back(std::make_pair(std::string("A"), std::string()));
e = base::AlterEnvironment(changes, empty);
EXPECT_TRUE(e[0] == NULL);
delete[] e;
Expand All @@ -819,7 +819,7 @@ TEST_F(ProcessUtilTest, AlterEnvironment) {
delete[] e;

changes.clear();
changes.push_back(std::make_pair(std::string("A"), std::string("")));
changes.push_back(std::make_pair(std::string("A"), std::string()));
e = base::AlterEnvironment(changes, a2);
EXPECT_TRUE(e[0] == NULL);
delete[] e;
Expand Down
44 changes: 22 additions & 22 deletions base/string_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ TEST(StringUtilTest, TruncateUTF8ToByteSize) {
std::string output;

// Empty strings and invalid byte_size arguments
EXPECT_FALSE(Truncated("", 0, &output));
EXPECT_FALSE(Truncated(std::string(), 0, &output));
EXPECT_EQ(output, "");
EXPECT_TRUE(Truncated("\xe1\x80\xbf", 0, &output));
EXPECT_EQ(output, "");
Expand Down Expand Up @@ -319,7 +319,7 @@ TEST(StringUtilTest, CollapseWhitespaceASCII) {
}

TEST(StringUtilTest, ContainsOnlyWhitespaceASCII) {
EXPECT_TRUE(ContainsOnlyWhitespaceASCII(""));
EXPECT_TRUE(ContainsOnlyWhitespaceASCII(std::string()));
EXPECT_TRUE(ContainsOnlyWhitespaceASCII(" "));
EXPECT_TRUE(ContainsOnlyWhitespaceASCII("\t"));
EXPECT_TRUE(ContainsOnlyWhitespaceASCII("\t \r \n "));
Expand Down Expand Up @@ -712,7 +712,7 @@ void TokenizeTest() {
EXPECT_EQ(r[2], STR(" three"));
r.clear();

size = Tokenize(STR(""), STR(","), &r);
size = Tokenize(STR(), STR(","), &r);
EXPECT_EQ(0U, size);
ASSERT_EQ(0U, r.size());
r.clear();
Expand Down Expand Up @@ -761,7 +761,7 @@ TEST(StringUtilTest, JoinString) {
in.push_back("c");
EXPECT_EQ("a,b,c", JoinString(in, ','));

in.push_back("");
in.push_back(std::string());
EXPECT_EQ("a,b,c,", JoinString(in, ','));
in.push_back(" ");
EXPECT_EQ("a|b|c|| ", JoinString(in, '|'));
Expand All @@ -780,7 +780,7 @@ TEST(StringUtilTest, JoinStringWithString) {
parts.push_back("c");
EXPECT_EQ("a, b, c", JoinString(parts, separator));

parts.push_back("");
parts.push_back(std::string());
EXPECT_EQ("a, b, c, ", JoinString(parts, separator));
parts.push_back(" ");
EXPECT_EQ("a|b|c|| ", JoinString(parts, "|"));
Expand Down Expand Up @@ -812,21 +812,21 @@ TEST(StringUtilTest, StartsWith) {
EXPECT_TRUE(StartsWithASCII("JavaScript:url", "javascript", false));
EXPECT_FALSE(StartsWithASCII("java", "javascript", true));
EXPECT_FALSE(StartsWithASCII("java", "javascript", false));
EXPECT_FALSE(StartsWithASCII("", "javascript", false));
EXPECT_FALSE(StartsWithASCII("", "javascript", true));
EXPECT_TRUE(StartsWithASCII("java", "", false));
EXPECT_TRUE(StartsWithASCII("java", "", true));
EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", false));
EXPECT_FALSE(StartsWithASCII(std::string(), "javascript", true));
EXPECT_TRUE(StartsWithASCII("java", std::string(), false));
EXPECT_TRUE(StartsWithASCII("java", std::string(), true));

EXPECT_TRUE(StartsWith(L"javascript:url", L"javascript", true));
EXPECT_FALSE(StartsWith(L"JavaScript:url", L"javascript", true));
EXPECT_TRUE(StartsWith(L"javascript:url", L"javascript", false));
EXPECT_TRUE(StartsWith(L"JavaScript:url", L"javascript", false));
EXPECT_FALSE(StartsWith(L"java", L"javascript", true));
EXPECT_FALSE(StartsWith(L"java", L"javascript", false));
EXPECT_FALSE(StartsWith(L"", L"javascript", false));
EXPECT_FALSE(StartsWith(L"", L"javascript", true));
EXPECT_TRUE(StartsWith(L"java", L"", false));
EXPECT_TRUE(StartsWith(L"java", L"", true));
EXPECT_FALSE(StartsWith(std::wstring(), L"javascript", false));
EXPECT_FALSE(StartsWith(std::wstring(), L"javascript", true));
EXPECT_TRUE(StartsWith(L"java", std::wstring(), false));
EXPECT_TRUE(StartsWith(L"java", std::wstring(), true));
}

TEST(StringUtilTest, EndsWith) {
Expand All @@ -838,14 +838,14 @@ TEST(StringUtilTest, EndsWith) {
EXPECT_FALSE(EndsWith(L".plug", L".plugin", false));
EXPECT_FALSE(EndsWith(L"Foo.plugin Bar", L".plugin", true));
EXPECT_FALSE(EndsWith(L"Foo.plugin Bar", L".plugin", false));
EXPECT_FALSE(EndsWith(L"", L".plugin", false));
EXPECT_FALSE(EndsWith(L"", L".plugin", true));
EXPECT_TRUE(EndsWith(L"Foo.plugin", L"", false));
EXPECT_TRUE(EndsWith(L"Foo.plugin", L"", true));
EXPECT_FALSE(EndsWith(std::wstring(), L".plugin", false));
EXPECT_FALSE(EndsWith(std::wstring(), L".plugin", true));
EXPECT_TRUE(EndsWith(L"Foo.plugin", std::wstring(), false));
EXPECT_TRUE(EndsWith(L"Foo.plugin", std::wstring(), true));
EXPECT_TRUE(EndsWith(L".plugin", L".plugin", false));
EXPECT_TRUE(EndsWith(L".plugin", L".plugin", true));
EXPECT_TRUE(EndsWith(L"", L"", false));
EXPECT_TRUE(EndsWith(L"", L"", true));
EXPECT_TRUE(EndsWith(std::wstring(), std::wstring(), false));
EXPECT_TRUE(EndsWith(std::wstring(), std::wstring(), true));
}

TEST(StringUtilTest, GetStringFWithOffsets) {
Expand Down Expand Up @@ -1142,10 +1142,10 @@ TEST(StringUtilTest, ReplaceChars) {
TEST(StringUtilTest, ContainsOnlyChars) {
// Providing an empty list of characters should return false but for the empty
// string.
EXPECT_TRUE(ContainsOnlyChars("", ""));
EXPECT_FALSE(ContainsOnlyChars("Hello", ""));
EXPECT_TRUE(ContainsOnlyChars(std::string(), std::string()));
EXPECT_FALSE(ContainsOnlyChars("Hello", std::string()));

EXPECT_TRUE(ContainsOnlyChars("", "1234"));
EXPECT_TRUE(ContainsOnlyChars(std::string(), "1234"));
EXPECT_TRUE(ContainsOnlyChars("1", "1234"));
EXPECT_TRUE(ContainsOnlyChars("1", "4321"));
EXPECT_TRUE(ContainsOnlyChars("123", "4321"));
Expand Down
3 changes: 2 additions & 1 deletion base/strings/string_split.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ bool SplitStringIntoKeyValuePairs(
success = false;
}
DCHECK_LE(value.size(), 1U);
kv_pairs->push_back(make_pair(key, value.empty()? "" : value[0]));
kv_pairs->push_back(
make_pair(key, value.empty() ? std::string() : value[0]));
}
return success;
}
Expand Down
24 changes: 13 additions & 11 deletions base/strings/string_split_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class SplitStringIntoKeyValuesTest : public testing::Test {
};

TEST_F(SplitStringIntoKeyValuesTest, EmptyInputMultipleValues) {
EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input
'\t', // Key separators
&key, &values));
EXPECT_FALSE(SplitStringIntoKeyValues(std::string(), // Empty input
'\t', // Key separators
&key,
&values));
EXPECT_TRUE(key.empty());
EXPECT_TRUE(values.empty());
}
Expand Down Expand Up @@ -69,9 +70,10 @@ TEST_F(SplitStringIntoKeyValuesTest, KeyWithMultipleValues) {
}

TEST_F(SplitStringIntoKeyValuesTest, EmptyInputSingleValue) {
EXPECT_FALSE(SplitStringIntoKeyValues("", // Empty input
'\t', // Key separators
&key, &values));
EXPECT_FALSE(SplitStringIntoKeyValues(std::string(), // Empty input
'\t', // Key separators
&key,
&values));
EXPECT_TRUE(key.empty());
EXPECT_TRUE(values.empty());
}
Expand Down Expand Up @@ -108,9 +110,9 @@ class SplitStringIntoKeyValuePairsTest : public testing::Test {
};

TEST_F(SplitStringIntoKeyValuePairsTest, EmptyString) {
EXPECT_TRUE(SplitStringIntoKeyValuePairs("",
':', // Key-value delimiters
',', // Key-value pair delims
EXPECT_TRUE(SplitStringIntoKeyValuePairs(std::string(),
':', // Key-value delimiters
',', // Key-value pair delims
&kv_pairs));
EXPECT_TRUE(kv_pairs.empty());
}
Expand Down Expand Up @@ -153,7 +155,7 @@ TEST_F(SplitStringIntoKeyValuePairsTest, DelimiterInValue) {

TEST(SplitStringUsingSubstrTest, EmptyString) {
std::vector<std::string> results;
SplitStringUsingSubstr("", "DELIMITER", &results);
SplitStringUsingSubstr(std::string(), "DELIMITER", &results);
ASSERT_EQ(1u, results.size());
EXPECT_THAT(results, ElementsAre(""));
}
Expand All @@ -162,7 +164,7 @@ TEST(SplitStringUsingSubstrTest, EmptyString) {
TEST(StringUtilTest, SplitString) {
std::vector<std::wstring> r;

SplitString(L"", L',', &r);
SplitString(std::wstring(), L',', &r);
EXPECT_EQ(0U, r.size());
r.clear();

Expand Down
Loading

0 comments on commit 007b3f8

Please sign in to comment.