re.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef BENCHMARK_RE_H_
  15. #define BENCHMARK_RE_H_
  16. #include "internal_macros.h"
  17. // clang-format off
  18. #if !defined(HAVE_STD_REGEX) && \
  19. !defined(HAVE_GNU_POSIX_REGEX) && \
  20. !defined(HAVE_POSIX_REGEX)
  21. // No explicit regex selection; detect based on builtin hints.
  22. #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE)
  23. #define HAVE_POSIX_REGEX 1
  24. #elif __cplusplus >= 199711L
  25. #define HAVE_STD_REGEX 1
  26. #endif
  27. #endif
  28. // Prefer C regex libraries when compiling w/o exceptions so that we can
  29. // correctly report errors.
  30. #if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \
  31. defined(HAVE_STD_REGEX) && \
  32. (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX))
  33. #undef HAVE_STD_REGEX
  34. #endif
  35. #if defined(HAVE_STD_REGEX)
  36. #include <regex>
  37. #elif defined(HAVE_GNU_POSIX_REGEX)
  38. #include <gnuregex.h>
  39. #elif defined(HAVE_POSIX_REGEX)
  40. #include <regex.h>
  41. #else
  42. #error No regular expression backend was found!
  43. #endif
  44. // clang-format on
  45. #include <string>
  46. #include "check.h"
  47. namespace benchmark {
  48. // A wrapper around the POSIX regular expression API that provides automatic
  49. // cleanup
  50. class Regex {
  51. public:
  52. Regex() : init_(false) {}
  53. ~Regex();
  54. // Compile a regular expression matcher from spec. Returns true on success.
  55. //
  56. // On failure (and if error is not nullptr), error is populated with a human
  57. // readable error message if an error occurs.
  58. bool Init(const std::string& spec, std::string* error);
  59. // Returns whether str matches the compiled regular expression.
  60. bool Match(const std::string& str);
  61. private:
  62. bool init_;
  63. // Underlying regular expression object
  64. #if defined(HAVE_STD_REGEX)
  65. std::regex re_;
  66. #elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
  67. regex_t re_;
  68. #else
  69. #error No regular expression backend implementation available
  70. #endif
  71. };
  72. #if defined(HAVE_STD_REGEX)
  73. inline bool Regex::Init(const std::string& spec, std::string* error) {
  74. #ifdef BENCHMARK_HAS_NO_EXCEPTIONS
  75. ((void)error); // suppress unused warning
  76. #else
  77. try {
  78. #endif
  79. re_ = std::regex(spec, std::regex_constants::extended);
  80. init_ = true;
  81. #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
  82. }
  83. catch (const std::regex_error& e) {
  84. if (error) {
  85. *error = e.what();
  86. }
  87. }
  88. #endif
  89. return init_;
  90. }
  91. inline Regex::~Regex() {}
  92. inline bool Regex::Match(const std::string& str) {
  93. if (!init_) {
  94. return false;
  95. }
  96. return std::regex_search(str, re_);
  97. }
  98. #else
  99. inline bool Regex::Init(const std::string& spec, std::string* error) {
  100. int ec = regcomp(&re_, spec.c_str(), REG_EXTENDED | REG_NOSUB);
  101. if (ec != 0) {
  102. if (error) {
  103. size_t needed = regerror(ec, &re_, nullptr, 0);
  104. char* errbuf = new char[needed];
  105. regerror(ec, &re_, errbuf, needed);
  106. // regerror returns the number of bytes necessary to null terminate
  107. // the string, so we move that when assigning to error.
  108. BM_CHECK_NE(needed, 0);
  109. error->assign(errbuf, needed - 1);
  110. delete[] errbuf;
  111. }
  112. return false;
  113. }
  114. init_ = true;
  115. return true;
  116. }
  117. inline Regex::~Regex() {
  118. if (init_) {
  119. regfree(&re_);
  120. }
  121. }
  122. inline bool Regex::Match(const std::string& str) {
  123. if (!init_) {
  124. return false;
  125. }
  126. return regexec(&re_, str.c_str(), 0, nullptr, 0) == 0;
  127. }
  128. #endif
  129. } // end namespace benchmark
  130. #endif // BENCHMARK_RE_H_