Cleanup and remove deprecated interface methods
[platform/upstream/libzypp.git] / zypp / base / Regex.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Regex.h
10  *
11 */
12 #ifndef ZYPP_BASE_REGEX_H
13 #define ZYPP_BASE_REGEX_H
14
15 #include <string>
16 #include <regex.h>
17
18 #include "zypp/base/Exception.h"
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23   ///////////////////////////////////////////////////////////////////
24   /** String related utilities and \ref ZYPP_STR_REGEX.
25    \see \ref ZYPP_STR_REGEX
26   */
27   namespace str
28   { /////////////////////////////////////////////////////////////////
29
30     ///////////////////////////////////////////////////////////////////
31     /** \defgroup ZYPP_STR_REGEX Regular expressions
32      *
33      * Namespace zypp::str regular expressions \b using the glibc regex library.
34      *
35      * \see \ref sat::AttrMatcher string matcher supporing regex, globing, etc.
36      *
37      * regex
38      * regex_match
39      * smatch
40      */
41
42     typedef Exception regex_error;
43
44     class smatch;
45     class regex;
46
47     bool regex_match(const char * s, str::smatch& matches, const regex& regex);
48     inline bool regex_match(const std::string& s, str::smatch& matches, const regex& regex)
49     { return regex_match( s.c_str(), matches, regex ); }
50
51     bool regex_match(const char * s, const regex& regex);
52     inline bool regex_match(const std::string& s, const regex& regex)
53     { return regex_match( s.c_str(), regex ); }
54
55     /**
56      * \see \ref sat::AttrMatcher string matcher supporing regex, globing, etc.
57      * \ingroup ZYPP_STR_REGEX
58      */
59     class regex {
60     public:
61
62       enum RegFlags {
63         optimize = 0,
64         match_extra = 0,
65         icase = REG_ICASE,
66         nosubs = REG_NOSUB,
67         match_extended = REG_EXTENDED,
68         normal = 1<<16
69       };
70
71       regex();
72       regex(const std::string& s,int flags = match_extended);
73       ~regex() throw();
74
75       regex(const regex & rhs)
76       { assign(rhs.m_str, rhs.m_flags); }
77
78       regex & operator=(const regex & rhs)
79       { assign(rhs.m_str, rhs.m_flags); return *this; }
80
81       /**
82        * string representation of the regular expression
83        */
84       std::string asString() const
85       { return m_str; }
86
87     public:
88       /** Expert backdoor. Returns pointer to the compiled regex for direct use in regexec() */
89       regex_t * get()
90       { return & m_preg; }
91
92     private:
93       void assign(const std::string& s,int flags = match_extended);
94
95     private:
96       friend class smatch;
97       friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
98       friend bool regex_match(const char * s,  const regex& regex);
99       std::string m_str;
100       int m_flags;
101       regex_t m_preg;
102       bool m_valid;
103     };
104
105     /**
106      * \ingroup ZYPP_STR_REGEX
107      * \see regex
108      */
109     class smatch {
110     public:
111       smatch();
112
113       std::string operator[](unsigned i) const;
114
115       unsigned size() const;
116
117       std::string match_str;
118       regmatch_t pmatch[12];
119     };
120
121
122
123     /////////////////////////////////////////////////////////////////
124   } // namespace str
125   ///////////////////////////////////////////////////////////////////
126   /////////////////////////////////////////////////////////////////
127 } // namespace zypp
128 ///////////////////////////////////////////////////////////////////
129 #endif // ZYPP_BASE_STRING_H