diff options
author | Paul Wankadia <junyer@google.com> | 2016-05-19 21:06:24 +1000 |
---|---|---|
committer | Paul Wankadia <junyer@google.com> | 2016-05-20 03:26:39 +0000 |
commit | f91e7e1bee9f68e4ef9ce49878647f85ed94a133 (patch) | |
tree | 3c4ef2da2ea20e3d5c45bb850f4254dcdeac270d /re2/re2.h | |
parent | 9efd0dfacc81cc279e135a0e092ff5e9c1d20637 (diff) | |
download | regex-re2-f91e7e1bee9f68e4ef9ce49878647f85ed94a133.tar.gz |
Replace variadic_function.h with variadic templates.
Change-Id: I8e527268405169ea9234b55d9f64f9349164104c
Reviewed-on: https://code-review.googlesource.com/4792
Reviewed-by: Paul Wankadia <junyer@google.com>
Diffstat (limited to 're2/re2.h')
-rw-r--r-- | re2/re2.h | 73 |
1 files changed, 49 insertions, 24 deletions
@@ -184,7 +184,6 @@ #include <mutex> #include <string> #include "re2/stringpiece.h" -#include "re2/variadic_function.h" #ifndef RE2_HAVE_LONGLONG #define RE2_HAVE_LONGLONG 1 @@ -290,11 +289,11 @@ class RE2 { /***** The useful part: the matching interface *****/ - // Matches "text" against "pattern". If pointer arguments are + // Matches "text" against "re". If pointer arguments are // supplied, copies matched sub-patterns into them. // // You can pass in a "const char*" or a "string" for "text". - // You can pass in a "const char*" or a "string" or a "RE2" for "pattern". + // You can pass in a "const char*" or a "string" or a "RE2" for "re". // // The provided pointer arguments can be pointers to any scalar numeric // type, or one of: @@ -304,7 +303,7 @@ class RE2 { // (void*)NULL (the corresponding matched sub-pattern is not copied) // // Returns true iff all of the following conditions are satisfied: - // a. "text" matches "pattern" exactly + // a. "text" matches "re" exactly // b. The number of matched sub-patterns is >= number of supplied pointers // c. The "i"th argument has a suitable type for holding the // string captured as the "i"th sub-pattern. If you pass in @@ -320,32 +319,58 @@ class RE2 { // RE2::FullMatch("abc", "[a-z]+(\\d+)?", &number); static bool FullMatchN(const StringPiece& text, const RE2& re, const Arg* const args[], int argc); - static const VariadicFunction2< - bool, const StringPiece&, const RE2&, Arg, RE2::FullMatchN> FullMatch; - // Exactly like FullMatch(), except that "pattern" is allowed to match + // Exactly like FullMatch(), except that "re" is allowed to match // a substring of "text". - static bool PartialMatchN(const StringPiece& text, const RE2& re, // 3..16 args + static bool PartialMatchN(const StringPiece& text, const RE2& re, const Arg* const args[], int argc); - static const VariadicFunction2< - bool, const StringPiece&, const RE2&, Arg, RE2::PartialMatchN> PartialMatch; - // Like FullMatch() and PartialMatch(), except that pattern has to - // match a prefix of "text", and "input" is advanced past the matched + // Like FullMatch() and PartialMatch(), except that "re" has to match + // a prefix of the text, and "input" is advanced past the matched // text. Note: "input" is modified iff this routine returns true. - static bool ConsumeN(StringPiece* input, const RE2& pattern, // 3..16 args + static bool ConsumeN(StringPiece* input, const RE2& re, const Arg* const args[], int argc); - static const VariadicFunction2< - bool, StringPiece*, const RE2&, Arg, RE2::ConsumeN> Consume; - - // Like Consume(..), but does not anchor the match at the beginning of the - // string. That is, "pattern" need not start its match at the beginning of - // "input". For example, "FindAndConsume(s, "(\\w+)", &word)" finds the next - // word in "s" and stores it in "word". - static bool FindAndConsumeN(StringPiece* input, const RE2& pattern, - const Arg* const args[], int argc); - static const VariadicFunction2< - bool, StringPiece*, const RE2&, Arg, RE2::FindAndConsumeN> FindAndConsume; + + // Like Consume(), but does not anchor the match at the beginning of + // the text. That is, "re" need not start its match at the beginning + // of "input". For example, "FindAndConsume(s, "(\\w+)", &word)" finds + // the next word in "s" and stores it in "word". + static bool FindAndConsumeN(StringPiece* input, const RE2& re, + const Arg* const args[], int argc); + + private: + template <typename F, typename SP, typename... A> + static inline bool Apply(F f, SP sp, const RE2& re, const A&... a) { + const Arg* const args[] = {&a...}; + constexpr int argc = sizeof...(a); + return f(sp, re, args, argc); + } + + public: + // In order to allow FullMatch() et al. to be called with a varying number + // of arguments of varying types, we use two layers of variadic templates. + // The first layer constructs the temporary Arg objects. The second layer + // (above) constructs the array of pointers to the temporary Arg objects. + + template <typename... A> + static bool FullMatch(const StringPiece& text, const RE2& re, A... a) { + return Apply(FullMatchN, text, re, Arg(a)...); + } + + template <typename... A> + static bool PartialMatch(const StringPiece& text, const RE2& re, A... a) { + return Apply(PartialMatchN, text, re, Arg(a)...); + } + + template <typename... A> + static bool Consume(StringPiece* input, const RE2& re, A... a) { + return Apply(ConsumeN, input, re, Arg(a)...); + } + + template <typename... A> + static bool FindAndConsume(StringPiece* input, const RE2& re, A... a) { + return Apply(FindAndConsumeN, input, re, Arg(a)...); + } // Replace the first match of "pattern" in "str" with "rewrite". // Within "rewrite", backslash-escaped digits (\1 to \9) can be |