1 // Copyright 2008 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // String generator: generates all possible strings of up to
6 // maxlen letters using the set of letters in alpha.
7 // Fetch strings using a Java-like Next()/HasNext() interface.
11 #include "util/test.h"
12 #include "re2/testing/string_generator.h"
16 StringGenerator::StringGenerator(int maxlen, const vector<string>& alphabet)
17 : maxlen_(maxlen), alphabet_(alphabet),
18 generate_null_(false),
19 random_(false), nrandom_(0), acm_(NULL) {
21 // Degenerate case: no letters, no non-empty strings.
22 if (alphabet_.size() == 0)
25 // Next() will return empty string (digits_ is empty).
29 StringGenerator::~StringGenerator() {
33 // Resets the string generator state to the beginning.
34 void StringGenerator::Reset() {
39 generate_null_ = false;
42 // Increments the big number in digits_, returning true if successful.
43 // Returns false if all the numbers have been used.
44 bool StringGenerator::IncrementDigits() {
45 // First try to increment the current number.
46 for (int i = digits_.size() - 1; i >= 0; i--) {
47 if (++digits_[i] < alphabet_.size())
52 // If that failed, make a longer number.
53 if (digits_.size() < maxlen_) {
61 // Generates random digits_, return true if successful.
62 // Returns false if the random sequence is over.
63 bool StringGenerator::RandomDigits() {
68 int len = acm_->Uniform(maxlen_+1);
70 for (int i = 0; i < len; i++)
71 digits_[i] = acm_->Uniform(alphabet_.size());
75 // Returns the next string in the iteration, which is the one
76 // currently described by digits_. Calls IncrementDigits
77 // after computing the string, so that it knows the answer
78 // for subsequent HasNext() calls.
79 const StringPiece& StringGenerator::Next() {
82 generate_null_ = false;
87 for (int i = 0; i < digits_.size(); i++) {
88 s_ += alphabet_[digits_[i]];
90 hasnext_ = random_ ? RandomDigits() : IncrementDigits();
95 // Sets generator up to return n random strings.
96 void StringGenerator::Random(int32 seed, int n) {
98 acm_ = new ACMRandom(seed);
104 hasnext_ = nrandom_ > 0;
107 void StringGenerator::GenerateNULL() {
108 generate_null_ = true;