diff --git a/src/utils/regex.cc b/src/utils/regex.cc index ff7c70dc..704971d1 100644 --- a/src/utils/regex.cc +++ b/src/utils/regex.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,7 @@ Regex::Regex(const std::string& pattern_) m_pc = pcre_compile(pattern.c_str(), PCRE_DOTALL|PCRE_MULTILINE, &errptr, &erroffset, NULL); + m_pce = pcre_study(m_pc, pcre_study_opt, &errptr); } @@ -68,6 +70,30 @@ Regex::~Regex() { } +std::list Regex::searchAll(const std::string& s) { + int ovector[OVECCOUNT]; + std::list retList; + int i; + const char *subject = s.c_str(); + int rc; + + rc = pcre_exec(m_pc, m_pce, subject, + s.size(), 0, 0, ovector, OVECCOUNT); + + for (i = 0; i < rc; i++) { + SMatch match; + const char *substring_start = subject + ovector[2*i]; + int substring_length = ovector[2*i+1] - ovector[2*i]; + + match.match = std::string(subject, ovector[2*i], + ovector[2*i+1] - ovector[2*i]); + + retList.push_front(match); + } + + return retList; +} + int regex_search(const std::string& s, SMatch *match, const Regex& regex) { int ovector[OVECCOUNT]; diff --git a/src/utils/regex.h b/src/utils/regex.h index 45405b1e..956b75d7 100644 --- a/src/utils/regex.h +++ b/src/utils/regex.h @@ -18,6 +18,7 @@ #include #include #include +#include #ifndef SRC_UTILS_REGEX_H_ #define SRC_UTILS_REGEX_H_ @@ -28,6 +29,16 @@ namespace Utils { #define OVECCOUNT 30 +class SMatch { + public: + SMatch() : size_(0) { } + size_t size() const { return size_; } + std::string str() const { return match; } + int size_; + std::string match; +}; + + class Regex { public: explicit Regex(const std::string& pattern_); @@ -36,16 +47,8 @@ class Regex { pcre *m_pc = NULL; pcre_extra *m_pce = NULL; int m_ovector[OVECCOUNT]; -}; - -class SMatch { - public: - SMatch() : size_(0) { } - size_t size() const { return size_; } - std::string str() const { return match; } - int size_; - std::string match; + std::list searchAll(const std::string& s); };