Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex config util #2199

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/text/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_googlesource_code_re2//:re2",
],
)

Expand All @@ -143,6 +144,7 @@ cc_test(
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_googlesource_code_re2//:re2",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down
16 changes: 16 additions & 0 deletions common/text/config_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <initializer_list>
#include <iterator>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -32,6 +33,7 @@
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "common/util/logging.h"
#include "re2/re2.h"

namespace verible {
using absl::string_view;
Expand Down Expand Up @@ -184,5 +186,19 @@ ConfigValueSetter SetNamedBits(
};
}

ConfigValueSetter SetRegex(std::unique_ptr<re2::RE2> *regex) {
CHECK(regex) << "Must provide pointer to a RE2 to store.";
return [regex](string_view v) {
*regex = std::make_unique<re2::RE2>(v, re2::RE2::Quiet);
if((*regex)->ok()) {
return absl::OkStatus();
}

std::string error_msg =
absl::StrCat("Failed to parse regular expression: ", (*regex)->error());
return absl::InvalidArgumentError(error_msg);
};
}

} // namespace config
} // namespace verible
6 changes: 6 additions & 0 deletions common/text/config_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "re2/re2.h"

namespace verible {
namespace config {
Expand Down Expand Up @@ -94,6 +96,10 @@ ConfigValueSetter SetStringOneOf(std::string *value,
// up to 32 choices.
ConfigValueSetter SetNamedBits(uint32_t *value,
const std::vector<absl::string_view> &choices);

// Set a Regex
ConfigValueSetter SetRegex(std::unique_ptr<re2::RE2> *regex);

} // namespace config
} // namespace verible

Expand Down
30 changes: 30 additions & 0 deletions common/text/config_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "common/text/config_utils.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -23,6 +24,7 @@
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "re2/re2.h"

namespace verible {
namespace config {
Expand Down Expand Up @@ -93,6 +95,19 @@ TEST(ConfigUtilsTest, ParseBool) {
absl::StartsWith(s.message(), "baz: Boolean value should be one of"));
}

TEST(ConfigUtilsTest, ParseRegex) {
absl::Status s;
std::unique_ptr<re2::RE2> regex;
s = ParseNameValues("regex:[a-b0-9_]", {{"regex", SetRegex(&regex)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(regex->pattern(), "[a-b0-9_]");

s = ParseNameValues("regex:[a-b0-9_", {{"regex", SetRegex(&regex)}});
EXPECT_FALSE(s.ok());
EXPECT_EQ(s.message(),
"regex: Failed to parse regular expression: missing ]: [a-b0-9_");
}

TEST(ConfigUtilsTest, ParseString) {
absl::Status s;
std::string str;
Expand Down Expand Up @@ -170,6 +185,21 @@ TEST(ConfigUtilsTest, ParseMultipleParameters) {
EXPECT_TRUE(s.ok()) << s.message();
EXPECT_TRUE(panic);
EXPECT_EQ(answer, 43);

std::string str1;
std::string str2;
s = ParseNameValues("baz:hello world;fry:multiple spaces in this one",
{{"baz", SetString(&str1)}, {"fry", SetString(&str2)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(str1, "hello world");
EXPECT_EQ(str2, "multiple spaces in this one");

std::unique_ptr<re2::RE2> regex;
s = ParseNameValues("baz:some text string;regex:[A-B0-9_]",
{{"baz", SetString(&str1)}, {"regex", SetRegex(&regex)}});
EXPECT_TRUE(s.ok());
EXPECT_EQ(str1, "some text string");
EXPECT_EQ(regex->pattern(), "[A-B0-9_]");
}
} // namespace config
} // namespace verible
Loading