1 //===-------------------------- regex.cpp ---------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 _LIBCPP_BEGIN_NAMESPACE_STD
17 make_error_type_string(regex_constants::error_type ecode)
21 case regex_constants::error_collate:
22 return "The expression contained an invalid collating element name.";
23 case regex_constants::error_ctype:
24 return "The expression contained an invalid character class name.";
25 case regex_constants::error_escape:
26 return "The expression contained an invalid escaped character, or a "
28 case regex_constants::error_backref:
29 return "The expression contained an invalid back reference.";
30 case regex_constants::error_brack:
31 return "The expression contained mismatched [ and ].";
32 case regex_constants::error_paren:
33 return "The expression contained mismatched ( and ).";
34 case regex_constants::error_brace:
35 return "The expression contained mismatched { and }.";
36 case regex_constants::error_badbrace:
37 return "The expression contained an invalid range in a {} expression.";
38 case regex_constants::error_range:
39 return "The expression contained an invalid character range, "
40 "such as [b-a] in most encodings.";
41 case regex_constants::error_space:
42 return "There was insufficient memory to convert the expression into "
43 "a finite state machine.";
44 case regex_constants::error_badrepeat:
45 return "One of *?+{ was not preceded by a valid regular expression.";
46 case regex_constants::error_complexity:
47 return "The complexity of an attempted match against a regular "
48 "expression exceeded a pre-set level.";
49 case regex_constants::error_stack:
50 return "There was insufficient memory to determine whether the regular "
51 "expression could match the specified character sequence.";
52 case regex_constants::__re_err_grammar:
53 return "An invalid regex grammar has been requested.";
54 case regex_constants::__re_err_empty:
55 return "An empty regex is not allowed in the POSIX grammar.";
59 return "Unknown error type";
62 regex_error::regex_error(regex_constants::error_type ecode)
63 : runtime_error(make_error_type_string(ecode)),
67 regex_error::~regex_error() throw() {}
77 const collationnames collatenames[] =
109 {"apostrophe", 0x27},
115 {"carriage-return", 0x0d},
116 {"circumflex", 0x5e},
117 {"circumflex-accent", 0x5e},
120 {"commercial-at", 0x40},
122 {"dollar-sign", 0x24},
125 {"equals-sign", 0x3d},
126 {"exclamation-mark", 0x21},
133 {"grave-accent", 0x60},
134 {"greater-than-sign", 0x3e},
137 {"hyphen-minus", 0x2d},
142 {"left-brace", 0x7b},
143 {"left-curly-bracket", 0x7b},
144 {"left-parenthesis", 0x28},
145 {"left-square-bracket", 0x5b},
146 {"less-than-sign", 0x3c},
152 {"number-sign", 0x23},
156 {"percent-sign", 0x25},
160 {"question-mark", 0x3f},
161 {"quotation-mark", 0x22},
163 {"reverse-solidus", 0x5c},
164 {"right-brace", 0x7d},
165 {"right-curly-bracket", 0x7d},
166 {"right-parenthesis", 0x29},
167 {"right-square-bracket", 0x5d},
181 {"underscore", 0x5f},
183 {"vertical-line", 0x7c},
184 {"vertical-tab", 0x0b},
195 regex_traits<char>::char_class_type mask_;
198 const classnames ClassNames[] =
200 {"alnum", ctype_base::alnum},
201 {"alpha", ctype_base::alpha},
202 {"blank", ctype_base::blank},
203 {"cntrl", ctype_base::cntrl},
204 {"d", ctype_base::digit},
205 {"digit", ctype_base::digit},
206 {"graph", ctype_base::graph},
207 {"lower", ctype_base::lower},
208 {"print", ctype_base::print},
209 {"punct", ctype_base::punct},
210 {"s", ctype_base::space},
211 {"space", ctype_base::space},
212 {"upper", ctype_base::upper},
213 {"w", regex_traits<char>::__regex_word},
214 {"xdigit", ctype_base::xdigit}
219 bool operator()(const collationnames& x, const char* y)
220 {return strcmp(x.elem_, y) < 0;}
221 bool operator()(const classnames& x, const char* y)
222 {return strcmp(x.elem_, y) < 0;}
228 __get_collation_name(const char* s)
230 const collationnames* i =
231 _VSTD::lower_bound(begin(collatenames), end(collatenames), s, use_strcmp());
233 if (i != end(collatenames) && strcmp(s, i->elem_) == 0)
238 regex_traits<char>::char_class_type
239 __get_classname(const char* s, bool __icase)
241 const classnames* i =
242 _VSTD::lower_bound(begin(ClassNames), end(ClassNames), s, use_strcmp());
243 regex_traits<char>::char_class_type r = 0;
244 if (i != end(ClassNames) && strcmp(s, i->elem_) == 0)
247 if (r == regex_traits<char>::__regex_word)
248 r |= ctype_base::alnum | ctype_base::upper | ctype_base::lower;
251 if (r & (ctype_base::lower | ctype_base::upper))
252 r |= ctype_base::alpha;
260 __match_any_but_newline<char>::__exec(__state& __s) const
262 if (__s.__current_ != __s.__last_)
264 switch (*__s.__current_)
268 __s.__do_ = __state::__reject;
269 __s.__node_ = nullptr;
272 __s.__do_ = __state::__accept_and_consume;
274 __s.__node_ = this->first();
280 __s.__do_ = __state::__reject;
281 __s.__node_ = nullptr;
287 __match_any_but_newline<wchar_t>::__exec(__state& __s) const
289 if (__s.__current_ != __s.__last_)
291 switch (*__s.__current_)
297 __s.__do_ = __state::__reject;
298 __s.__node_ = nullptr;
301 __s.__do_ = __state::__accept_and_consume;
303 __s.__node_ = this->first();
309 __s.__do_ = __state::__reject;
310 __s.__node_ = nullptr;
314 _LIBCPP_END_NAMESPACE_STD