re2/re2/testing/regexp_generator.h
Go to the documentation of this file.
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 #ifndef RE2_TESTING_REGEXP_GENERATOR_H_
6 #define RE2_TESTING_REGEXP_GENERATOR_H_
7 
8 // Regular expression generator: generates all possible
9 // regular expressions within given parameters (see below for details).
10 
11 #include <stdint.h>
12 #include <random>
13 #include <string>
14 #include <vector>
15 
16 #include "util/util.h"
17 #include "re2/stringpiece.h"
18 
19 namespace re2 {
20 
21 // Regular expression generator.
22 //
23 // Given a set of atom expressions like "a", "b", or "."
24 // and operators like "%s*", generates all possible regular expressions
25 // using at most maxbases base expressions and maxops operators.
26 // For each such expression re, calls HandleRegexp(re).
27 //
28 // Callers are expected to subclass RegexpGenerator and provide HandleRegexp.
29 //
30 class RegexpGenerator {
31  public:
32  RegexpGenerator(int maxatoms, int maxops,
33  const std::vector<std::string>& atoms,
34  const std::vector<std::string>& ops);
35  virtual ~RegexpGenerator() {}
36 
37  // Generates all the regular expressions, calling HandleRegexp(re) for each.
38  void Generate();
39 
40  // Generates n random regular expressions, calling HandleRegexp(re) for each.
41  void GenerateRandom(int32_t seed, int n);
42 
43  // Handles a regular expression. Must be provided by subclass.
44  virtual void HandleRegexp(const std::string& regexp) = 0;
45 
46  // The egrep regexp operators: * + ? | and concatenation.
47  static const std::vector<std::string>& EgrepOps();
48 
49  private:
50  void RunPostfix(const std::vector<std::string>& post);
51  void GeneratePostfix(std::vector<std::string>* post,
52  int nstk, int ops, int lits);
53  bool GenerateRandomPostfix(std::vector<std::string>* post,
54  int nstk, int ops, int lits);
55 
56  int maxatoms_; // Maximum number of atoms allowed in expr.
57  int maxops_; // Maximum number of ops allowed in expr.
58  std::vector<std::string> atoms_; // Possible atoms.
59  std::vector<std::string> ops_; // Possible ops.
60  std::minstd_rand0 rng_; // Random number generator.
61 
62  RegexpGenerator(const RegexpGenerator&) = delete;
63  RegexpGenerator& operator=(const RegexpGenerator&) = delete;
64 };
65 
66 // Helpers for preparing arguments to RegexpGenerator constructor.
67 
68 // Returns one string for each character in s.
69 std::vector<std::string> Explode(const StringPiece& s);
70 
71 // Splits string everywhere sep is found, returning
72 // vector of pieces.
73 std::vector<std::string> Split(const StringPiece& sep, const StringPiece& s);
74 
75 } // namespace re2
76 
77 #endif // RE2_TESTING_REGEXP_GENERATOR_H_
re2::RegexpGenerator::ops_
std::vector< std::string > ops_
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:59
post
static void post(QUEUE *q, enum uv__work_kind kind)
Definition: threadpool.c:142
re2::Split
std::vector< std::string > Split(const StringPiece &sep, const StringPiece &s)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:256
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
re2::RegexpGenerator::maxatoms_
int maxatoms_
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:56
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::RegexpGenerator::RegexpGenerator
RegexpGenerator(int maxatoms, int maxops, const std::vector< std::string > &atoms, const std::vector< std::string > &ops)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:54
re2::RegexpGenerator::RunPostfix
void RunPostfix(const std::vector< std::string > &post)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:192
re2::RegexpGenerator::~RegexpGenerator
virtual ~RegexpGenerator()
Definition: re2/re2/testing/regexp_generator.h:35
text_format_test_wrapper.sep
sep
Definition: text_format_test_wrapper.py:34
re2::RegexpGenerator::atoms_
std::vector< std::string > atoms_
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:58
re2::RegexpGenerator::operator=
RegexpGenerator & operator=(const RegexpGenerator &)=delete
stdint.h
re2::RegexpGenerator::HandleRegexp
virtual void HandleRegexp(const std::string &regexp)=0
re2::RegexpGenerator::Generate
void Generate()
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:67
re2::RegexpGenerator::EgrepOps
static const std::vector< std::string > & EgrepOps()
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:41
re2::Explode
std::vector< std::string > Explode(const StringPiece &s)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:241
re2::RegexpGenerator
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:30
re2::RegexpGenerator::GeneratePostfix
void GeneratePostfix(std::vector< std::string > *post, int nstk, int ops, int lits)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:106
re2::RegexpGenerator::GenerateRandom
void GenerateRandom(int32_t seed, int n)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:73
re2::RegexpGenerator::GenerateRandomPostfix
bool GenerateRandomPostfix(std::vector< std::string > *post, int nstk, int ops, int lits)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:142
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
re2::RegexpGenerator::maxops_
int maxops_
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:57
re2::RegexpGenerator::rng_
std::minstd_rand0 rng_
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.h:60


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:02