This source file includes following definitions.
- EgrepOps
- ops_
- Generate
- GenerateRandom
- CountArgs
- GeneratePostfix
- GenerateRandomPostfix
- RunPostfix
- Explode
- Split
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include "util/test.h"
#include "re2/testing/regexp_generator.h"
namespace re2 {
const vector<string>& RegexpGenerator::EgrepOps() {
static const char *ops[] = {
"%s%s",
"%s|%s",
"%s*",
"%s+",
"%s?",
"%s\\C*",
};
static vector<string> v(ops, ops + arraysize(ops));
return v;
}
RegexpGenerator::RegexpGenerator(int maxatoms, int maxops,
const vector<string>& atoms,
const vector<string>& ops)
: maxatoms_(maxatoms), maxops_(maxops), atoms_(atoms), ops_(ops) {
if (atoms_.size() == 0)
maxatoms_ = 0;
if (ops_.size() == 0)
maxops_ = 0;
}
void RegexpGenerator::Generate() {
vector<string> postfix;
GeneratePostfix(&postfix, 0, 0, 0);
}
void RegexpGenerator::GenerateRandom(int32 seed, int n) {
ACMRandom acm(seed);
acm_ = &acm;
for (int i = 0; i < n; i++) {
vector<string> postfix;
GenerateRandomPostfix(&postfix, 0, 0, 0);
}
acm_ = NULL;
}
static int CountArgs(const string& s) {
const char *p = s.c_str();
int n = 0;
while ((p = strstr(p, "%s")) != NULL) {
p += 2;
n++;
}
return n;
}
void RegexpGenerator::GeneratePostfix(vector<string>* post, int nstk,
int ops, int atoms) {
if (nstk == 1)
RunPostfix(*post);
if (ops + nstk - 1 > maxops_)
return;
if (atoms < maxatoms_) {
for (int i = 0; i < atoms_.size(); i++) {
post->push_back(atoms_[i]);
GeneratePostfix(post, nstk + 1, ops, atoms + 1);
post->pop_back();
}
}
if (ops < maxops_) {
for (int i = 0; i < ops_.size(); i++) {
const string& fmt = ops_[i];
int nargs = CountArgs(fmt);
if (nargs <= nstk) {
post->push_back(fmt);
GeneratePostfix(post, nstk - nargs + 1, ops + 1, atoms);
post->pop_back();
}
}
}
}
bool RegexpGenerator::GenerateRandomPostfix(vector<string> *post, int nstk,
int ops, int atoms) {
for (;;) {
if (nstk == 1 && acm_->Uniform(maxatoms_ + 1 - atoms) == 0) {
RunPostfix(*post);
return true;
}
if (ops + nstk - 1 > maxops_)
return false;
if (ops < maxops_ && acm_->Uniform(2) == 0) {
const string& fmt = ops_[acm_->Uniform(ops_.size())];
int nargs = CountArgs(fmt);
if (nargs <= nstk) {
post->push_back(fmt);
bool ret = GenerateRandomPostfix(post, nstk - nargs + 1,
ops + 1, atoms);
post->pop_back();
if (ret)
return true;
}
}
if (atoms < maxatoms_ && acm_->Uniform(2) == 0) {
post->push_back(atoms_[acm_->Uniform(atoms_.size())]);
bool ret = GenerateRandomPostfix(post, nstk + 1, ops, atoms + 1);
post->pop_back();
if (ret)
return true;
}
}
}
void RegexpGenerator::RunPostfix(const vector<string>& post) {
stack<string> regexps;
for (int i = 0; i < post.size(); i++) {
switch (CountArgs(post[i])) {
default:
LOG(FATAL) << "Bad operator: " << post[i];
case 0:
regexps.push(post[i]);
break;
case 1: {
string a = regexps.top();
regexps.pop();
regexps.push("(?:" + StringPrintf(post[i].c_str(), a.c_str()) + ")");
break;
}
case 2: {
string b = regexps.top();
regexps.pop();
string a = regexps.top();
regexps.pop();
regexps.push("(?:" +
StringPrintf(post[i].c_str(), a.c_str(), b.c_str()) +
")");
break;
}
}
}
if (regexps.size() != 1) {
printf("Bad regexp program:\n");
for (int i = 0; i < post.size(); i++) {
printf(" %s\n", CEscape(post[i]).c_str());
}
printf("Stack after running program:\n");
while (!regexps.empty()) {
printf(" %s\n", CEscape(regexps.top()).c_str());
regexps.pop();
}
LOG(FATAL) << "Bad regexp program.";
}
HandleRegexp(regexps.top());
HandleRegexp("^(?:" + regexps.top() + ")$");
HandleRegexp("^(?:" + regexps.top() + ")");
HandleRegexp("(?:" + regexps.top() + ")$");
}
vector<string> Explode(const StringPiece& s) {
vector<string> v;
for (const char *q = s.begin(); q < s.end(); ) {
const char* p = q;
Rune r;
q += chartorune(&r, q);
v.push_back(string(p, q - p));
}
return v;
}
vector<string> Split(const StringPiece& sep, const StringPiece& s) {
vector<string> v;
if (sep.size() == 0)
return Explode(s);
const char *p = s.begin();
for (const char *q = s.begin(); q + sep.size() <= s.end(); q++) {
if (StringPiece(q, sep.size()) == sep) {
v.push_back(string(p, q - p));
p = q + sep.size();
q = p - 1;
continue;
}
}
if (p < s.end())
v.push_back(string(p, s.end() - p));
return v;
}
}