You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2019/02/01 23:07:20 UTC

[trafficserver] branch master updated: Regex: update to use string_view.

This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d21674  Regex: update to use string_view.
3d21674 is described below

commit 3d21674ccc729cd753c85cab6ffbebafa7f65386
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Mon Jan 14 17:17:18 2019 -0600

    Regex: update to use string_view.
---
 include/tscore/Regex.h         | 44 ++++++++++++++++++++++++++++++++++++------
 proxy/http/remap/UrlRewrite.cc |  3 ++-
 src/tscore/Regex.cc            | 22 +++++++++------------
 3 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/include/tscore/Regex.h b/include/tscore/Regex.h
index 8888ae6..f80121e 100644
--- a/include/tscore/Regex.h
+++ b/include/tscore/Regex.h
@@ -23,6 +23,8 @@
 
 #pragma once
 
+#include <string_view>
+
 #include "tscore/ink_config.h"
 
 #ifdef HAVE_PCRE_PCRE_H
@@ -45,16 +47,46 @@ class Regex
 {
 public:
   /// Default number of capture groups.
-  static constexpr size_t DEFAULT_GROUP_COUNT = 30;
+  static constexpr size_t DEFAULT_GROUP_COUNT = 10;
 
   Regex() = default;
+  ~Regex();
+
+  /** Compile the @a pattern into a regular expression.
+   *
+   * @param pattern Source pattern for regular expression (null terminated).
+   * @param flags Compilation flags.
+   * @return @a true if compiled successfully, @a false otherwise.
+   *
+   * @a flags should be the bitwise @c or of @c REFlags values.
+   */
   bool compile(const char *pattern, const unsigned flags = 0);
-  // It is safe to call exec() concurrently on the same object instance
-  bool exec(const char *str);
-  bool exec(const char *str, int length);
-  bool exec(const char *str, int length, int *ovector, int ovecsize);
+
+  /** Execute the regular expression.
+   *
+   * @param str String to match against.
+   * @return @c true if the patter matched, @a false if not.
+   *
+   * It is safe to call this method concurrently on the same instance of @a this.
+   */
+  bool exec(std::string_view const &str);
+
+  /** Execute the regular expression.
+   *
+   * @param str String to match against.
+   * @param ovector Capture results.
+   * @param ovecsize Number of elements in @a ovector.
+   * @return @c true if the patter matched, @a false if not.
+   *
+   * It is safe to call this method concurrently on the same instance of @a this.
+   *
+   * Each capture group takes 3 elements of @a ovector, therefore @a ovecsize must
+   * be a multiple of 3 and at least three times the number of desired capture groups.
+   */
+  bool exec(std::string_view const &str, int *ovector, int ovecsize);
+
+  /// @return The number of groups captured in the last call to @c exec.
   int get_capture_count();
-  ~Regex();
 
 private:
   pcre *regex             = nullptr;
diff --git a/proxy/http/remap/UrlRewrite.cc b/proxy/http/remap/UrlRewrite.cc
index 3f3d0ed..13e3980 100644
--- a/proxy/http/remap/UrlRewrite.cc
+++ b/proxy/http/remap/UrlRewrite.cc
@@ -897,7 +897,8 @@ UrlRewrite::_regexMappingLookup(RegexMappingList &regex_mappings, URL *request_u
     }
 
     int matches_info[MAX_REGEX_SUBS * 3];
-    bool match_result = list_iter->regular_expression.exec(request_host, request_host_len, matches_info, countof(matches_info));
+    bool match_result =
+      list_iter->regular_expression.exec(std::string_view(request_host, request_host_len), matches_info, countof(matches_info));
 
     if (match_result == true) {
       Debug("url_rewrite_regex",
diff --git a/src/tscore/Regex.cc b/src/tscore/Regex.cc
index 7902892..d067072 100644
--- a/src/tscore/Regex.cc
+++ b/src/tscore/Regex.cc
@@ -21,6 +21,8 @@
   limitations under the License.
  */
 
+#include <array>
+
 #include "tscore/ink_platform.h"
 #include "tscore/ink_thread.h"
 #include "tscore/ink_memory.h"
@@ -101,25 +103,19 @@ Regex::get_capture_count()
 }
 
 bool
-Regex::exec(const char *str)
-{
-  return exec(str, strlen(str));
-}
-
-bool
-Regex::exec(const char *str, int length)
+Regex::exec(std::string_view const &str)
 {
-  int ovector[DEFAULT_GROUP_COUNT];
-  return exec(str, length, ovector, countof(ovector));
+  std::array<int, DEFAULT_GROUP_COUNT * 3> ovector;
+  return this->exec(str, ovector.data(), ovector.size());
 }
 
 bool
-Regex::exec(const char *str, int length, int *ovector, int ovecsize)
+Regex::exec(std::string_view const &str, int *ovector, int ovecsize)
 {
   int rv;
 
-  rv = pcre_exec(regex, regex_extra, str, length, 0, 0, ovector, ovecsize);
-  return rv > 0 ? true : false;
+  rv = pcre_exec(regex, regex_extra, str.data(), str.size(), 0, 0, ovector, ovecsize);
+  return rv > 0;
 }
 
 Regex::~Regex()
@@ -238,7 +234,7 @@ DFA::match(const char *str, int length) const
   dfa_pattern *p = _my_patterns;
 
   while (p) {
-    rc = p->_re->exec(str, length);
+    rc = p->_re->exec({str, size_t(length)});
     if (rc > 0) {
       return p->_idx;
     }