Adds support to regexp::searchAll

This commit is contained in:
Felipe Zimmerle 2016-11-22 15:37:12 -03:00
parent d3a4ec760c
commit 9c7988d88f
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
2 changed files with 38 additions and 9 deletions

View File

@ -21,6 +21,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <list>
#include <fstream>
#include <iostream>
@ -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<SMatch> Regex::searchAll(const std::string& s) {
int ovector[OVECCOUNT];
std::list<SMatch> 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];

View File

@ -18,6 +18,7 @@
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#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<SMatch> searchAll(const std::string& s);
};