10d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Copyright 2008 The RE2 Authors. All Rights Reserved. 20d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Use of this source code is governed by a BSD-style 30d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// license that can be found in the LICENSE file. 40d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 50d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// String generator: generates all possible strings of up to 60d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// maxlen letters using the set of letters in alpha. 70d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Fetch strings using a Java-like Next()/HasNext() interface. 80d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 90d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#ifndef RE2_TESTING_STRING_GENERATOR_H__ 100d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#define RE2_TESTING_STRING_GENERATOR_H__ 110d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 120d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include <string> 130d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include <vector> 140d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "util/util.h" 150d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "util/random.h" 160d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "re2/stringpiece.h" 170d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 180d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinnamespace re2 { 190d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 200d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinclass StringGenerator { 210d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin public: 220d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin StringGenerator(int maxlen, const vector<string>& alphabet); 230d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin ~StringGenerator(); 240d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin const StringPiece& Next(); 250d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool HasNext() { return hasnext_; } 260d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 270d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin // Resets generator to start sequence over. 280d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin void Reset(); 290d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 300d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin // Causes generator to emit random strings for next n calls to Next(). 310d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin void Random(int32 seed, int n); 320d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 330d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin // Causes generator to emit a NULL as the next call. 340d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin void GenerateNULL(); 350d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 360d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin private: 370d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool IncrementDigits(); 380d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool RandomDigits(); 390d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 400d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin // Global state. 410d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin int maxlen_; // Maximum length string to generate. 420d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin vector<string> alphabet_; // Alphabet, one string per letter. 430d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 440d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin // Iteration state. 450d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin StringPiece sp_; // Last StringPiece returned by Next(). 460d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin string s_; // String data in last StringPiece returned by Next(). 470d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool hasnext_; // Whether Next() can be called again. 480d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin vector<int> digits_; // Alphabet indices for next string. 490d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool generate_null_; // Whether to generate a NULL StringPiece next. 500d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool random_; // Whether generated strings are random. 510d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin int nrandom_; // Number of random strings left to generate. 520d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin ACMRandom* acm_; // Random number generator 530d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin DISALLOW_EVIL_CONSTRUCTORS(StringGenerator); 540d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}; 550d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 560d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} // namespace re2 570d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 580d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#endif // RE2_TESTING_STRING_GENERATOR_H__ 59