adding regex wrapper implementation
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Aug 2007 14:08:24 +0000 (14:08 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Aug 2007 14:08:24 +0000 (14:08 +0000)
zypp/base/Regex.cc [new file with mode: 0644]
zypp/base/Regex.h [new file with mode: 0644]

diff --git a/zypp/base/Regex.cc b/zypp/base/Regex.cc
new file mode 100644 (file)
index 0000000..03f0f79
--- /dev/null
@@ -0,0 +1,78 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/Regex.cc
+ *
+*/
+#include <cstdio>
+#include <cstdarg>
+
+#include <iostream>
+
+#include "zypp/base/Regex.h"
+
+using namespace zypp::str;
+
+regex::regex(const std::string& str, int flags)
+{
+    m_valid = true;
+    if (regcomp(&m_preg, str.c_str(), REG_EXTENDED))
+        m_valid = false;
+}
+
+regex::~regex() throw()
+{
+    if (m_valid)
+        regfree(&m_preg);
+}
+
+bool zypp::str::regex_match(const std::string& s, smatch& matches, const regex& regex)
+{
+    bool r = regex.m_valid && !regexec(&regex.m_preg, s.c_str(), 12, &matches.pmatch[0], 0);
+    if (r)
+        matches.match_str = s;
+    return r;
+}
+
+bool zypp::str::regex_match(const std::string& s,  const regex& regex)
+{
+    return !regexec(&regex.m_preg, s.c_str(), 0, NULL, 0);
+}
+
+bool zypp::str::regex_search(const std::string& s, smatch& matches, const regex& regex)
+{
+    bool r= regex.m_valid && !regexec(&regex.m_preg, s.c_str(), 12, &matches.pmatch[0], 0);
+    if (r)
+        matches.match_str = s;
+    return r;
+}
+
+smatch::smatch()
+{
+    memset(&pmatch, -1, sizeof(pmatch));
+}
+
+std::string smatch::operator[](unsigned i) const 
+{
+    if (i < 12 && pmatch[i].rm_so != -1)
+        return match_str.substr(pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so);
+    return std::string();
+}
+
+
+unsigned smatch::size() const
+{
+    unsigned matches = 0;
+    while (matches < 12 && pmatch[matches+1].rm_so != -1) {
+    //    std::cout << "match[" << matches << "]: *" << (*this)[matches
+    //        +1] << "*" << std::endl;
+        matches++;
+    }
+
+    return matches;
+}
diff --git a/zypp/base/Regex.h b/zypp/base/Regex.h
new file mode 100644 (file)
index 0000000..650d5f8
--- /dev/null
@@ -0,0 +1,73 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zypp/base/Regex.h
+ *
+*/
+#ifndef ZYPP_BASE_REGEX_H
+#define ZYPP_BASE_REGEX_H
+
+#include <iosfwd>
+#include <string>
+
+#include <regex.h>
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  /** String related utilities and \ref ZYPP_STR_REGEX.
+   \see \ref ZYPP_STR_REGEX
+  */
+  namespace str
+  { /////////////////////////////////////////////////////////////////
+
+    class regex {
+    public:
+
+        enum RegFlags {
+            optimize = 0,
+            match_extra = 0,
+            icase = REG_ICASE,
+            nosubs = REG_NOSUB,
+            match_extended = REG_EXTENDED
+        };
+
+        regex(const std::string& s,int flags = match_extended);
+        ~regex() throw();
+
+
+    public:
+        regex_t m_preg;
+        bool m_valid;
+    };
+
+
+    class smatch {
+    public:
+        smatch();
+
+        std::string operator[](unsigned i) const;
+
+        unsigned size() const;
+
+        std::string match_str;
+        regmatch_t pmatch[12];
+    };
+
+    bool regex_match(const std::string& s, str::smatch& matches, const regex& regex);
+    bool regex_match(const std::string& s,  const regex& regex);
+    bool regex_search(const std::string& s, str::smatch& matches, const regex& regex);
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace str
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_STRING_H