Skip to content

Commit

Permalink
Get rid of using std::string;. (part 5 of N)
Browse files Browse the repository at this point in the history
Change-Id: Icf7ef53354fce3c7d1f19d35799437e4b6f4f7fb
Reviewed-on: https://code-review.googlesource.com/c/re2/+/39132
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Mar 18, 2019
1 parent ae56511 commit b049f8e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 79 deletions.
20 changes: 10 additions & 10 deletions re2/testing/exhaustive_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ static void PrintResult(const RE2& re, const StringPiece& input, RE2::Anchor anc
// Processes a single generated regexp.
// Compiles it using Regexp interface and PCRE, and then
// checks that NFA, DFA, and PCRE all return the same results.
void ExhaustiveTester::HandleRegexp(const string& const_regexp) {
void ExhaustiveTester::HandleRegexp(const std::string& const_regexp) {
regexps_++;
string regexp = const_regexp;
std::string regexp = const_regexp;
if (!topwrapper_.empty())
regexp = StringPrintf(topwrapper_.c_str(), regexp.c_str());

Expand Down Expand Up @@ -142,12 +142,12 @@ void ExhaustiveTester::HandleRegexp(const string& const_regexp) {

// Runs an exhaustive test on the given parameters.
void ExhaustiveTest(int maxatoms, int maxops,
const std::vector<string>& alphabet,
const std::vector<string>& ops,
const std::vector<std::string>& alphabet,
const std::vector<std::string>& ops,
int maxstrlen,
const std::vector<string>& stralphabet,
const string& wrapper,
const string& topwrapper) {
const std::vector<std::string>& stralphabet,
const std::string& wrapper,
const std::string& topwrapper) {
if (RE2_DEBUG_MODE) {
if (maxatoms > 1)
maxatoms--;
Expand All @@ -169,9 +169,9 @@ void ExhaustiveTest(int maxatoms, int maxops,

// Runs an exhaustive test using the given parameters and
// the basic egrep operators.
void EgrepTest(int maxatoms, int maxops, const string& alphabet,
int maxstrlen, const string& stralphabet,
const string& wrapper) {
void EgrepTest(int maxatoms, int maxops, const std::string& alphabet,
int maxstrlen, const std::string& stralphabet,
const std::string& wrapper) {
const char* tops[] = { "", "^(?:%s)", "(?:%s)$", "^(?:%s)$" };

for (int i = 0; i < arraysize(tops); i++) {
Expand Down
32 changes: 16 additions & 16 deletions re2/testing/exhaustive_tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class ExhaustiveTester : public RegexpGenerator {
public:
ExhaustiveTester(int maxatoms,
int maxops,
const std::vector<string>& alphabet,
const std::vector<string>& ops,
const std::vector<std::string>& alphabet,
const std::vector<std::string>& ops,
int maxstrlen,
const std::vector<string>& stralphabet,
const string& wrapper,
const string& topwrapper)
const std::vector<std::string>& stralphabet,
const std::string& wrapper,
const std::string& topwrapper)
: RegexpGenerator(maxatoms, maxops, alphabet, ops),
strgen_(maxstrlen, stralphabet),
wrapper_(wrapper),
Expand All @@ -60,7 +60,7 @@ class ExhaustiveTester : public RegexpGenerator {
int failures() { return failures_; }

// Needed for RegexpGenerator interface.
void HandleRegexp(const string& regexp);
void HandleRegexp(const std::string& regexp);

// Causes testing to generate random input strings.
void RandomStrings(int32_t seed, int32_t count) {
Expand All @@ -71,8 +71,8 @@ class ExhaustiveTester : public RegexpGenerator {

private:
StringGenerator strgen_;
string wrapper_; // Regexp wrapper - either empty or has one %s.
string topwrapper_; // Regexp top-level wrapper.
std::string wrapper_; // Regexp wrapper - either empty or has one %s.
std::string topwrapper_; // Regexp top-level wrapper.
int regexps_; // Number of HandleRegexp calls
int tests_; // Number of regexp tests.
int failures_; // Number of tests failed.
Expand All @@ -87,18 +87,18 @@ class ExhaustiveTester : public RegexpGenerator {

// Runs an exhaustive test on the given parameters.
void ExhaustiveTest(int maxatoms, int maxops,
const std::vector<string>& alphabet,
const std::vector<string>& ops,
const std::vector<std::string>& alphabet,
const std::vector<std::string>& ops,
int maxstrlen,
const std::vector<string>& stralphabet,
const string& wrapper,
const string& topwrapper);
const std::vector<std::string>& stralphabet,
const std::string& wrapper,
const std::string& topwrapper);

// Runs an exhaustive test using the given parameters and
// the basic egrep operators.
void EgrepTest(int maxatoms, int maxops, const string& alphabet,
int maxstrlen, const string& stralphabet,
const string& wrapper);
void EgrepTest(int maxatoms, int maxops, const std::string& alphabet,
int maxstrlen, const std::string& stralphabet,
const std::string& wrapper);

} // namespace re2

Expand Down
50 changes: 25 additions & 25 deletions re2/testing/regexp_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
namespace re2 {

// Returns a vector of the egrep regexp operators.
const std::vector<string>& RegexpGenerator::EgrepOps() {
const std::vector<std::string>& RegexpGenerator::EgrepOps() {
static const char *ops[] = {
"%s%s",
"%s|%s",
Expand All @@ -47,13 +47,13 @@ const std::vector<string>& RegexpGenerator::EgrepOps() {
"%s?",
"%s\\C*",
};
static std::vector<string> v(ops, ops + arraysize(ops));
static std::vector<std::string> v(ops, ops + arraysize(ops));
return v;
}

RegexpGenerator::RegexpGenerator(int maxatoms, int maxops,
const std::vector<string>& atoms,
const std::vector<string>& ops)
const std::vector<std::string>& atoms,
const std::vector<std::string>& ops)
: maxatoms_(maxatoms), maxops_(maxops), atoms_(atoms), ops_(ops) {
// Degenerate case.
if (atoms_.empty())
Expand All @@ -65,7 +65,7 @@ RegexpGenerator::RegexpGenerator(int maxatoms, int maxops,
// Generates all possible regular expressions (within the parameters),
// calling HandleRegexp for each one.
void RegexpGenerator::Generate() {
std::vector<string> postfix;
std::vector<std::string> postfix;
GeneratePostfix(&postfix, 0, 0, 0);
}

Expand All @@ -74,13 +74,13 @@ void RegexpGenerator::GenerateRandom(int32_t seed, int n) {
rng_.seed(seed);

for (int i = 0; i < n; i++) {
std::vector<string> postfix;
std::vector<std::string> postfix;
GenerateRandomPostfix(&postfix, 0, 0, 0);
}
}

// Counts and returns the number of occurrences of "%s" in s.
static int CountArgs(const string& s) {
static int CountArgs(const std::string& s) {
const char *p = s.c_str();
int n = 0;
while ((p = strstr(p, "%s")) != NULL) {
Expand All @@ -103,8 +103,8 @@ static int CountArgs(const string& s) {
//
// The initial call should be GeneratePostfix([empty vector], 0, 0, 0).
//
void RegexpGenerator::GeneratePostfix(std::vector<string>* post, int nstk,
int ops, int atoms) {
void RegexpGenerator::GeneratePostfix(std::vector<std::string>* post,
int nstk, int ops, int atoms) {
if (nstk == 1)
RunPostfix(*post);

Expand All @@ -126,7 +126,7 @@ void RegexpGenerator::GeneratePostfix(std::vector<string>* post, int nstk,
// Add operators if there are enough arguments.
if (ops < maxops_) {
for (size_t i = 0; i < ops_.size(); i++) {
const string& fmt = ops_[i];
const std::string& fmt = ops_[i];
int nargs = CountArgs(fmt);
if (nargs <= nstk) {
post->push_back(fmt);
Expand All @@ -139,8 +139,8 @@ void RegexpGenerator::GeneratePostfix(std::vector<string>* post, int nstk,

// Generates a random postfix command sequence.
// Stops and returns true once a single sequence has been generated.
bool RegexpGenerator::GenerateRandomPostfix(std::vector<string>* post, int nstk,
int ops, int atoms) {
bool RegexpGenerator::GenerateRandomPostfix(std::vector<std::string>* post,
int nstk, int ops, int atoms) {
std::uniform_int_distribution<int> random_stop(0, maxatoms_ - atoms);
std::uniform_int_distribution<int> random_bit(0, 1);
std::uniform_int_distribution<int> random_ops_index(
Expand All @@ -163,7 +163,7 @@ bool RegexpGenerator::GenerateRandomPostfix(std::vector<string>* post, int nstk,

// Add operators if there are enough arguments.
if (ops < maxops_ && random_bit(rng_) == 0) {
const string& fmt = ops_[random_ops_index(rng_)];
const std::string& fmt = ops_[random_ops_index(rng_)];
int nargs = CountArgs(fmt);
if (nargs <= nstk) {
post->push_back(fmt);
Expand All @@ -189,8 +189,8 @@ bool RegexpGenerator::GenerateRandomPostfix(std::vector<string>* post, int nstk,
// Interprets the postfix command sequence to create a regular expression
// passed to HandleRegexp. The results of operators like %s|%s are wrapped
// in (?: ) to avoid needing to maintain a precedence table.
void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
std::stack<string> regexps;
void RegexpGenerator::RunPostfix(const std::vector<std::string>& post) {
std::stack<std::string> regexps;
for (size_t i = 0; i < post.size(); i++) {
switch (CountArgs(post[i])) {
default:
Expand All @@ -199,15 +199,15 @@ void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
regexps.push(post[i]);
break;
case 1: {
string a = regexps.top();
std::string a = regexps.top();
regexps.pop();
regexps.push("(?:" + StringPrintf(post[i].c_str(), a.c_str()) + ")");
break;
}
case 2: {
string b = regexps.top();
std::string b = regexps.top();
regexps.pop();
string a = regexps.top();
std::string a = regexps.top();
regexps.pop();
regexps.push("(?:" +
StringPrintf(post[i].c_str(), a.c_str(), b.c_str()) +
Expand Down Expand Up @@ -238,38 +238,38 @@ void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
}

// Split s into an vector of strings, one for each UTF-8 character.
std::vector<string> Explode(const StringPiece& s) {
std::vector<string> v;
std::vector<std::string> Explode(const StringPiece& s) {
std::vector<std::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));
v.push_back(std::string(p, q - p));
}

return v;
}

// Split string everywhere a substring is found, returning
// vector of pieces.
std::vector<string> Split(const StringPiece& sep, const StringPiece& s) {
std::vector<string> v;
std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s) {
std::vector<std::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));
v.push_back(std::string(p, q - p));
p = q + sep.size();
q = p - 1; // -1 for ++ in loop
continue;
}
}
if (p < s.end())
v.push_back(string(p, s.end() - p));
v.push_back(std::string(p, s.end() - p));
return v;
}

Expand Down
34 changes: 18 additions & 16 deletions re2/testing/regexp_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ namespace re2 {
//
class RegexpGenerator {
public:
RegexpGenerator(int maxatoms, int maxops, const std::vector<string>& atoms,
const std::vector<string>& ops);
RegexpGenerator(int maxatoms, int maxops,
const std::vector<std::string>& atoms,
const std::vector<std::string>& ops);
virtual ~RegexpGenerator() {}

// Generates all the regular expressions, calling HandleRegexp(re) for each.
Expand All @@ -40,22 +41,23 @@ class RegexpGenerator {
void GenerateRandom(int32_t seed, int n);

// Handles a regular expression. Must be provided by subclass.
virtual void HandleRegexp(const string& regexp) = 0;
virtual void HandleRegexp(const std::string& regexp) = 0;

// The egrep regexp operators: * + ? | and concatenation.
static const std::vector<string>& EgrepOps();
static const std::vector<std::string>& EgrepOps();

private:
void RunPostfix(const std::vector<string>& post);
void GeneratePostfix(std::vector<string>* post, int nstk, int ops, int lits);
bool GenerateRandomPostfix(std::vector<string>* post, int nstk, int ops,
int lits);

int maxatoms_; // Maximum number of atoms allowed in expr.
int maxops_; // Maximum number of ops allowed in expr.
std::vector<string> atoms_; // Possible atoms.
std::vector<string> ops_; // Possible ops.
std::minstd_rand0 rng_; // Random number generator.
void RunPostfix(const std::vector<std::string>& post);
void GeneratePostfix(std::vector<std::string>* post,
int nstk, int ops, int lits);
bool GenerateRandomPostfix(std::vector<std::string>* post,
int nstk, int ops, int lits);

int maxatoms_; // Maximum number of atoms allowed in expr.
int maxops_; // Maximum number of ops allowed in expr.
std::vector<std::string> atoms_; // Possible atoms.
std::vector<std::string> ops_; // Possible ops.
std::minstd_rand0 rng_; // Random number generator.

RegexpGenerator(const RegexpGenerator&) = delete;
RegexpGenerator& operator=(const RegexpGenerator&) = delete;
Expand All @@ -64,11 +66,11 @@ class RegexpGenerator {
// Helpers for preparing arguments to RegexpGenerator constructor.

// Returns one string for each character in s.
std::vector<string> Explode(const StringPiece& s);
std::vector<std::string> Explode(const StringPiece& s);

// Splits string everywhere sep is found, returning
// vector of pieces.
std::vector<string> Split(const StringPiece& sep, const StringPiece& s);
std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s);

} // namespace re2

Expand Down
2 changes: 1 addition & 1 deletion re2/testing/string_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace re2 {

StringGenerator::StringGenerator(int maxlen,
const std::vector<string>& alphabet)
const std::vector<std::string>& alphabet)
: maxlen_(maxlen), alphabet_(alphabet),
generate_null_(false),
random_(false), nrandom_(0) {
Expand Down
8 changes: 4 additions & 4 deletions re2/testing/string_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace re2 {

class StringGenerator {
public:
StringGenerator(int maxlen, const std::vector<string>& alphabet);
StringGenerator(int maxlen, const std::vector<std::string>& alphabet);
~StringGenerator() {}

const StringPiece& Next();
Expand All @@ -41,12 +41,12 @@ class StringGenerator {
bool RandomDigits();

// Global state.
int maxlen_; // Maximum length string to generate.
std::vector<string> alphabet_; // Alphabet, one string per letter.
int maxlen_; // Maximum length string to generate.
std::vector<std::string> alphabet_; // Alphabet, one string per letter.

// Iteration state.
StringPiece sp_; // Last StringPiece returned by Next().
string s_; // String data in last StringPiece returned by Next().
std::string s_; // String data in last StringPiece returned by Next().
bool hasnext_; // Whether Next() can be called again.
std::vector<int> digits_; // Alphabet indices for next string.
bool generate_null_; // Whether to generate a NULL StringPiece next.
Expand Down
Loading

0 comments on commit b049f8e

Please sign in to comment.