Skip to content

Commit

Permalink
common: text: config_utils: make ParseNameValues skip empty strings
Browse files Browse the repository at this point in the history
Individual rules can be configured using the following schema:
        "<param>:<paramvalue>;<param2>:<value>"
which assumes that there wont be leading or trailing semicolons (;).
Instead of failing, let's make configuration parsing more permissive and
allow them.
  • Loading branch information
IEncinas10 committed Sep 29, 2024
1 parent d0f6c1d commit 278c546
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion common/text/config_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ absl::Status ParseNameValues(string_view config_string,
const std::initializer_list<NVConfigSpec> &spec) {
if (config_string.empty()) return absl::OkStatus();

for (const string_view single_config : absl::StrSplit(config_string, ';')) {
for (const string_view single_config :
absl::StrSplit(config_string, ';', absl::SkipEmpty())) {
const std::pair<string_view, string_view> nv_pair =
absl::StrSplit(single_config, ':');
const auto value_config = std::find_if( // linear search
Expand Down
24 changes: 24 additions & 0 deletions common/text/config_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,29 @@ TEST(ConfigUtilsTest, ParseMultipleParameters) {
EXPECT_EQ(str1, "some text string");
EXPECT_EQ(regex->pattern(), "[A-B0-9_]");
}

TEST(ConfigUtilsTest, AllowTrailingOrLeadingSemicolons) {
absl::Status s;
int answer;
bool panic;
s = ParseNameValues("answer:42;panic:off;", {{"answer", SetInt(&answer)},
{"panic", SetBool(&panic)}});
EXPECT_TRUE(s.ok());
EXPECT_FALSE(panic);
EXPECT_EQ(answer, 42);

s = ParseNameValues(";answer:43;panic:on", {{"answer", SetInt(&answer)},
{"panic", SetBool(&panic)}});
EXPECT_TRUE(s.ok()) << s.message();
EXPECT_TRUE(panic);
EXPECT_EQ(answer, 43);

s = ParseNameValues(";answer:44;panic:on;", {{"answer", SetInt(&answer)},
{"panic", SetBool(&panic)}});
EXPECT_TRUE(s.ok()) << s.message();
EXPECT_TRUE(panic);
EXPECT_EQ(answer, 44);
}

} // namespace config
} // namespace verible

0 comments on commit 278c546

Please sign in to comment.