[v3,07/21] Add POSIX regex wrapper functions.

Message ID 20200424092132.150547-8-gprocida@google.com
State Superseded
Headers
Series Simplify regex and suppression parsing. |

Commit Message

Giuliano Procida April 24, 2020, 9:21 a.m. UTC
  libabigail code uses the POSIX regex library consistently:

    - compile std::string to regex, with the flag REG_EXTENDED
    - store regex using a shared pointer wrapper
    - check match of regex against std::string

All the C string / std::string logic and so on is repeated at every
call site. This patch introduces wrapper functions to take care of
this logic.

There are no behavioural changes.

	* include/abg-regex.h (compile): Declare new function.
	(match): Declare new function.
	* src/abg-regex.cc (compile): Add new function wrapping
	regcomp. (match): Add new function wrapping regexec.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 include/abg-regex.h |  6 ++++++
 src/abg-regex.cc    | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
  

Comments

Matthias Männich April 27, 2020, 11:23 a.m. UTC | #1
On Fri, Apr 24, 2020 at 10:21:18AM +0100, Giuliano Procida wrote:
>libabigail code uses the POSIX regex library consistently:
>
>    - compile std::string to regex, with the flag REG_EXTENDED
>    - store regex using a shared pointer wrapper
>    - check match of regex against std::string
>
>All the C string / std::string logic and so on is repeated at every
>call site. This patch introduces wrapper functions to take care of
>this logic.
>
>There are no behavioural changes.
>
>	* include/abg-regex.h (compile): Declare new function.
>	(match): Declare new function.
>	* src/abg-regex.cc (compile): Add new function wrapping
>	regcomp. (match): Add new function wrapping regexec.
>
>Signed-off-by: Giuliano Procida <gprocida@google.com>

Reviewed-by: Matthias Maennich <maennich@google.com>

Cheers,
Matthias
>---
> include/abg-regex.h |  6 ++++++
> src/abg-regex.cc    | 41 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 47 insertions(+)
>
>diff --git a/include/abg-regex.h b/include/abg-regex.h
>index 59976794..c7951a8f 100644
>--- a/include/abg-regex.h
>+++ b/include/abg-regex.h
>@@ -71,6 +71,12 @@ operator<<(std::ostream& os, const escape& esc);
> std::string
> generate_from_strings(const std::vector<std::string>& strs);
>
>+regex_t_sptr
>+compile(const std::string& str);
>+
>+bool
>+match(const regex_t_sptr& r, const std::string& str);
>+
> }// end namespace regex
>
> /// Specialization of sptr_utils::build_sptr for regex_t.
>diff --git a/src/abg-regex.cc b/src/abg-regex.cc
>index 90e4d144..7e5277e2 100644
>--- a/src/abg-regex.cc
>+++ b/src/abg-regex.cc
>@@ -23,11 +23,22 @@
> /// Some specialization for shared pointer utility templates.
> ///
>
>+#include "config.h"
>+
> #include <sstream>
> #include <ostream>
>+
>+#include "abg-internal.h"
>+
>+// <headers defining libabigail's API go under here>
>+ABG_BEGIN_EXPORT_DECLARATIONS
>+
> #include "abg-sptr-utils.h"
> #include "abg-regex.h"
>
>+ABG_END_EXPORT_DECLARATIONS
>+// </headers defining libabigail's API>
>+
> namespace abigail
> {
>
>@@ -101,6 +112,36 @@ generate_from_strings(const std::vector<std::string>& strs)
>   return os.str();
> }
>
>+/// Compile a regex from a string.
>+///
>+/// The result is held in a shared pointer. This will be null if regex
>+/// compilation fails.
>+///
>+/// @param str the string representation of the regex.
>+///
>+/// @return shared pointer holder of a compiled regex object.
>+regex_t_sptr
>+compile(const std::string& str)
>+{
>+  regex_t_sptr r = sptr_utils::build_sptr(new regex_t);
>+  if (regcomp(r.get(), str.c_str(), REG_EXTENDED))
>+    r.reset();
>+  return r;
>+}
>+
>+/// See if a string matches a regex.
>+///
>+/// @param r a shared pointer holder of a compiled regex object.
>+///
>+/// @param str a string.
>+///
>+/// @return whether there was a match.
>+bool
>+match(const regex_t_sptr& r, const std::string& str)
>+{
>+  return !regexec(r.get(), str.c_str(), 0, NULL, 0);
>+}
>+
> }//end namespace regex
>
> }//end namespace abigail
>-- 
>2.26.2.303.gf8c07b1a785-goog
>
  

Patch

diff --git a/include/abg-regex.h b/include/abg-regex.h
index 59976794..c7951a8f 100644
--- a/include/abg-regex.h
+++ b/include/abg-regex.h
@@ -71,6 +71,12 @@  operator<<(std::ostream& os, const escape& esc);
 std::string
 generate_from_strings(const std::vector<std::string>& strs);
 
+regex_t_sptr
+compile(const std::string& str);
+
+bool
+match(const regex_t_sptr& r, const std::string& str);
+
 }// end namespace regex
 
 /// Specialization of sptr_utils::build_sptr for regex_t.
diff --git a/src/abg-regex.cc b/src/abg-regex.cc
index 90e4d144..7e5277e2 100644
--- a/src/abg-regex.cc
+++ b/src/abg-regex.cc
@@ -23,11 +23,22 @@ 
 /// Some specialization for shared pointer utility templates.
 ///
 
+#include "config.h"
+
 #include <sstream>
 #include <ostream>
+
+#include "abg-internal.h"
+
+// <headers defining libabigail's API go under here>
+ABG_BEGIN_EXPORT_DECLARATIONS
+
 #include "abg-sptr-utils.h"
 #include "abg-regex.h"
 
+ABG_END_EXPORT_DECLARATIONS
+// </headers defining libabigail's API>
+
 namespace abigail
 {
 
@@ -101,6 +112,36 @@  generate_from_strings(const std::vector<std::string>& strs)
   return os.str();
 }
 
+/// Compile a regex from a string.
+///
+/// The result is held in a shared pointer. This will be null if regex
+/// compilation fails.
+///
+/// @param str the string representation of the regex.
+///
+/// @return shared pointer holder of a compiled regex object.
+regex_t_sptr
+compile(const std::string& str)
+{
+  regex_t_sptr r = sptr_utils::build_sptr(new regex_t);
+  if (regcomp(r.get(), str.c_str(), REG_EXTENDED))
+    r.reset();
+  return r;
+}
+
+/// See if a string matches a regex.
+///
+/// @param r a shared pointer holder of a compiled regex object.
+///
+/// @param str a string.
+///
+/// @return whether there was a match.
+bool
+match(const regex_t_sptr& r, const std::string& str)
+{
+  return !regexec(r.get(), str.c_str(), 0, NULL, 0);
+}
+
 }//end namespace regex
 
 }//end namespace abigail