Skip to content

Commit

Permalink
tweak to format_string_for_each_named_arg
Browse files Browse the repository at this point in the history
Summary:
* Skip past the seen names initially rather than scanning the seen names for the next `{`.
* Avoid `std::isalpha` which is locale-dependent.

Reviewed By: vitaut

Differential Revision: D67282453

fbshipit-source-id: 6675baf70d5e367741f58d2f706cf25596c06564
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Dec 17, 2024
1 parent c05c66c commit 264b8b7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions folly/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ struct format_string_for_each_named_arg_fn {
auto const pos = str.find('{');
auto const beg = pos == view::npos ? str.size() : pos + 1;
if (beg == str.size()) {
return; // malformed
return; // completed
}
if (str[beg] == '{') {
str = str.substr(beg + 1);
Expand All @@ -782,10 +782,11 @@ struct format_string_for_each_named_arg_fn {
return; // malformed
}
auto const arg = str.substr(beg, end - beg);
if (!arg.empty() && (arg[0] == '_' || std::isalpha(arg[0]))) {
auto const c = arg.empty() ? 0 : arg[0];
if (c == '_' || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
fn(arg);
}
str = str.substr(beg);
str = str.substr(end);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions folly/test/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,4 +1487,5 @@ TEST(String, format_string_for_each_named_arg) {
EXPECT_THAT(fn("hello{3}world{bob}go"), testing::ElementsAre("bob"));
EXPECT_THAT(fn("hello{bob}world{3}go"), testing::ElementsAre("bob"));
EXPECT_THAT(fn("hello{bob}world{sam}go"), testing::ElementsAre("bob", "sam"));
EXPECT_THAT(fn("{bob}world{sam}"), testing::ElementsAre("bob", "sam"));
}

0 comments on commit 264b8b7

Please sign in to comment.