Skip to content

Commit 069e4c4

Browse files
Googlerjonsimantov
authored andcommitted
Optimize calls to std::string::find() and friends for a single char.
The character literal overload is more efficient. go/lsc-performance-faster-string-find Tested: TAP for global presubmit queue http://test/OCL:347655100:BASE:347631998:1608131503084:adc733c1 PiperOrigin-RevId: 347936227
1 parent 3103434 commit 069e4c4

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

app/instance_id/instance_id_desktop_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ bool InstanceIdDesktopImpl::FetchServerToken(const char* scope, bool* retry) {
721721
size_t component_start = 0;
722722
size_t component_end;
723723
do {
724-
component_end = error.find(":", component_start);
724+
component_end = error.find(':', component_start);
725725
std::string error_component =
726726
error.substr(component_start, component_end - component_start);
727727
if (error_component == kPhoneRegistrationError) {

app/src/locale.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ std::string GetLocale() {
6969
// Some of the environment variables have a "." after the name to specify the
7070
// terminal encoding, e.g. "en_US.UTF-8", so we want to cut the string on the
7171
// ".".
72-
size_t cut = output.find(".");
72+
size_t cut = output.find('.');
7373
if (cut != std::string::npos) {
7474
output = output.substr(0, cut);
7575
}
7676
// Do the same with a "@" which some locales also have.
77-
cut = output.find("@");
77+
cut = output.find('@');
7878
if (cut != std::string::npos) {
7979
output = output.substr(0, cut);
8080
}

app/tests/locale_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TEST_F(LocaleTest, TestGetLocale) {
4949
// Make sure this looks like a locale, e.g. has at least 5 characters and
5050
// contains an underscore.
5151
EXPECT_GE(loc.size(), 5);
52-
EXPECT_NE(loc.find("_"), std::string::npos);
52+
EXPECT_NE(loc.find('_'), std::string::npos);
5353
}
5454

5555
} // namespace internal

auth/src/desktop/rpcs/error_codes.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ AuthError GetAuthErrorCode(const std::string& error,
225225
// Couldn't find it, check again with just the first word.
226226
// Sometimes the backend will return the error code along with
227227
// some explanatory text (see kAuthErrorNoSuchProvider above).
228-
size_t space = error.find(" ");
228+
size_t space = error.find(' ');
229229
if (space != std::string::npos) {
230230
std::string first_word = error.substr(0, space);
231231
found = find_error(first_word);

storage/src/common/storage_uri_parser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static const char kBucketPathSeparator[] = "/o/";
4545
// Strip the trailing slash from the specified string, leaving the string
4646
// unmodified if it's not found.
4747
static std::string StripTrailingSlash(const std::string& value) {
48-
auto it = value.rfind("/");
48+
auto it = value.rfind('/');
4949
return it == value.length() - 1 ? value.substr(0, it) : value;
5050
}
5151

@@ -82,7 +82,7 @@ bool UriToComponents(const std::string& url, const char* object_type,
8282
return false;
8383
}
8484
std::string full_path(url.substr(scheme.length()));
85-
std::string::size_type it = full_path.find("/");
85+
std::string::size_type it = full_path.find('/');
8686
std::string host(full_path);
8787
if (it != std::string::npos) host = full_path.substr(0, it);
8888
std::string remaining_path(full_path.substr(host.length()));

0 commit comments

Comments
 (0)