aboutsummaryrefslogtreecommitdiff
path: root/cutl/re/re.cxx
blob: 9cd2f2e102f21ad272d35ede375ff0de3583da06 (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
// file      : cutl/re/re.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <ostream>

#include <cutl/re.hxx>

#include <cutl/details/boost/tr1/regex.hpp>

using namespace std;

namespace cutl
{
  namespace re
  {
    //
    // format
    //

    format::
    ~format () throw ()
    {
    }

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

    //
    // regex
    //
    struct regex::impl
    {
      impl () {}
      impl (string const& s): r (s, tr1::regex_constants::ECMAScript) {}
      impl (tr1::regex const& r): r (r) {}

      tr1::regex r;
    };

    regex::
    ~regex ()
    {
      delete impl_;
    }

    regex::
    regex (regex const& r)
        : impl_ (new impl (r.impl_->r))
    {
    }

    regex& regex::
    operator= (regex const& r)
    {
      impl_->r = r.impl_->r;
      return *this;
    }

    void regex::
    init (string const* s)
    {
      try
      {
        if (impl_ == 0)
          impl_ = s == 0 ? new impl : new impl (*s);
        else
          impl_->r = *s;
      }
      catch (tr1::regex_error const& e)
      {
        throw format (s == 0 ? "" : *s, e.what ());
      }
    }

    bool regex::
    match (string const& s) const
    {
      return tr1::regex_match (s, impl_->r);
    }

    bool regex::
    search (string const& s) const
    {
      return tr1::regex_search (s, impl_->r);
    }

    string regex::
    replace (string const& s, string 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 regex_replace (s, impl_->r, sub, f);
    }

    string regex::
    str () const
    {
      return impl_->r.str ();
    }

    bool regex::
    empty () const
    {
      return impl_->r.empty ();
    }

    ostream&
    operator<< (ostream& os, regex const& r)
    {
      return os << r.str ().c_str ();
    }

    //
    // regexsub
    //
    void regexsub::
    init (string const& s)
    {
      string r;
      string::size_type p (parse (s, 0, r));
      regex_ = r;
      p = parse (s, p, sub_);
      if (p + 1 < s.size ())
        throw format (s, "junk after third delimiter");
    }

    //
    // parse()
    //
    string::size_type
    parse (string const& s, string::size_type p, string& r)
    {
      r.clear ();
      string::size_type n (s.size ());

      if (p >= n)
        throw format (s, "empty expression");

      char d (s[p++]);

      for (; p < n; ++p)
      {
        if (s[p] == d)
          break;

        if (s[p] == '\\')
        {
          if (++p < n)
          {
            if (s[p] != d && s[p] != '\\')
              r += '\\';
            r += s[p];
          }
          // else {we ran out of stuff before finding the delimiter}
        }
        else
          r += s[p];
      }

      if (p == n)
        throw format (s, "missing closing delimiter");

      return p;
    }
  }
}