Skip to content

Commit b049f8e

Browse files
committed
Get rid of using std::string;. (part 5 of N)
Change-Id: Icf7ef53354fce3c7d1f19d35799437e4b6f4f7fb Reviewed-on: https://code-review.googlesource.com/c/re2/+/39132 Reviewed-by: Paul Wankadia <[email protected]>
1 parent ae56511 commit b049f8e

File tree

7 files changed

+82
-79
lines changed

7 files changed

+82
-79
lines changed

re2/testing/exhaustive_tester.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ static void PrintResult(const RE2& re, const StringPiece& input, RE2::Anchor anc
7373
// Processes a single generated regexp.
7474
// Compiles it using Regexp interface and PCRE, and then
7575
// checks that NFA, DFA, and PCRE all return the same results.
76-
void ExhaustiveTester::HandleRegexp(const string& const_regexp) {
76+
void ExhaustiveTester::HandleRegexp(const std::string& const_regexp) {
7777
regexps_++;
78-
string regexp = const_regexp;
78+
std::string regexp = const_regexp;
7979
if (!topwrapper_.empty())
8080
regexp = StringPrintf(topwrapper_.c_str(), regexp.c_str());
8181

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

143143
// Runs an exhaustive test on the given parameters.
144144
void ExhaustiveTest(int maxatoms, int maxops,
145-
const std::vector<string>& alphabet,
146-
const std::vector<string>& ops,
145+
const std::vector<std::string>& alphabet,
146+
const std::vector<std::string>& ops,
147147
int maxstrlen,
148-
const std::vector<string>& stralphabet,
149-
const string& wrapper,
150-
const string& topwrapper) {
148+
const std::vector<std::string>& stralphabet,
149+
const std::string& wrapper,
150+
const std::string& topwrapper) {
151151
if (RE2_DEBUG_MODE) {
152152
if (maxatoms > 1)
153153
maxatoms--;
@@ -169,9 +169,9 @@ void ExhaustiveTest(int maxatoms, int maxops,
169169

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

177177
for (int i = 0; i < arraysize(tops); i++) {

re2/testing/exhaustive_tester.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ class ExhaustiveTester : public RegexpGenerator {
4242
public:
4343
ExhaustiveTester(int maxatoms,
4444
int maxops,
45-
const std::vector<string>& alphabet,
46-
const std::vector<string>& ops,
45+
const std::vector<std::string>& alphabet,
46+
const std::vector<std::string>& ops,
4747
int maxstrlen,
48-
const std::vector<string>& stralphabet,
49-
const string& wrapper,
50-
const string& topwrapper)
48+
const std::vector<std::string>& stralphabet,
49+
const std::string& wrapper,
50+
const std::string& topwrapper)
5151
: RegexpGenerator(maxatoms, maxops, alphabet, ops),
5252
strgen_(maxstrlen, stralphabet),
5353
wrapper_(wrapper),
@@ -60,7 +60,7 @@ class ExhaustiveTester : public RegexpGenerator {
6060
int failures() { return failures_; }
6161

6262
// Needed for RegexpGenerator interface.
63-
void HandleRegexp(const string& regexp);
63+
void HandleRegexp(const std::string& regexp);
6464

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

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

8888
// Runs an exhaustive test on the given parameters.
8989
void ExhaustiveTest(int maxatoms, int maxops,
90-
const std::vector<string>& alphabet,
91-
const std::vector<string>& ops,
90+
const std::vector<std::string>& alphabet,
91+
const std::vector<std::string>& ops,
9292
int maxstrlen,
93-
const std::vector<string>& stralphabet,
94-
const string& wrapper,
95-
const string& topwrapper);
93+
const std::vector<std::string>& stralphabet,
94+
const std::string& wrapper,
95+
const std::string& topwrapper);
9696

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

103103
} // namespace re2
104104

re2/testing/regexp_generator.cc

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
namespace re2 {
3939

4040
// Returns a vector of the egrep regexp operators.
41-
const std::vector<string>& RegexpGenerator::EgrepOps() {
41+
const std::vector<std::string>& RegexpGenerator::EgrepOps() {
4242
static const char *ops[] = {
4343
"%s%s",
4444
"%s|%s",
@@ -47,13 +47,13 @@ const std::vector<string>& RegexpGenerator::EgrepOps() {
4747
"%s?",
4848
"%s\\C*",
4949
};
50-
static std::vector<string> v(ops, ops + arraysize(ops));
50+
static std::vector<std::string> v(ops, ops + arraysize(ops));
5151
return v;
5252
}
5353

5454
RegexpGenerator::RegexpGenerator(int maxatoms, int maxops,
55-
const std::vector<string>& atoms,
56-
const std::vector<string>& ops)
55+
const std::vector<std::string>& atoms,
56+
const std::vector<std::string>& ops)
5757
: maxatoms_(maxatoms), maxops_(maxops), atoms_(atoms), ops_(ops) {
5858
// Degenerate case.
5959
if (atoms_.empty())
@@ -65,7 +65,7 @@ RegexpGenerator::RegexpGenerator(int maxatoms, int maxops,
6565
// Generates all possible regular expressions (within the parameters),
6666
// calling HandleRegexp for each one.
6767
void RegexpGenerator::Generate() {
68-
std::vector<string> postfix;
68+
std::vector<std::string> postfix;
6969
GeneratePostfix(&postfix, 0, 0, 0);
7070
}
7171

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

7676
for (int i = 0; i < n; i++) {
77-
std::vector<string> postfix;
77+
std::vector<std::string> postfix;
7878
GenerateRandomPostfix(&postfix, 0, 0, 0);
7979
}
8080
}
8181

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

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

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

164164
// Add operators if there are enough arguments.
165165
if (ops < maxops_ && random_bit(rng_) == 0) {
166-
const string& fmt = ops_[random_ops_index(rng_)];
166+
const std::string& fmt = ops_[random_ops_index(rng_)];
167167
int nargs = CountArgs(fmt);
168168
if (nargs <= nstk) {
169169
post->push_back(fmt);
@@ -189,8 +189,8 @@ bool RegexpGenerator::GenerateRandomPostfix(std::vector<string>* post, int nstk,
189189
// Interprets the postfix command sequence to create a regular expression
190190
// passed to HandleRegexp. The results of operators like %s|%s are wrapped
191191
// in (?: ) to avoid needing to maintain a precedence table.
192-
void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
193-
std::stack<string> regexps;
192+
void RegexpGenerator::RunPostfix(const std::vector<std::string>& post) {
193+
std::stack<std::string> regexps;
194194
for (size_t i = 0; i < post.size(); i++) {
195195
switch (CountArgs(post[i])) {
196196
default:
@@ -199,15 +199,15 @@ void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
199199
regexps.push(post[i]);
200200
break;
201201
case 1: {
202-
string a = regexps.top();
202+
std::string a = regexps.top();
203203
regexps.pop();
204204
regexps.push("(?:" + StringPrintf(post[i].c_str(), a.c_str()) + ")");
205205
break;
206206
}
207207
case 2: {
208-
string b = regexps.top();
208+
std::string b = regexps.top();
209209
regexps.pop();
210-
string a = regexps.top();
210+
std::string a = regexps.top();
211211
regexps.pop();
212212
regexps.push("(?:" +
213213
StringPrintf(post[i].c_str(), a.c_str(), b.c_str()) +
@@ -238,38 +238,38 @@ void RegexpGenerator::RunPostfix(const std::vector<string>& post) {
238238
}
239239

240240
// Split s into an vector of strings, one for each UTF-8 character.
241-
std::vector<string> Explode(const StringPiece& s) {
242-
std::vector<string> v;
241+
std::vector<std::string> Explode(const StringPiece& s) {
242+
std::vector<std::string> v;
243243

244244
for (const char *q = s.begin(); q < s.end(); ) {
245245
const char* p = q;
246246
Rune r;
247247
q += chartorune(&r, q);
248-
v.push_back(string(p, q - p));
248+
v.push_back(std::string(p, q - p));
249249
}
250250

251251
return v;
252252
}
253253

254254
// Split string everywhere a substring is found, returning
255255
// vector of pieces.
256-
std::vector<string> Split(const StringPiece& sep, const StringPiece& s) {
257-
std::vector<string> v;
256+
std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s) {
257+
std::vector<std::string> v;
258258

259259
if (sep.size() == 0)
260260
return Explode(s);
261261

262262
const char *p = s.begin();
263263
for (const char *q = s.begin(); q + sep.size() <= s.end(); q++) {
264264
if (StringPiece(q, sep.size()) == sep) {
265-
v.push_back(string(p, q - p));
265+
v.push_back(std::string(p, q - p));
266266
p = q + sep.size();
267267
q = p - 1; // -1 for ++ in loop
268268
continue;
269269
}
270270
}
271271
if (p < s.end())
272-
v.push_back(string(p, s.end() - p));
272+
v.push_back(std::string(p, s.end() - p));
273273
return v;
274274
}
275275

re2/testing/regexp_generator.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ namespace re2 {
2929
//
3030
class RegexpGenerator {
3131
public:
32-
RegexpGenerator(int maxatoms, int maxops, const std::vector<string>& atoms,
33-
const std::vector<string>& ops);
32+
RegexpGenerator(int maxatoms, int maxops,
33+
const std::vector<std::string>& atoms,
34+
const std::vector<std::string>& ops);
3435
virtual ~RegexpGenerator() {}
3536

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

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

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

4849
private:
49-
void RunPostfix(const std::vector<string>& post);
50-
void GeneratePostfix(std::vector<string>* post, int nstk, int ops, int lits);
51-
bool GenerateRandomPostfix(std::vector<string>* post, int nstk, int ops,
52-
int lits);
53-
54-
int maxatoms_; // Maximum number of atoms allowed in expr.
55-
int maxops_; // Maximum number of ops allowed in expr.
56-
std::vector<string> atoms_; // Possible atoms.
57-
std::vector<string> ops_; // Possible ops.
58-
std::minstd_rand0 rng_; // Random number generator.
50+
void RunPostfix(const std::vector<std::string>& post);
51+
void GeneratePostfix(std::vector<std::string>* post,
52+
int nstk, int ops, int lits);
53+
bool GenerateRandomPostfix(std::vector<std::string>* post,
54+
int nstk, int ops, int lits);
55+
56+
int maxatoms_; // Maximum number of atoms allowed in expr.
57+
int maxops_; // Maximum number of ops allowed in expr.
58+
std::vector<std::string> atoms_; // Possible atoms.
59+
std::vector<std::string> ops_; // Possible ops.
60+
std::minstd_rand0 rng_; // Random number generator.
5961

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

6668
// Returns one string for each character in s.
67-
std::vector<string> Explode(const StringPiece& s);
69+
std::vector<std::string> Explode(const StringPiece& s);
6870

6971
// Splits string everywhere sep is found, returning
7072
// vector of pieces.
71-
std::vector<string> Split(const StringPiece& sep, const StringPiece& s);
73+
std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s);
7274

7375
} // namespace re2
7476

re2/testing/string_generator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace re2 {
1919

2020
StringGenerator::StringGenerator(int maxlen,
21-
const std::vector<string>& alphabet)
21+
const std::vector<std::string>& alphabet)
2222
: maxlen_(maxlen), alphabet_(alphabet),
2323
generate_null_(false),
2424
random_(false), nrandom_(0) {

re2/testing/string_generator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace re2 {
2121

2222
class StringGenerator {
2323
public:
24-
StringGenerator(int maxlen, const std::vector<string>& alphabet);
24+
StringGenerator(int maxlen, const std::vector<std::string>& alphabet);
2525
~StringGenerator() {}
2626

2727
const StringPiece& Next();
@@ -41,12 +41,12 @@ class StringGenerator {
4141
bool RandomDigits();
4242

4343
// Global state.
44-
int maxlen_; // Maximum length string to generate.
45-
std::vector<string> alphabet_; // Alphabet, one string per letter.
44+
int maxlen_; // Maximum length string to generate.
45+
std::vector<std::string> alphabet_; // Alphabet, one string per letter.
4646

4747
// Iteration state.
4848
StringPiece sp_; // Last StringPiece returned by Next().
49-
string s_; // String data in last StringPiece returned by Next().
49+
std::string s_; // String data in last StringPiece returned by Next().
5050
bool hasnext_; // Whether Next() can be called again.
5151
std::vector<int> digits_; // Alphabet indices for next string.
5252
bool generate_null_; // Whether to generate a NULL StringPiece next.

0 commit comments

Comments
 (0)