diff --git a/include/abg-regex.h b/include/abg-regex.h index 164083cc..7ab9fc08 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& strs); +regex_t_sptr +compile(const std::string& str); + +bool +match(const regex_t_sptr& r, const std::string& str); + }// end namespace regex namespace sptr_utils diff --git a/src/abg-regex.cc b/src/abg-regex.cc index 59f56deb..5bcd130b 100644 --- a/src/abg-regex.cc +++ b/src/abg-regex.cc @@ -23,12 +23,22 @@ /// Some specialization for shared pointer utility templates. /// +#include "config.h" + #include #include +#include "abg-internal.h" + +// +ABG_BEGIN_EXPORT_DECLARATIONS + #include "abg-regex.h" #include "abg-sptr-utils.h" +ABG_END_EXPORT_DECLARATIONS +// + namespace abigail { @@ -104,6 +114,36 @@ generate_from_strings(const std::vector& 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