a6fe0f6e254e2426da458a8b27ea090809d814cf
[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      * regex
36      * regex_match
37      * smatch
38      */
39
40     typedef Exception regex_error;
41
42     class smatch;
43     class regex;
44
45     bool regex_match(const char * s, str::smatch& matches, const regex& regex);
46     inline bool regex_match(const std::string& s, str::smatch& matches, const regex& regex)
47     { return regex_match( s.c_str(), matches, regex ); }
48
49     bool regex_match(const char * s, const regex& regex);
50     inline bool regex_match(const std::string& s, const regex& regex)
51     { return regex_match( s.c_str(), regex ); }
52
53     class regex {
54     public:
55
56       enum RegFlags {
57         optimize = 0,
58         match_extra = 0,
59         icase = REG_ICASE,
60         nosubs = REG_NOSUB,
61         match_extended = REG_EXTENDED,
62         normal = 1<<16
63       };
64
65       regex();
66       regex(const std::string& s,int flags = match_extended);
67       ~regex() throw();
68
69       regex(const regex & rhs)
70       { assign(rhs.m_str, rhs.m_flags); }
71
72       regex & operator=(const regex & rhs)
73       { assign(rhs.m_str, rhs.m_flags); return *this; }
74
75       /**
76        * string representation of the regular expression
77        */
78       std::string asString() const
79       { return m_str; }
80
81     public:
82       /** Expert backdoor. Returns pointer to the compiled regex for direct use in regexec() */
83       regex_t * get()
84       { return & m_preg; }
85
86     private:
87       void assign(const std::string& s,int flags = match_extended);
88
89     private:
90       friend class smatch;
91       friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
92       friend bool regex_match(const char * s,  const regex& regex);
93       std::string m_str;
94       int m_flags;
95       regex_t m_preg;
96       bool m_valid;
97     };
98
99     class smatch {
100     public:
101       smatch();
102
103       std::string operator[](unsigned i) const;
104
105       unsigned size() const;
106
107       std::string match_str;
108       regmatch_t pmatch[12];
109     };
110
111
112
113     /////////////////////////////////////////////////////////////////
114   } // namespace str
115   ///////////////////////////////////////////////////////////////////
116   /////////////////////////////////////////////////////////////////
117 } // namespace zypp
118 ///////////////////////////////////////////////////////////////////
119 #endif // ZYPP_BASE_STRING_H