This source file includes following definitions.
- acm_
- Reset
- IncrementDigits
- RandomDigits
- Next
- Random
- GenerateNULL
#include <string>
#include <vector>
#include "util/test.h"
#include "re2/testing/string_generator.h"
namespace re2 {
StringGenerator::StringGenerator(int maxlen, const vector<string>& alphabet)
: maxlen_(maxlen), alphabet_(alphabet),
generate_null_(false),
random_(false), nrandom_(0), acm_(NULL) {
if (alphabet_.size() == 0)
maxlen_ = 0;
hasnext_ = true;
}
StringGenerator::~StringGenerator() {
delete acm_;
}
void StringGenerator::Reset() {
digits_.clear();
hasnext_ = true;
random_ = false;
nrandom_ = 0;
generate_null_ = false;
}
bool StringGenerator::IncrementDigits() {
for (int i = digits_.size() - 1; i >= 0; i--) {
if (++digits_[i] < alphabet_.size())
return true;
digits_[i] = 0;
}
if (digits_.size() < maxlen_) {
digits_.push_back(0);
return true;
}
return false;
}
bool StringGenerator::RandomDigits() {
if (--nrandom_ <= 0)
return false;
int len = acm_->Uniform(maxlen_+1);
digits_.resize(len);
for (int i = 0; i < len; i++)
digits_[i] = acm_->Uniform(alphabet_.size());
return true;
}
const StringPiece& StringGenerator::Next() {
CHECK(hasnext_);
if (generate_null_) {
generate_null_ = false;
sp_ = NULL;
return sp_;
}
s_.clear();
for (int i = 0; i < digits_.size(); i++) {
s_ += alphabet_[digits_[i]];
}
hasnext_ = random_ ? RandomDigits() : IncrementDigits();
sp_ = s_;
return sp_;
}
void StringGenerator::Random(int32 seed, int n) {
if (acm_ == NULL)
acm_ = new ACMRandom(seed);
else
acm_->Reset(seed);
random_ = true;
nrandom_ = n;
hasnext_ = nrandom_ > 0;
}
void StringGenerator::GenerateNULL() {
generate_null_ = true;
hasnext_ = true;
}
}