diff options
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/regex/regex.cpp | 71 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/regexp.cpp | 65 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/regexp.h | 62 |
3 files changed, 13 insertions, 185 deletions
diff --git a/vespalib/src/tests/regex/regex.cpp b/vespalib/src/tests/regex/regex.cpp index 95d6a7e88e7..d1b94daa7ba 100644 --- a/vespalib/src/tests/regex/regex.cpp +++ b/vespalib/src/tests/regex/regex.cpp @@ -3,49 +3,10 @@ #include <vespa/vespalib/util/regexp.h> #include <vespa/vespalib/util/exception.h> +#include <regex> using namespace vespalib; -TEST("require that empty expression works as expected") { - Regexp empty(""); - EXPECT_TRUE(empty.valid()); - EXPECT_TRUE(empty.match("")); - EXPECT_TRUE(empty.match("foo")); - EXPECT_TRUE(empty.match("bar")); -} - -TEST("require that substring expression works as expected") { - Regexp re("foo"); - EXPECT_TRUE(re.match("foo")); - EXPECT_TRUE(re.match("afoob")); - EXPECT_TRUE(re.match("foo foo")); - EXPECT_FALSE(re.match("bar")); - EXPECT_FALSE(re.match("fobaroo")); -} - -TEST("require that it is default case sentive") { - Regexp re("foo"); - EXPECT_TRUE(re.match("foo")); - EXPECT_FALSE(re.match("fOo")); -} - -TEST("require that it is case insentive") { - Regexp re("foo", Regexp::Flags().enableICASE()); - EXPECT_TRUE(re.match("foo")); - EXPECT_TRUE(re.match("fOo")); -} - -TEST("require that invalid expression fails compilation") { - Regexp bad("[unbalanced"); - EXPECT_FALSE(bad.valid()); - EXPECT_FALSE(bad.match("nothing")); -} - -TEST("require that * is not valid") { - Regexp bad("*"); - EXPECT_FALSE(bad.valid()); -} - TEST("require that prefix detection works") { EXPECT_EQUAL("", Regexp::get_prefix("")); EXPECT_EQUAL("", Regexp::get_prefix("foo")); @@ -86,33 +47,23 @@ struct ExprFixture { } }; -TEST_F("require that regexp can be made from prefix string", ExprFixture()) { - for (vespalib::string str: f1.expressions) { - Regexp re(Regexp::make_from_prefix(str)); - EXPECT_TRUE(re.match(str)); - EXPECT_TRUE(re.match(str + "foo")); - EXPECT_FALSE(re.match("foo" + str)); - EXPECT_FALSE(re.match("foo" + str + "bar")); - } -} - TEST_F("require that regexp can be made from suffix string", ExprFixture()) { for (vespalib::string str: f1.expressions) { - Regexp re(Regexp::make_from_suffix(str)); - EXPECT_TRUE(re.match(str)); - EXPECT_FALSE(re.match(str + "foo")); - EXPECT_TRUE(re.match("foo" + str)); - EXPECT_FALSE(re.match("foo" + str + "bar")); + std::regex re(std::string(Regexp::make_from_suffix(str))); + EXPECT_TRUE(std::regex_search(std::string(str), re)); + EXPECT_FALSE(std::regex_search(std::string(str + "foo"), re)); + EXPECT_TRUE(std::regex_search(std::string("foo" + str), re)); + EXPECT_FALSE(std::regex_search(std::string("foo" + str + "bar"), re)); } } TEST_F("require that regexp can be made from substring string", ExprFixture()) { for (vespalib::string str: f1.expressions) { - Regexp re(Regexp::make_from_substring(str)); - EXPECT_TRUE(re.match(str)); - EXPECT_TRUE(re.match(str + "foo")); - EXPECT_TRUE(re.match("foo" + str)); - EXPECT_TRUE(re.match("foo" + str + "bar")); + std::regex re(std::string(Regexp::make_from_substring(str))); + EXPECT_TRUE(std::regex_search(std::string(str), re)); + EXPECT_TRUE(std::regex_search(std::string(str + "foo"), re)); + EXPECT_TRUE(std::regex_search(std::string("foo" + str), re)); + EXPECT_TRUE(std::regex_search(std::string("foo" + str + "bar"), re)); } } diff --git a/vespalib/src/vespa/vespalib/util/regexp.cpp b/vespalib/src/vespa/vespalib/util/regexp.cpp index 71949b82ef3..b3cad06382e 100644 --- a/vespalib/src/vespa/vespalib/util/regexp.cpp +++ b/vespalib/src/vespa/vespalib/util/regexp.cpp @@ -12,65 +12,6 @@ LOG_SETUP(".vespalib.util.regexp"); namespace vespalib { -Regexp::Flags::Flags() : - _flags(RE_SYNTAX_POSIX_EXTENDED) -{ } - -Regexp::Flags & -Regexp::Flags::enableICASE() -{ - _flags |= RE_ICASE; - return *this; -} - -bool -Regexp::compile(vespalib::stringref re, Flags flags) -{ - re_set_syntax(flags.flags()); - regex_t *preg = (regex_t *)_data; - preg->translate = NULL; - preg->fastmap = static_cast<char *>(malloc(256)); - preg->buffer = NULL; - preg->allocated = 0; - const char * error = re_compile_pattern(re.data(), re.size(), preg); - if (error != 0) { - LOG(warning, "invalid regexp '%s': %s", vespalib::string(re).c_str(), error); - return false; - } - if (re_compile_fastmap(preg) != 0) { - LOG(warning, "re_compile_fastmap failed for regexp '%s'", vespalib::string(re).c_str()); - return false; - } - return true; -} - - -Regexp::Regexp(vespalib::stringref re, Flags flags) - : _valid(false), - _data(new regex_t) -{ - _valid = compile(re, flags); -} - -bool -Regexp::match(vespalib::stringref s) const -{ - if ( ! valid() ) { return false; } - regex_t *preg = const_cast<regex_t *>(static_cast<const regex_t *>(_data)); - int pos(re_search(preg, s.data(), s.size(), 0, s.size(), NULL)); - if (pos < -1) { - throw IllegalArgumentException(make_string("re_search failed with code(%d)", pos)); - } - return pos >= 0; -} - -Regexp::~Regexp() -{ - regex_t *preg = static_cast<regex_t *>(_data); - regfree(preg); - delete preg; -} - namespace { bool has_option(vespalib::stringref re) { @@ -117,12 +58,6 @@ Regexp::get_prefix(vespalib::stringref re) } vespalib::string -Regexp::make_from_prefix(vespalib::stringref prefix) -{ - return "^" + escape(prefix); -} - -vespalib::string Regexp::make_from_suffix(vespalib::stringref suffix) { return escape(suffix) + "$"; diff --git a/vespalib/src/vespa/vespalib/util/regexp.h b/vespalib/src/vespa/vespalib/util/regexp.h index fe1930d2b4a..9897b488aff 100644 --- a/vespalib/src/vespa/vespalib/util/regexp.h +++ b/vespalib/src/vespa/vespalib/util/regexp.h @@ -2,60 +2,15 @@ #pragma once #include <vespa/vespalib/stllike/string.h> -#include <vespa/vespalib/util/noncopyable.hpp> namespace vespalib { /** - * Utility class for simple regular expression matching. - * This class wraps the C library implementation of - * the posix regex API, for simple and easy usage. - * Note: also minimizes namespace pollution, you don't - * need to include <regex.h> when using class. + * Utility class inspecting and generating regular expression strings. **/ -class Regexp : public noncopyable +class Regexp { public: - class Flags { - public: - /** - * By default enable posix extended regex. - **/ - Flags(); - /** - * Enable case insentive search. - **/ - Flags & enableICASE(); - /** - * Return the decoded set of flags for the implementation. - **/ - unsigned long flags() const { return _flags; } - private: - unsigned long _flags; - }; - /** - * Construct from a Posix Extended regular expression. - * @throw IllegalArgumentException if the RE is invalid. - * @param re Regular expression. - **/ - Regexp(vespalib::stringref re, Flags=Flags()); - - ~Regexp(); - - /** - * Will tell if the regexp was valid. - * @return true if regexp is valid. - **/ - bool valid() const { return _valid; } - - /** - * Check if the given string is matched by this regexp. - * If called on invalid regexp it will return false. - * @param s text to search for a match. - * @return true if a match was found. - **/ - bool match(vespalib::stringref s) const; - /** * Look at the given regular expression and identify the prefix * that must be present for a string to match it. Note that an @@ -69,14 +24,6 @@ public: static vespalib::string get_prefix(vespalib::stringref re); /** - * Make a regexp matching strings with the given prefix. - * - * @param prefix the prefix - * @return the regexp - **/ - static vespalib::string make_from_prefix(vespalib::stringref prefix); - - /** * Make a regexp matching strings with the given suffix. * * @param suffix the suffix @@ -91,11 +38,6 @@ public: * @return the regexp **/ static vespalib::string make_from_substring(vespalib::stringref substring); - -private: - bool _valid; - void *_data; - bool compile(vespalib::stringref re, Flags flags); }; } // namespace vespalib |