summaryrefslogtreecommitdiff
path: root/libbackend-elements/backend-elements/regex.hxx
blob: 9dc60240e8644a389d6b1f7c409e2573fb2c0029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// file      : backend-elements/regex.hxx
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2005-2010 Boris Kolpackov
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef BACKEND_ELEMENTS_REGEX_HXX
#define BACKEND_ELEMENTS_REGEX_HXX

#include <ostream>

#include <boost/regex.hpp>

#include <backend-elements/types.hxx>

namespace BackendElements
{
  namespace Regex
  {
    template <typename C>
    struct Format
    {
      Format (StringTemplate<C> const& expression,
              StringTemplate<C> const& description)
          : expression_ (expression), description_ (description)
      {
      }

      StringTemplate<C> const&
      expression () const
      {
        return expression_;
      }

      StringTemplate<C> const&
      description () const
      {
        return description_;
      }

    private:
      StringTemplate<C> expression_;
      StringTemplate<C> description_;
    };

    // Regex pattern.
    //
    template <typename C>
    struct Pattern
    {
      Pattern ()
      {
      }

      Pattern (Char const* p)
      {
        init (StringTemplate<C> (p));
      }

      Pattern (StringTemplate<C> const& p)
      {
        init (p);
      }

      Pattern&
      operator= (Char const* p)
      {
        init (StringTemplate<C> (p));
        return *this;
      }

      Pattern&
      operator= (StringTemplate<C> const& p)
      {
        init (p);
        return *this;
      }

    public:
      Boolean
      match (StringTemplate<C> const& s) const
      {
        return regex_match (s, pat_, boost::format_all);
      }

      StringTemplate<C>
      merge (StringTemplate<C> const& sub,
             StringTemplate<C> const& s,
             Boolean first_only = false) const
      {
        if (first_only)
          return regex_merge (
            s, pat_, sub, boost::format_all | boost::format_first_only);
        else
          return regex_merge ( s, pat_, sub, boost::format_all);
      }

    public:
      Boolean
      empty () const
      {
        return pat_.empty ();
      }

    public:
      boost::basic_regex<C> const&
      impl_pattern () const
      {
        return pat_;
      }

    private:
      Void
      init (StringTemplate<C> const& r);

    private:
      boost::basic_regex<C> pat_;
    };

    template <typename C1, typename C2>
    inline std::basic_ostream<C1>&
    operator<< (std::basic_ostream<C1>& os, Pattern<C2> const& p)
    {
      return os << p.impl_pattern ().str ().c_str ();
    }

    // Regex expression: '/pattern/substitution/'.
    //
    template <typename C>
    struct Expression
    {
      Expression ()
      {
      }

      // Expression is of the form /regex/format/ where '/' can be
      // replaced with any delimiter.
      //
      Expression (Char const* e)
      {
        init (StringTemplate<C> (e));
      }

      Expression (StringTemplate<C> const& e)
      {
        init (e);
      }

      Expression&
      operator= (Char const* e)
      {
        init (StringTemplate<C> (e));
        return *this;
      }

      Expression&
      operator= (StringTemplate<C> const& e)
      {
        init (e);
        return *this;
      }

    public:
      Boolean
      match (StringTemplate<C> const& s) const
      {
        return pat_.match (s);
      }

      StringTemplate<C>
      merge (StringTemplate<C> const& s, Boolean first_only = false) const
      {
        return pat_.merge (sub_, s, first_only);
      }

    public:
      const Pattern<C>&
      pattern () const
      {
        return pat_;
      }

      const StringTemplate<C>&
      substitution () const
      {
        return sub_;
      }

    public:
      Boolean
      empty () const
      {
        return pat_.empty () && sub_.empty ();
      }

    private:
      Void
      init (StringTemplate<C> const& r);

    private:
      Pattern<C> pat_;
      StringTemplate<C> sub_;
    };
  }
}

#include <backend-elements/regex.txx>

#endif  // BACKEND_ELEMENTS_REGEX_HXX