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.
4
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.
8
9#ifndef RE2_TESTING_STRING_GENERATOR_H__
10#define RE2_TESTING_STRING_GENERATOR_H__
11
12#include <string>
13#include <vector>
14#include "util/util.h"
15#include "util/random.h"
16#include "re2/stringpiece.h"
17
18namespace re2 {
19
20class StringGenerator {
21 public:
22  StringGenerator(int maxlen, const vector<string>& alphabet);
23  ~StringGenerator();
24  const StringPiece& Next();
25  bool HasNext() { return hasnext_; }
26
27  // Resets generator to start sequence over.
28  void Reset();
29
30  // Causes generator to emit random strings for next n calls to Next().
31  void Random(int32 seed, int n);
32
33  // Causes generator to emit a NULL as the next call.
34  void GenerateNULL();
35
36 private:
37  bool IncrementDigits();
38  bool RandomDigits();
39
40  // Global state.
41  int maxlen_;               // Maximum length string to generate.
42  vector<string> alphabet_;  // Alphabet, one string per letter.
43
44  // Iteration state.
45  StringPiece sp_;           // Last StringPiece returned by Next().
46  string s_;                 // String data in last StringPiece returned by Next().
47  bool hasnext_;             // Whether Next() can be called again.
48  vector<int> digits_;       // Alphabet indices for next string.
49  bool generate_null_;       // Whether to generate a NULL StringPiece next.
50  bool random_;              // Whether generated strings are random.
51  int nrandom_;              // Number of random strings left to generate.
52  ACMRandom* acm_;           // Random number generator
53  DISALLOW_EVIL_CONSTRUCTORS(StringGenerator);
54};
55
56}  // namespace re2
57
58#endif  // RE2_TESTING_STRING_GENERATOR_H__
59