~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to rak/regex.h

  • Committer: Bazaar Package Importer
  • Author(s): Qingning Huo
  • Date: 2006-01-12 20:47:33 UTC
  • mto: (4.1.1 edgy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060112204733-2vw5t3vdne40s8mq
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// rak - Rakshasa's toolbox
 
2
// Copyright (C) 2005, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (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
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
// This is a hacked up whole string pattern matching. Replace with
 
38
// TR1's regex when that becomes widely available. It is intended for
 
39
// small strings.
 
40
 
 
41
#ifndef RAK_REGEX_H
 
42
#define RAK_REGEX_H
 
43
 
 
44
#include <sys/types.h>
 
45
 
 
46
#include <functional>
 
47
#include <string>
 
48
#include <list>
 
49
 
 
50
namespace rak {
 
51
 
 
52
class regex : public std::unary_function<std::string, bool> {
 
53
public:
 
54
  regex() {}
 
55
  regex(const std::string& p) : m_pattern(p) {}
 
56
 
 
57
  const std::string& pattern() const { return m_pattern; }
 
58
 
 
59
  bool operator () (const std::string& p) const;
 
60
 
 
61
private:
 
62
  std::string m_pattern;
 
63
};
 
64
 
 
65
// This isn't optimized, or very clean. A simple hack that should work.
 
66
bool
 
67
regex::operator () (const std::string& text) const {
 
68
  if (m_pattern.empty() ||
 
69
      text.empty() ||
 
70
      (m_pattern[0] != '*' && m_pattern[0] != text[0]))
 
71
    return false;
 
72
 
 
73
  // Replace with unordered_vector?
 
74
  std::list<unsigned int> paths;
 
75
  paths.push_front(0);
 
76
 
 
77
  for (std::string::const_iterator itrText = ++text.begin(), lastText = text.end(); itrText != lastText; ++itrText) {
 
78
    
 
79
    for (std::list<unsigned int>::iterator itrPaths = paths.begin(), lastPaths = paths.end(); itrPaths != lastPaths; ) {
 
80
 
 
81
      unsigned int next = *itrPaths + 1;
 
82
 
 
83
      if (m_pattern[*itrPaths] != '*')
 
84
        itrPaths = paths.erase(itrPaths);
 
85
      else
 
86
        itrPaths++;
 
87
 
 
88
      // When we reach the end of 'm_pattern', we don't have a whole
 
89
      // match of 'text'.
 
90
      if (next == m_pattern.size())
 
91
        continue;
 
92
 
 
93
      // Push to the back so that '*' will match zero length strings.
 
94
      if (m_pattern[next] == '*')
 
95
        paths.push_back(next);
 
96
 
 
97
      if (m_pattern[next] == *itrText)
 
98
        paths.push_front(next);
 
99
    }
 
100
 
 
101
    if (paths.empty())
 
102
      return false;
 
103
  }
 
104
 
 
105
  return std::find(paths.begin(), paths.end(), m_pattern.size() - 1) != paths.end();
 
106
}
 
107
 
 
108
}
 
109
 
 
110
#endif
 
111
// rak - Rakshasa's toolbox
 
112
// Copyright (C) 2005, Jari Sundell
 
113
//
 
114
// This program is free software; you can redistribute it and/or modify
 
115
// it under the terms of the GNU General Public License as published by
 
116
// the Free Software Foundation; either version 2 of the License, or
 
117
// (at your option) any later version.
 
118
// 
 
119
// This program is distributed in the hope that it will be useful,
 
120
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
121
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
122
// GNU General Public License for more details.
 
123
// 
 
124
// You should have received a copy of the GNU General Public License
 
125
// along with this program; if not, write to the Free Software
 
126
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
127
//
 
128
// In addition, as a special exception, the copyright holders give
 
129
// permission to link the code of portions of this program with the
 
130
// OpenSSL library under certain conditions as described in each
 
131
// individual source file, and distribute linked combinations
 
132
// including the two.
 
133
//
 
134
// You must obey the GNU General Public License in all respects for
 
135
// all of the code used other than OpenSSL.  If you modify file(s)
 
136
// with this exception, you may extend this exception to your version
 
137
// of the file(s), but you are not obligated to do so.  If you do not
 
138
// wish to do so, delete this exception statement from your version.
 
139
// If you delete this exception statement from all source files in the
 
140
// program, then also delete it here.
 
141
//
 
142
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
143
//
 
144
//           Skomakerveien 33
 
145
//           3185 Skoppum, NORWAY
 
146
 
 
147
// This is a hacked up whole string pattern matching. Replace with
 
148
// TR1's regex when that becomes widely available. It is intended for
 
149
// small strings.
 
150
 
 
151
#ifndef RAK_REGEX_H
 
152
#define RAK_REGEX_H
 
153
 
 
154
#include <sys/types.h>
 
155
 
 
156
#include <functional>
 
157
#include <string>
 
158
#include <list>
 
159
 
 
160
namespace rak {
 
161
 
 
162
class regex : public std::unary_function<std::string, bool> {
 
163
public:
 
164
  regex() {}
 
165
  regex(const std::string& p) : m_pattern(p) {}
 
166
 
 
167
  const std::string& pattern() const { return m_pattern; }
 
168
 
 
169
  bool operator () (const std::string& p) const;
 
170
 
 
171
private:
 
172
  std::string m_pattern;
 
173
};
 
174
 
 
175
// This isn't optimized, or very clean. A simple hack that should work.
 
176
bool
 
177
regex::operator () (const std::string& text) const {
 
178
  if (m_pattern.empty() ||
 
179
      text.empty() ||
 
180
      (m_pattern[0] != '*' && m_pattern[0] != text[0]))
 
181
    return false;
 
182
 
 
183
  // Replace with unordered_vector?
 
184
  std::list<unsigned int> paths;
 
185
  paths.push_front(0);
 
186
 
 
187
  for (std::string::const_iterator itrText = ++text.begin(), lastText = text.end(); itrText != lastText; ++itrText) {
 
188
    
 
189
    for (std::list<unsigned int>::iterator itrPaths = paths.begin(), lastPaths = paths.end(); itrPaths != lastPaths; ) {
 
190
 
 
191
      unsigned int next = *itrPaths + 1;
 
192
 
 
193
      if (m_pattern[*itrPaths] != '*')
 
194
        itrPaths = paths.erase(itrPaths);
 
195
      else
 
196
        itrPaths++;
 
197
 
 
198
      // When we reach the end of 'm_pattern', we don't have a whole
 
199
      // match of 'text'.
 
200
      if (next == m_pattern.size())
 
201
        continue;
 
202
 
 
203
      // Push to the back so that '*' will match zero length strings.
 
204
      if (m_pattern[next] == '*')
 
205
        paths.push_back(next);
 
206
 
 
207
      if (m_pattern[next] == *itrText)
 
208
        paths.push_front(next);
 
209
    }
 
210
 
 
211
    if (paths.empty())
 
212
      return false;
 
213
  }
 
214
 
 
215
  return std::find(paths.begin(), paths.end(), m_pattern.size() - 1) != paths.end();
 
216
}
 
217
 
 
218
}
 
219
 
 
220
#endif