regeximpl.h
Go to the documentation of this file.
1 #ifndef REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
2 #define REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
3 
4 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5 #pragma once
6 #endif
7 
8 
9 #include "stream.h"
10 #include "stringsource.h"
11 #include "streamcharsource.h"
12 
13 namespace YAML_PM
14 {
15  // query matches
16  inline bool RegEx::Matches(char ch) const {
17  std::string str;
18  str += ch;
19  return Matches(str);
20  }
21 
22  inline bool RegEx::Matches(const std::string& str) const {
23  return Match(str) >= 0;
24  }
25 
26  inline bool RegEx::Matches(const Stream& in) const {
27  return Match(in) >= 0;
28  }
29 
30  template <typename Source>
31  inline bool RegEx::Matches(const Source& source) const {
32  return Match(source) >= 0;
33  }
34 
35  // Match
36  // . Matches the given string against this regular expression.
37  // . Returns the number of characters matched.
38  // . Returns -1 if no characters were matched (the reason for
39  // not returning zero is that we may have an empty regex
40  // which is ALWAYS successful at matching zero characters).
41  // . REMEMBER that we only match from the start of the buffer!
42  inline int RegEx::Match(const std::string& str) const
43  {
44  StringCharSource source(str.c_str(), str.size());
45  return Match(source);
46  }
47 
48  inline int RegEx::Match(const Stream& in) const
49  {
50  StreamCharSource source(in);
51  return Match(source);
52  }
53 
54  template <typename Source>
55  inline bool RegEx::IsValidSource(const Source& source) const
56  {
57  return source;
58  }
59 
60  template<>
61  inline bool RegEx::IsValidSource<StringCharSource>(const StringCharSource&source) const
62  {
63  switch(m_op) {
64  case REGEX_MATCH:
65  case REGEX_RANGE:
66  return source;
67  default:
68  return true;
69  }
70  }
71 
72  template <typename Source>
73  inline int RegEx::Match(const Source& source) const
74  {
75  return IsValidSource(source) ? MatchUnchecked(source) : -1;
76  }
77 
78  template <typename Source>
79  inline int RegEx::MatchUnchecked(const Source& source) const
80  {
81  switch(m_op) {
82  case REGEX_EMPTY:
83  return MatchOpEmpty(source);
84  case REGEX_MATCH:
85  return MatchOpMatch(source);
86  case REGEX_RANGE:
87  return MatchOpRange(source);
88  case REGEX_OR:
89  return MatchOpOr(source);
90  case REGEX_AND:
91  return MatchOpAnd(source);
92  case REGEX_NOT:
93  return MatchOpNot(source);
94  case REGEX_SEQ:
95  return MatchOpSeq(source);
96  }
97 
98  return -1;
99  }
100 
102  // Operators
103  // Note: the convention MatchOp*<Source> is that we can assume IsSourceValid(source).
104  // So we do all our checks *before* we call these functions
105 
106  // EmptyOperator
107  template <typename Source>
108  inline int RegEx::MatchOpEmpty(const Source& source) const {
109  return source[0] == Stream::eof() ? 0 : -1;
110  }
111 
112  template <>
113  inline int RegEx::MatchOpEmpty<StringCharSource>(const StringCharSource& source) const {
114  return !source ? 0 : -1; // the empty regex only is successful on the empty string
115  }
116 
117  // MatchOperator
118  template <typename Source>
119  inline int RegEx::MatchOpMatch(const Source& source) const {
120  if(source[0] != m_a)
121  return -1;
122  return 1;
123  }
124 
125  // RangeOperator
126  template <typename Source>
127  inline int RegEx::MatchOpRange(const Source& source) const {
128  if(m_a > source[0] || m_z < source[0])
129  return -1;
130  return 1;
131  }
132 
133  // OrOperator
134  template <typename Source>
135  inline int RegEx::MatchOpOr(const Source& source) const {
136  for(std::size_t i=0;i<m_params.size();i++) {
137  int n = m_params[i].MatchUnchecked(source);
138  if(n >= 0)
139  return n;
140  }
141  return -1;
142  }
143 
144  // AndOperator
145  // Note: 'AND' is a little funny, since we may be required to match things
146  // of different lengths. If we find a match, we return the length of
147  // the FIRST entry on the list.
148  template <typename Source>
149  inline int RegEx::MatchOpAnd(const Source& source) const {
150  int first = -1;
151  for(std::size_t i=0;i<m_params.size();i++) {
152  int n = m_params[i].MatchUnchecked(source);
153  if(n == -1)
154  return -1;
155  if(i == 0)
156  first = n;
157  }
158  return first;
159  }
160 
161  // NotOperator
162  template <typename Source>
163  inline int RegEx::MatchOpNot(const Source& source) const {
164  if(m_params.empty())
165  return -1;
166  if(m_params[0].MatchUnchecked(source) >= 0)
167  return -1;
168  return 1;
169  }
170 
171  // SeqOperator
172  template <typename Source>
173  inline int RegEx::MatchOpSeq(const Source& source) const {
174  int offset = 0;
175  for(std::size_t i=0;i<m_params.size();i++) {
176  int n = m_params[i].Match(source + offset); // note Match, not MatchUnchecked because we need to check validity after the offset
177  if(n == -1)
178  return -1;
179  offset += n;
180  }
181 
182  return offset;
183  }
184 }
185 
186 #endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
YAML_PM::REGEX_SEQ
@ REGEX_SEQ
Definition: regex.h:16
YAML_PM::RegEx::MatchOpEmpty
int MatchOpEmpty(const Source &source) const
Definition: regeximpl.h:108
YAML_PM::REGEX_RANGE
@ REGEX_RANGE
Definition: regex.h:16
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::StreamCharSource
Definition: streamcharsource.h:14
YAML_PM::REGEX_EMPTY
@ REGEX_EMPTY
Definition: regex.h:16
YAML_PM::RegEx::MatchOpRange
int MatchOpRange(const Source &source) const
Definition: regeximpl.h:127
YAML_PM::RegEx::MatchUnchecked
int MatchUnchecked(const Source &source) const
Definition: regeximpl.h:79
YAML_PM::RegEx::MatchOpMatch
int MatchOpMatch(const Source &source) const
Definition: regeximpl.h:119
YAML_PM::REGEX_NOT
@ REGEX_NOT
Definition: regex.h:16
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::RegEx::MatchOpOr
int MatchOpOr(const Source &source) const
Definition: regeximpl.h:135
YAML_PM::RegEx::m_op
REGEX_OP m_op
Definition: regex.h:59
YAML_PM::REGEX_AND
@ REGEX_AND
Definition: regex.h:16
YAML_PM::RegEx::m_a
char m_a
Definition: regex.h:60
YAML_PM::RegEx::MatchOpNot
int MatchOpNot(const Source &source) const
Definition: regeximpl.h:163
YAML_PM::StringCharSource
Definition: stringsource.h:13
YAML_PM::REGEX_MATCH
@ REGEX_MATCH
Definition: regex.h:16
stream.h
YAML_PM::RegEx::MatchOpSeq
int MatchOpSeq(const Source &source) const
Definition: regeximpl.h:173
YAML_PM::RegEx::m_z
char m_z
Definition: regex.h:60
YAML_PM::RegEx::Match
int Match(const std::string &str) const
Definition: regeximpl.h:42
YAML_PM::RegEx::m_params
std::vector< RegEx > m_params
Definition: regex.h:61
stringsource.h
YAML_PM::RegEx::Matches
bool Matches(char ch) const
Definition: regeximpl.h:16
YAML_PM::Stream::eof
static char eof()
Definition: stream.h:36
YAML_PM::RegEx::MatchOpAnd
int MatchOpAnd(const Source &source) const
Definition: regeximpl.h:149
YAML_PM::Stream
Definition: stream.h:20
YAML_PM::REGEX_OR
@ REGEX_OR
Definition: regex.h:16
YAML_PM::RegEx::IsValidSource
bool IsValidSource(const Source &source) const
Definition: regeximpl.h:55
streamcharsource.h


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:45:59