OLD | NEW |
1 // Copyright 2008 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2008 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // String generator: generates all possible strings of up to | 5 // String generator: generates all possible strings of up to |
6 // maxlen letters using the set of letters in alpha. | 6 // maxlen letters using the set of letters in alpha. |
7 // Fetch strings using a Java-like Next()/HasNext() interface. | 7 // Fetch strings using a Java-like Next()/HasNext() interface. |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 25 matching lines...) Expand all Loading... |
36 hasnext_ = true; | 36 hasnext_ = true; |
37 random_ = false; | 37 random_ = false; |
38 nrandom_ = 0; | 38 nrandom_ = 0; |
39 generate_null_ = false; | 39 generate_null_ = false; |
40 } | 40 } |
41 | 41 |
42 // Increments the big number in digits_, returning true if successful. | 42 // Increments the big number in digits_, returning true if successful. |
43 // Returns false if all the numbers have been used. | 43 // Returns false if all the numbers have been used. |
44 bool StringGenerator::IncrementDigits() { | 44 bool StringGenerator::IncrementDigits() { |
45 // First try to increment the current number. | 45 // First try to increment the current number. |
46 for (int i = digits_.size() - 1; i >= 0; i--) { | 46 for (int i = static_cast<int>(digits_.size()) - 1; i >= 0; i--) { |
47 if (++digits_[i] < alphabet_.size()) | 47 if (++digits_[i] < static_cast<int>(alphabet_.size())) |
48 return true; | 48 return true; |
49 digits_[i] = 0; | 49 digits_[i] = 0; |
50 } | 50 } |
51 | 51 |
52 // If that failed, make a longer number. | 52 // If that failed, make a longer number. |
53 if (digits_.size() < maxlen_) { | 53 if (static_cast<int>(digits_.size()) < maxlen_) { |
54 digits_.push_back(0); | 54 digits_.push_back(0); |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 return false; | 58 return false; |
59 } | 59 } |
60 | 60 |
61 // Generates random digits_, return true if successful. | 61 // Generates random digits_, return true if successful. |
62 // Returns false if the random sequence is over. | 62 // Returns false if the random sequence is over. |
63 bool StringGenerator::RandomDigits() { | 63 bool StringGenerator::RandomDigits() { |
64 if (--nrandom_ <= 0) | 64 if (--nrandom_ <= 0) |
65 return false; | 65 return false; |
66 | 66 |
67 // Pick length. | 67 // Pick length. |
68 int len = acm_->Uniform(maxlen_+1); | 68 int len = acm_->Uniform(maxlen_+1); |
69 digits_.resize(len); | 69 digits_.resize(len); |
70 for (int i = 0; i < len; i++) | 70 for (int i = 0; i < len; i++) |
71 digits_[i] = acm_->Uniform(alphabet_.size()); | 71 digits_[i] = acm_->Uniform(static_cast<int32>(alphabet_.size())); |
72 return true; | 72 return true; |
73 } | 73 } |
74 | 74 |
75 // Returns the next string in the iteration, which is the one | 75 // Returns the next string in the iteration, which is the one |
76 // currently described by digits_. Calls IncrementDigits | 76 // currently described by digits_. Calls IncrementDigits |
77 // after computing the string, so that it knows the answer | 77 // after computing the string, so that it knows the answer |
78 // for subsequent HasNext() calls. | 78 // for subsequent HasNext() calls. |
79 const StringPiece& StringGenerator::Next() { | 79 const StringPiece& StringGenerator::Next() { |
80 CHECK(hasnext_); | 80 CHECK(hasnext_); |
81 if (generate_null_) { | 81 if (generate_null_) { |
82 generate_null_ = false; | 82 generate_null_ = false; |
83 sp_ = NULL; | 83 sp_ = NULL; |
84 return sp_; | 84 return sp_; |
85 } | 85 } |
86 s_.clear(); | 86 s_.clear(); |
87 for (int i = 0; i < digits_.size(); i++) { | 87 for (size_t i = 0; i < digits_.size(); i++) { |
88 s_ += alphabet_[digits_[i]]; | 88 s_ += alphabet_[digits_[i]]; |
89 } | 89 } |
90 hasnext_ = random_ ? RandomDigits() : IncrementDigits(); | 90 hasnext_ = random_ ? RandomDigits() : IncrementDigits(); |
91 sp_ = s_; | 91 sp_ = s_; |
92 return sp_; | 92 return sp_; |
93 } | 93 } |
94 | 94 |
95 // Sets generator up to return n random strings. | 95 // Sets generator up to return n random strings. |
96 void StringGenerator::Random(int32 seed, int n) { | 96 void StringGenerator::Random(int32 seed, int n) { |
97 if (acm_ == NULL) | 97 if (acm_ == NULL) |
98 acm_ = new ACMRandom(seed); | 98 acm_ = new ACMRandom(seed); |
99 else | 99 else |
100 acm_->Reset(seed); | 100 acm_->Reset(seed); |
101 | 101 |
102 random_ = true; | 102 random_ = true; |
103 nrandom_ = n; | 103 nrandom_ = n; |
104 hasnext_ = nrandom_ > 0; | 104 hasnext_ = nrandom_ > 0; |
105 } | 105 } |
106 | 106 |
107 void StringGenerator::GenerateNULL() { | 107 void StringGenerator::GenerateNULL() { |
108 generate_null_ = true; | 108 generate_null_ = true; |
109 hasnext_ = true; | 109 hasnext_ = true; |
110 } | 110 } |
111 | 111 |
112 } // namespace re2 | 112 } // namespace re2 |
113 | |
OLD | NEW |