1eca7ffdbf45d567281bced0cdab377d5bec0257
[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 <iosfwd>
16 #include <string>
17 #include <regex.h>
18
19 #include "zypp/base/Exception.h"
20
21 //////////////////////////////////////////////////////////////////
22 namespace zypp
23 {
24   //////////////////////////////////////////////////////////////////
25   /// \namespace str
26   /// \brief String related utilities and \ref ZYPP_STR_REGEX.
27   namespace str
28   {
29     //////////////////////////////////////////////////////////////////
30     /// \defgroup ZYPP_STR_REGEX Regular expression matching
31     /// \brief Regular expressions using the glibc regex library.
32     ///
33     /// \see also \ref StrMatcher string matcher also supporting globing, etc.
34     ///
35     /// \code
36     ///  str::regex rxexpr( "^(A)?([0-9]*) im" );
37     ///  str::smatch what;
38     ///
39     ///  std::string mytext( "Y123 imXXXX" );
40     ///  if ( str::regex_match( mytext, what, rxexpr ) )
41     ///  {
42     ///    MIL << "MATCH '" << what[0] << "'" << endl;
43     ///    MIL << " subs: " << what.size()-1 << endl;
44     ///    for_( i, 1U, what.size() )
45     ///      MIL << "      [" << i << "] " << what[i] << endl;
46     ///  }
47     ///  else
48     ///  {
49     ///    WAR << "NO MATCH '" << rxexpr << "' in '" <<  mytext << endl;
50     ///  }
51     /// \endcode
52     //////////////////////////////////////////////////////////////////
53
54     typedef Exception regex_error;
55
56     class smatch;
57     class regex;
58
59     //////////////////////////////////////////////////////////////////
60     /// \brief Regular expression matching
61     ///
62     /// \ingroup ZYPP_STR_REGEX
63     /// \relates regex
64     /// Return whether a \ref regex matches a specific string. An optionally
65     /// passed \ref smatch object will contain the match reults.
66     //////////////////////////////////////////////////////////////////
67     bool regex_match( const char * s, smatch & matches, const regex & regex );
68
69     /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
70     inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
71     { return regex_match( s.c_str(), matches, regex ); }
72
73     /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
74     bool regex_match( const char * s, const regex & regex );
75
76     /** \copydoc regex_match \relates regex \ingroup ZYPP_STR_REGEX */
77     inline bool regex_match( const std::string & s, const regex & regex )
78     { return regex_match( s.c_str(), regex ); }
79
80     //////////////////////////////////////////////////////////////////
81     /// \class regex
82     /// \brief Regular expression
83     ///
84     /// \ingroup ZYPP_STR_REGEX
85     //////////////////////////////////////////////////////////////////
86     class regex
87     {
88     public:
89
90       enum RegFlags {
91         optimize        = 0,            ///< \deprecated legacy, obsolete
92         match_extra     = 0,            ///< \deprecated legacy, obsolete
93         icase           = REG_ICASE,    ///< Do not differentiate case
94         nosubs          = REG_NOSUB,    ///< Support for substring addressing of matches is not required
95         match_extended  = REG_EXTENDED, ///< Use POSIX Extended Regular Expression syntax when interpreting regex.
96         normal          = 1<<16         ///< \deprecated legacy, use match_extended
97       };
98
99       regex();
100       regex(const std::string& s,int flags = match_extended);
101       ~regex() throw();
102
103       regex(const regex & rhs)
104       { assign(rhs.m_str, rhs.m_flags); }
105
106       regex & operator=(const regex & rhs)
107       { assign(rhs.m_str, rhs.m_flags); return *this; }
108
109       /**
110        * string representation of the regular expression
111        */
112       std::string asString() const
113       { return m_str; }
114
115     public:
116       /** Expert backdoor. Returns pointer to the compiled regex for direct use in regexec() */
117       regex_t * get()
118       { return & m_preg; }
119
120     private:
121       void assign(const std::string& s,int flags = match_extended);
122
123     private:
124       friend class smatch;
125       friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
126       friend bool regex_match(const char * s,  const regex& regex);
127       std::string m_str;
128       int m_flags;
129       regex_t m_preg;
130       bool m_valid;
131     };
132
133     /** \relates regex Stream output */
134     inline std::ostream & operator<<( std::ostream & str, const regex & obj )
135     { return str << obj.asString(); }
136
137     //////////////////////////////////////////////////////////////////
138     /// \class smatch
139     /// \brief Regular expression match result
140     ///
141     /// \ingroup ZYPP_STR_REGEX
142     ///
143     /// Index \c n=0 returns the string object representing the character
144     /// sequence that matched the whole regular expression.
145     /// If \c n is out of range, or if \c n is an unmatched sub-expression,
146     /// then an empty string is returned.
147     //////////////////////////////////////////////////////////////////
148     class smatch
149     {
150     public:
151       smatch();
152
153       std::string operator[](unsigned i) const;
154
155       unsigned size() const;
156
157       std::string match_str;
158       regmatch_t pmatch[12];
159     };
160
161   } // namespace str
162   //////////////////////////////////////////////////////////////////
163 } // namespace zypp
164 //////////////////////////////////////////////////////////////////
165 #endif // ZYPP_BASE_STRING_H