Skip to content

Commit

Permalink
stats: moving a single-use utilility inline into the class (envoyprox…
Browse files Browse the repository at this point in the history
…y#27113)

Risk Level: low
Testing: code still covered
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk authored May 3, 2023
1 parent 047f3c8 commit 3ea7ff0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 39 deletions.
11 changes: 0 additions & 11 deletions source/common/common/regex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,5 @@ ProtobufTypes::MessagePtr GoogleReEngineFactory::createEmptyConfigProto() {

REGISTER_FACTORY(GoogleReEngineFactory, EngineFactory);

std::regex Utility::parseStdRegex(const std::string& regex, std::regex::flag_type flags) {
// TODO(zuercher): In the future, PGV (https://github.com/bufbuild/protoc-gen-validate)
// annotations may allow us to remove this in favor of direct validation of regular
// expressions.
try {
return std::regex(regex, flags);
} catch (const std::regex_error& e) {
throw EnvoyException(fmt::format("Invalid regex '{}': {}", regex, e.what()));
}
}

} // namespace Regex
} // namespace Envoy
10 changes: 0 additions & 10 deletions source/common/common/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ enum class Type { Re2, StdRegex };
*/
class Utility {
public:
/**
* Constructs a std::regex, converting any std::regex_error exception into an EnvoyException.
* @param regex std::string containing the regular expression to parse.
* @param flags std::regex::flag_type containing parser flags. Defaults to std::regex::optimize.
* @return std::regex constructed from regex and flags.
* @throw EnvoyException if the regex string is invalid.
*/
static std::regex parseStdRegex(const std::string& regex,
std::regex::flag_type flags = std::regex::optimize);

/**
* Construct a compiled regex matcher from a match config.
*/
Expand Down
12 changes: 10 additions & 2 deletions source/common/stats/tag_extractor_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

namespace Envoy {
namespace Stats {
namespace {
std::regex parseStdRegex(const std::string& regex) {
TRY_ASSERT_MAIN_THREAD { return std::regex(regex, std::regex::optimize); }
END_TRY
catch (const std::regex_error& e) {
throw EnvoyException(fmt::format("Invalid regex '{}': {}", regex, e.what()));
}
}
} // namespace

const std::vector<absl::string_view>& TagExtractionContext::tokens() {
if (tokens_.empty()) {
Expand Down Expand Up @@ -87,8 +96,7 @@ bool TagExtractorImplBase::substrMismatch(absl::string_view stat_name) const {

TagExtractorStdRegexImpl::TagExtractorStdRegexImpl(absl::string_view name, absl::string_view regex,
absl::string_view substr)
: TagExtractorImplBase(name, regex, substr),
regex_(Regex::Utility::parseStdRegex(std::string(regex))) {}
: TagExtractorImplBase(name, regex, substr), regex_(parseStdRegex(std::string(regex))) {}

std::string& TagExtractorImplBase::addTagReturningValueRef(std::vector<Tag>& tags) const {
tags.emplace_back();
Expand Down
16 changes: 0 additions & 16 deletions test/common/common/regex_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ namespace Envoy {
namespace Regex {
namespace {

TEST(Utility, ParseStdRegex) {
EXPECT_THROW_WITH_REGEX(Utility::parseStdRegex("(+invalid)"), EnvoyException,
"Invalid regex '\\(\\+invalid\\)': .+");

{
std::regex regex = Utility::parseStdRegex("x*");
EXPECT_NE(0, regex.flags() & std::regex::optimize);
}

{
std::regex regex = Utility::parseStdRegex("x*", std::regex::icase);
EXPECT_NE(0, regex.flags() & std::regex::icase);
EXPECT_EQ(0, regex.flags() & std::regex::optimize);
}
}

TEST(Utility, ParseRegex) {
ScopedInjectableLoader<Regex::Engine> engine(std::make_unique<Regex::GoogleReEngine>());
{
Expand Down

0 comments on commit 3ea7ff0

Please sign in to comment.