aboutsummaryrefslogtreecommitdiff
path: root/cutl/re/re.cxx
blob: 95a641160cd60560d543434315220dbd5caa121a (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
209
210
211
212
213
214
215
216
217
218
219
220
221
// file      : cutl/re/re.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <cutl/re.hxx>

#ifndef LIBCUTL_EXTERNAL_BOOST
#  include <cutl/details/boost/tr1/regex.hpp>
#else
#  include <boost/tr1/regex.hpp>
#endif

using namespace std;

namespace cutl
{
  namespace re
  {
    //
    // format_base
    //

    format_base::
    ~format_base () throw ()
    {
    }

    char const* format_base::
    what () const throw ()
    {
      return description_.c_str ();
    }

    //
    // basic_regex
    //
    template <typename C>
    struct basic_regex<C>::impl
    {
      typedef basic_string<C> string_type;
      typedef tr1::basic_regex<C> regex_type;
      typedef typename regex_type::flag_type flag_type;

      impl () {}
      impl (regex_type const& r): r (r) {}
      impl (string_type const& s, bool icase)
      {
        flag_type f (tr1::regex_constants::ECMAScript);

        if (icase)
          f |= tr1::regex_constants::icase;

        r.assign (s, f);
      }

      regex_type r;
    };

    template <>
    basic_regex<char>::
    ~basic_regex ()
    {
      delete impl_;
    }

    template <>
    basic_regex<wchar_t>::
    ~basic_regex ()
    {
      delete impl_;
    }

    template <>
    basic_regex<char>::
    basic_regex (basic_regex const& r)
        : str_ (r.str_), impl_ (new impl (r.impl_->r))
    {
    }

    template <>
    basic_regex<wchar_t>::
    basic_regex (basic_regex const& r)
        : str_ (r.str_), impl_ (new impl (r.impl_->r))
    {
    }

    template <>
    basic_regex<char>& basic_regex<char>::
    operator= (basic_regex const& r)
    {
      string_type tmp (r.str_);
      impl_->r = r.impl_->r;
      str_.swap (tmp);
      return *this;
    }

    template <>
    basic_regex<wchar_t>& basic_regex<wchar_t>::
    operator= (basic_regex const& r)
    {
      string_type tmp (r.str_);
      impl_->r = r.impl_->r;
      str_.swap (tmp);
      return *this;
    }

    template <>
    void basic_regex<char>::
    init (string_type const* s, bool icase)
    {
      string_type tmp (s == 0 ? string_type () : *s);

      try
      {
        if (impl_ == 0)
          impl_ = s == 0 ? new impl : new impl (*s, icase);
        else
        {
          impl::flag_type f (tr1::regex_constants::ECMAScript);

          if (icase)
            f |= tr1::regex_constants::icase;

          impl_->r.assign (*s, f);
        }
      }
      catch (tr1::regex_error const& e)
      {
        throw basic_format<char> (s == 0 ? "" : *s, e.what ());
      }

      str_.swap (tmp);
    }

    template <>
    void basic_regex<wchar_t>::
    init (string_type const* s, bool icase)
    {
      string_type tmp (s == 0 ? string_type () : *s);

      try
      {
        if (impl_ == 0)
          impl_ = s == 0 ? new impl : new impl (*s, icase);
        else
        {
          impl::flag_type f (tr1::regex_constants::ECMAScript);

          if (icase)
            f |= tr1::regex_constants::icase;

          impl_->r.assign (*s, f);
        }
      }
      catch (tr1::regex_error const& e)
      {
        throw basic_format<wchar_t> (s == 0 ? L"" : *s, e.what ());
      }

      str_.swap (tmp);
    }

    template <>
    bool basic_regex<char>::
    match (string_type const& s) const
    {
      return tr1::regex_match (s, impl_->r);
    }

    template <>
    bool basic_regex<wchar_t>::
    match (string_type const& s) const
    {
      return tr1::regex_match (s, impl_->r);
    }

    template <>
    bool basic_regex<char>::
    search (string_type const& s) const
    {
      return tr1::regex_search (s, impl_->r);
    }

    template <>
    bool basic_regex<wchar_t>::
    search (string_type const& s) const
    {
      return tr1::regex_search (s, impl_->r);
    }

    template <>
    string basic_regex<char>::
    replace (string_type const& s,
             string_type const& sub,
             bool first_only) const
    {
      tr1::regex_constants::match_flag_type f (
        tr1::regex_constants::format_default);

      if (first_only)
        f |= tr1::regex_constants::format_first_only;

      return tr1::regex_replace (s, impl_->r, sub, f);
    }

    template <>
    wstring basic_regex<wchar_t>::
    replace (string_type const& s,
             string_type const& sub,
             bool first_only) const
    {
      tr1::regex_constants::match_flag_type f (
        tr1::regex_constants::format_default);

      if (first_only)
        f |= tr1::regex_constants::format_first_only;

      return tr1::regex_replace (s, impl_->r, sub, f);
    }
  }
}