diff options
author | Paul Wankadia <junyer@google.com> | 2016-05-27 14:45:06 +1000 |
---|---|---|
committer | Paul Wankadia <junyer@google.com> | 2016-05-27 04:56:12 +0000 |
commit | c5f0094d79032d53b4aace38580ac3d6d6f460fd (patch) | |
tree | 9c8614193b54196c513fecc9239afeb963f34765 /re2/re2.h | |
parent | bfa5864478949b19e345fc8da15e681ee0326dac (diff) | |
download | regex-re2-c5f0094d79032d53b4aace38580ac3d6d6f460fd.tar.gz |
Replace variadic_function.h with variadic templates.
This time, using forwarding references and not breaking SWIG.
Change-Id: I4069ae960f2d8b1efb79e7cc6f7ba2a7a56d52c0
Reviewed-on: https://code-review.googlesource.com/4882
Reviewed-by: Paul Wankadia <junyer@google.com>
Diffstat (limited to 're2/re2.h')
-rw-r--r-- | re2/re2.h | 75 |
1 files changed, 51 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,60 @@ 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); + +#ifndef SWIG + 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(std::forward<A>(a))...); + } + + template <typename... A> + static bool PartialMatch(const StringPiece& text, const RE2& re, A&&... a) { + return Apply(PartialMatchN, text, re, Arg(std::forward<A>(a))...); + } + + template <typename... A> + static bool Consume(StringPiece* input, const RE2& re, A&&... a) { + return Apply(ConsumeN, input, re, Arg(std::forward<A>(a))...); + } + + template <typename... A> + static bool FindAndConsume(StringPiece* input, const RE2& re, A&&... a) { + return Apply(FindAndConsumeN, input, re, Arg(std::forward<A>(a))...); + } +#endif // Replace the first match of "pattern" in "str" with "rewrite". // Within "rewrite", backslash-escaped digits (\1 to \9) can be |