~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to src/regex.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* regex.cpp
 
2
 * Copyright (C) 2005-2008 Tommi Maekitalo
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 *
 
18
 */
 
19
 
 
20
 
 
21
#include "cxxtools/regex.h"
 
22
#include <stdexcept>
 
23
#include <locale>
 
24
#include <cctype>
 
25
 
 
26
namespace cxxtools
 
27
{
 
28
  unsigned RegexSMatch::size() const
 
29
  {
 
30
    unsigned n;
 
31
    for (n = 0; n < 10 && matchbuf[n].rm_so >= 0; ++n)
 
32
      ;
 
33
 
 
34
    return n;
 
35
  }
 
36
 
 
37
  std::string RegexSMatch::get(unsigned n) const
 
38
  {
 
39
    return str.substr(matchbuf[n].rm_so, matchbuf[n].rm_eo - matchbuf[n].rm_so);
 
40
  }
 
41
 
 
42
  std::string RegexSMatch::format(const std::string& s) const
 
43
  {
 
44
    enum state_type
 
45
    {
 
46
      state_0,
 
47
      state_esc,
 
48
      state_var0,
 
49
      state_var1,
 
50
      state_1
 
51
    } state;
 
52
 
 
53
    state = state_0;
 
54
    std::string ret;
 
55
 
 
56
    for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
 
57
    {
 
58
      char ch = *it;
 
59
 
 
60
      switch (state)
 
61
      {
 
62
        case state_0:
 
63
          if (ch == '$')
 
64
            state = state_var0;
 
65
          else if (ch == '\\')
 
66
            state = state_esc;
 
67
          break;
 
68
 
 
69
        case state_esc:
 
70
          ret += ch;
 
71
          state = state_1;
 
72
          break;
 
73
 
 
74
        case state_var0:
 
75
          if (std::isdigit(ch))
 
76
          {
 
77
            ret = std::string(s.begin(), it - 1);
 
78
            regoff_t s = matchbuf[ch - '0'].rm_so;
 
79
            regoff_t e = matchbuf[ch - '0'].rm_eo;
 
80
            if (s >= 0 && e >= 0)
 
81
              ret.append(str, s, e-s);
 
82
            state = state_1;
 
83
          }
 
84
          else
 
85
            state = state_0;
 
86
          break;
 
87
 
 
88
        case state_1:
 
89
          if (ch == '$')
 
90
            state = state_var1;
 
91
          else if (state == '\\')
 
92
            state = state_esc;
 
93
          else
 
94
            ret += ch;
 
95
          break;
 
96
 
 
97
        case state_var1:
 
98
          if (std::isdigit(ch))
 
99
          {
 
100
            unsigned s = matchbuf[ch - '0'].rm_so;
 
101
            unsigned e = matchbuf[ch - '0'].rm_eo;
 
102
            if (s >= 0 && e >= 0)
 
103
              ret.append(str, s, e-s);
 
104
            state = state_1;
 
105
          }
 
106
          else if (ch == '$')
 
107
            ret += '$';
 
108
          else
 
109
          {
 
110
            ret += '$';
 
111
            ret += ch;
 
112
          }
 
113
          break;
 
114
      }
 
115
    }
 
116
 
 
117
    switch (state)
 
118
    {
 
119
      case state_0:
 
120
      case state_var0:
 
121
        return s;
 
122
 
 
123
      case state_esc:
 
124
        return ret + '\\';
 
125
 
 
126
      case state_var1:
 
127
        return ret + '$';
 
128
 
 
129
      case state_1:
 
130
        return ret;
 
131
    }
 
132
 
 
133
    return ret;
 
134
  }
 
135
 
 
136
  void Regex::checkerr(int ret) const
 
137
  {
 
138
    if (ret != 0)
 
139
    {
 
140
      char errbuf[256];
 
141
      regerror(ret, expr.getPointer(), errbuf, sizeof(errbuf));
 
142
      throw std::runtime_error(errbuf);
 
143
    }
 
144
  }
 
145
 
 
146
  bool Regex::match(const std::string& str_, int eflags) const
 
147
  {
 
148
    RegexSMatch smatch;
 
149
    return match(str_, smatch, eflags);
 
150
  }
 
151
 
 
152
  bool Regex::match(const std::string& str_, RegexSMatch& smatch, int eflags) const
 
153
  {
 
154
    smatch.str = str_;
 
155
    int ret = regexec(expr.getPointer(), str_.c_str(),
 
156
        sizeof(smatch.matchbuf) / sizeof(regmatch_t), smatch.matchbuf, eflags);
 
157
 
 
158
    if (ret ==REG_NOMATCH)
 
159
      return false;
 
160
 
 
161
    checkerr(ret);
 
162
    return true;
 
163
  }
 
164
 
 
165
}