• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BENCHMARK_RE_H_
16 #define BENCHMARK_RE_H_
17 
18 #if defined(HAVE_STD_REGEX)
19 #include <regex>
20 #elif defined(HAVE_GNU_POSIX_REGEX)
21 #include <gnuregex.h>
22 #elif defined(HAVE_POSIX_REGEX)
23 #include <regex.h>
24 #else
25 #error No regular expression backend was found!
26 #endif
27 #include <string>
28 
29 namespace benchmark {
30 
31 // A wrapper around the POSIX regular expression API that provides automatic
32 // cleanup
33 class Regex {
34  public:
35   Regex();
36   ~Regex();
37 
38   // Compile a regular expression matcher from spec.  Returns true on success.
39   //
40   // On failure (and if error is not nullptr), error is populated with a human
41   // readable error message if an error occurs.
42   bool Init(const std::string& spec, std::string* error);
43 
44   // Returns whether str matches the compiled regular expression.
45   bool Match(const std::string& str);
46  private:
47   bool init_;
48   // Underlying regular expression object
49 #if defined(HAVE_STD_REGEX)
50   std::regex re_;
51 #elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
52   regex_t re_;
53 #else
54 # error No regular expression backend implementation available
55 #endif
56 };
57 
58 }  // end namespace benchmark
59 
60 #endif  // BENCHMARK_RE_H_
61