xref: /openbsd-src/gnu/llvm/libcxx/src/regex.cpp (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
246035553Spatrick //
346035553Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
446035553Spatrick // See https://llvm.org/LICENSE.txt for license information.
546035553Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
646035553Spatrick //
746035553Spatrick //===----------------------------------------------------------------------===//
846035553Spatrick 
9*4bdff4beSrobert #include <algorithm>
10*4bdff4beSrobert #include <iterator>
11*4bdff4beSrobert #include <regex>
1246035553Spatrick 
1346035553Spatrick _LIBCPP_BEGIN_NAMESPACE_STD
1446035553Spatrick 
1546035553Spatrick static
1646035553Spatrick const char*
make_error_type_string(regex_constants::error_type ecode)1746035553Spatrick make_error_type_string(regex_constants::error_type ecode)
1846035553Spatrick {
1946035553Spatrick     switch (ecode)
2046035553Spatrick     {
2146035553Spatrick     case regex_constants::error_collate:
2246035553Spatrick         return "The expression contained an invalid collating element name.";
2346035553Spatrick     case regex_constants::error_ctype:
2446035553Spatrick         return "The expression contained an invalid character class name.";
2546035553Spatrick     case regex_constants::error_escape:
2646035553Spatrick         return "The expression contained an invalid escaped character, or a "
2746035553Spatrick                "trailing escape.";
2846035553Spatrick     case regex_constants::error_backref:
2946035553Spatrick         return "The expression contained an invalid back reference.";
3046035553Spatrick     case regex_constants::error_brack:
3146035553Spatrick         return "The expression contained mismatched [ and ].";
3246035553Spatrick     case regex_constants::error_paren:
3346035553Spatrick         return "The expression contained mismatched ( and ).";
3446035553Spatrick     case regex_constants::error_brace:
3546035553Spatrick         return "The expression contained mismatched { and }.";
3646035553Spatrick     case regex_constants::error_badbrace:
3746035553Spatrick         return "The expression contained an invalid range in a {} expression.";
3846035553Spatrick     case regex_constants::error_range:
3946035553Spatrick         return "The expression contained an invalid character range, "
4046035553Spatrick                "such as [b-a] in most encodings.";
4146035553Spatrick     case regex_constants::error_space:
4246035553Spatrick         return "There was insufficient memory to convert the expression into "
4346035553Spatrick                "a finite state machine.";
4446035553Spatrick     case regex_constants::error_badrepeat:
4546035553Spatrick         return "One of *?+{ was not preceded by a valid regular expression.";
4646035553Spatrick     case regex_constants::error_complexity:
4746035553Spatrick         return "The complexity of an attempted match against a regular "
4846035553Spatrick                "expression exceeded a pre-set level.";
4946035553Spatrick     case regex_constants::error_stack:
5046035553Spatrick         return "There was insufficient memory to determine whether the regular "
5146035553Spatrick                "expression could match the specified character sequence.";
5246035553Spatrick     case regex_constants::__re_err_grammar:
5346035553Spatrick         return "An invalid regex grammar has been requested.";
5446035553Spatrick     case regex_constants::__re_err_empty:
5546035553Spatrick         return "An empty regex is not allowed in the POSIX grammar.";
5646035553Spatrick     case regex_constants::__re_err_parse:
5746035553Spatrick         return "The parser did not consume the entire regular expression.";
5846035553Spatrick     default:
5946035553Spatrick         break;
6046035553Spatrick     }
6146035553Spatrick     return "Unknown error type";
6246035553Spatrick }
6346035553Spatrick 
regex_error(regex_constants::error_type ecode)6446035553Spatrick regex_error::regex_error(regex_constants::error_type ecode)
6546035553Spatrick     : runtime_error(make_error_type_string(ecode)),
6646035553Spatrick       __code_(ecode)
6746035553Spatrick {}
6846035553Spatrick 
~regex_error()6946035553Spatrick regex_error::~regex_error() throw() {}
7046035553Spatrick 
7146035553Spatrick namespace {
7246035553Spatrick 
7346035553Spatrick struct collationnames
7446035553Spatrick {
7546035553Spatrick     const char* elem_;
7646035553Spatrick     char char_;
7746035553Spatrick };
7846035553Spatrick 
79*4bdff4beSrobert #if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
80*4bdff4beSrobert // EBCDIC IBM-1047
81*4bdff4beSrobert // Sorted via the EBCDIC collating sequence
82*4bdff4beSrobert const collationnames collatenames[] =
83*4bdff4beSrobert {
84*4bdff4beSrobert     {"a", 0x81},
85*4bdff4beSrobert     {"alert", 0x2f},
86*4bdff4beSrobert     {"ampersand", 0x50},
87*4bdff4beSrobert     {"apostrophe", 0x7d},
88*4bdff4beSrobert     {"asterisk", 0x5c},
89*4bdff4beSrobert     {"b", 0x82},
90*4bdff4beSrobert     {"backslash", 0xe0},
91*4bdff4beSrobert     {"backspace", 0x16},
92*4bdff4beSrobert     {"c", 0x83},
93*4bdff4beSrobert     {"carriage-return", 0xd},
94*4bdff4beSrobert     {"circumflex", 0x5f},
95*4bdff4beSrobert     {"circumflex-accent", 0x5f},
96*4bdff4beSrobert     {"colon", 0x7a},
97*4bdff4beSrobert     {"comma", 0x6b},
98*4bdff4beSrobert     {"commercial-at", 0x7c},
99*4bdff4beSrobert     {"d", 0x84},
100*4bdff4beSrobert     {"dollar-sign", 0x5b},
101*4bdff4beSrobert     {"e", 0x85},
102*4bdff4beSrobert     {"eight", 0xf8},
103*4bdff4beSrobert     {"equals-sign", 0x7e},
104*4bdff4beSrobert     {"exclamation-mark", 0x5a},
105*4bdff4beSrobert     {"f", 0x86},
106*4bdff4beSrobert     {"five", 0xf5},
107*4bdff4beSrobert     {"form-feed", 0xc},
108*4bdff4beSrobert     {"four", 0xf4},
109*4bdff4beSrobert     {"full-stop", 0x4b},
110*4bdff4beSrobert     {"g", 0x87},
111*4bdff4beSrobert     {"grave-accent", 0x79},
112*4bdff4beSrobert     {"greater-than-sign", 0x6e},
113*4bdff4beSrobert     {"h", 0x88},
114*4bdff4beSrobert     {"hyphen", 0x60},
115*4bdff4beSrobert     {"hyphen-minus", 0x60},
116*4bdff4beSrobert     {"i", 0x89},
117*4bdff4beSrobert     {"j", 0x91},
118*4bdff4beSrobert     {"k", 0x92},
119*4bdff4beSrobert     {"l", 0x93},
120*4bdff4beSrobert     {"left-brace", 0xc0},
121*4bdff4beSrobert     {"left-curly-bracket", 0xc0},
122*4bdff4beSrobert     {"left-parenthesis", 0x4d},
123*4bdff4beSrobert     {"left-square-bracket", 0xad},
124*4bdff4beSrobert     {"less-than-sign", 0x4c},
125*4bdff4beSrobert     {"low-line", 0x6d},
126*4bdff4beSrobert     {"m", 0x94},
127*4bdff4beSrobert     {"n", 0x95},
128*4bdff4beSrobert     {"newline", 0x15},
129*4bdff4beSrobert     {"nine", 0xf9},
130*4bdff4beSrobert     {"number-sign", 0x7b},
131*4bdff4beSrobert     {"o", 0x96},
132*4bdff4beSrobert     {"one", 0xf1},
133*4bdff4beSrobert     {"p", 0x97},
134*4bdff4beSrobert     {"percent-sign", 0x6c},
135*4bdff4beSrobert     {"period", 0x4b},
136*4bdff4beSrobert     {"plus-sign", 0x4e},
137*4bdff4beSrobert     {"q", 0x98},
138*4bdff4beSrobert     {"question-mark", 0x6f},
139*4bdff4beSrobert     {"quotation-mark", 0x7f},
140*4bdff4beSrobert     {"r", 0x99},
141*4bdff4beSrobert     {"reverse-solidus", 0xe0},
142*4bdff4beSrobert     {"right-brace", 0xd0},
143*4bdff4beSrobert     {"right-curly-bracket", 0xd0},
144*4bdff4beSrobert     {"right-parenthesis", 0x5d},
145*4bdff4beSrobert     {"right-square-bracket", 0xbd},
146*4bdff4beSrobert     {"s", 0xa2},
147*4bdff4beSrobert     {"semicolon", 0x5e},
148*4bdff4beSrobert     {"seven", 0xf7},
149*4bdff4beSrobert     {"six", 0xf6},
150*4bdff4beSrobert     {"slash", 0x61},
151*4bdff4beSrobert     {"solidus", 0x61},
152*4bdff4beSrobert     {"space", 0x40},
153*4bdff4beSrobert     {"t", 0xa3},
154*4bdff4beSrobert     {"tab", 0x5},
155*4bdff4beSrobert     {"three", 0xf3},
156*4bdff4beSrobert     {"tilde", 0xa1},
157*4bdff4beSrobert     {"two", 0xf2},
158*4bdff4beSrobert     {"u", 0xa4},
159*4bdff4beSrobert     {"underscore", 0x6d},
160*4bdff4beSrobert     {"v", 0xa5},
161*4bdff4beSrobert     {"vertical-line", 0x4f},
162*4bdff4beSrobert     {"vertical-tab", 0xb},
163*4bdff4beSrobert     {"w", 0xa6},
164*4bdff4beSrobert     {"x", 0xa7},
165*4bdff4beSrobert     {"y", 0xa8},
166*4bdff4beSrobert     {"z", 0xa9},
167*4bdff4beSrobert     {"zero", 0xf0},
168*4bdff4beSrobert     {"A", 0xc1},
169*4bdff4beSrobert     {"B", 0xc2},
170*4bdff4beSrobert     {"C", 0xc3},
171*4bdff4beSrobert     {"D", 0xc4},
172*4bdff4beSrobert     {"E", 0xc5},
173*4bdff4beSrobert     {"F", 0xc6},
174*4bdff4beSrobert     {"G", 0xc7},
175*4bdff4beSrobert     {"H", 0xc8},
176*4bdff4beSrobert     {"I", 0xc9},
177*4bdff4beSrobert     {"J", 0xd1},
178*4bdff4beSrobert     {"K", 0xd2},
179*4bdff4beSrobert     {"L", 0xd3},
180*4bdff4beSrobert     {"M", 0xd4},
181*4bdff4beSrobert     {"N", 0xd5},
182*4bdff4beSrobert     {"NUL", 0},
183*4bdff4beSrobert     {"O", 0xd6},
184*4bdff4beSrobert     {"P", 0xd7},
185*4bdff4beSrobert     {"Q", 0xd8},
186*4bdff4beSrobert     {"R", 0xd9},
187*4bdff4beSrobert     {"S", 0xe2},
188*4bdff4beSrobert     {"T", 0xe3},
189*4bdff4beSrobert     {"U", 0xe4},
190*4bdff4beSrobert     {"V", 0xe5},
191*4bdff4beSrobert     {"W", 0xe6},
192*4bdff4beSrobert     {"X", 0xe7},
193*4bdff4beSrobert     {"Y", 0xe8},
194*4bdff4beSrobert     {"Z", 0xe9}
195*4bdff4beSrobert };
196*4bdff4beSrobert #else
197*4bdff4beSrobert // ASCII
19846035553Spatrick const collationnames collatenames[] =
19946035553Spatrick {
20046035553Spatrick     {"A", 0x41},
20146035553Spatrick     {"B", 0x42},
20246035553Spatrick     {"C", 0x43},
20346035553Spatrick     {"D", 0x44},
20446035553Spatrick     {"E", 0x45},
20546035553Spatrick     {"F", 0x46},
20646035553Spatrick     {"G", 0x47},
20746035553Spatrick     {"H", 0x48},
20846035553Spatrick     {"I", 0x49},
20946035553Spatrick     {"J", 0x4a},
21046035553Spatrick     {"K", 0x4b},
21146035553Spatrick     {"L", 0x4c},
21246035553Spatrick     {"M", 0x4d},
21346035553Spatrick     {"N", 0x4e},
21446035553Spatrick     {"NUL", 0x00},
21546035553Spatrick     {"O", 0x4f},
21646035553Spatrick     {"P", 0x50},
21746035553Spatrick     {"Q", 0x51},
21846035553Spatrick     {"R", 0x52},
21946035553Spatrick     {"S", 0x53},
22046035553Spatrick     {"T", 0x54},
22146035553Spatrick     {"U", 0x55},
22246035553Spatrick     {"V", 0x56},
22346035553Spatrick     {"W", 0x57},
22446035553Spatrick     {"X", 0x58},
22546035553Spatrick     {"Y", 0x59},
22646035553Spatrick     {"Z", 0x5a},
22746035553Spatrick     {"a", 0x61},
22846035553Spatrick     {"alert", 0x07},
22946035553Spatrick     {"ampersand", 0x26},
23046035553Spatrick     {"apostrophe", 0x27},
23146035553Spatrick     {"asterisk", 0x2a},
23246035553Spatrick     {"b", 0x62},
23346035553Spatrick     {"backslash", 0x5c},
23446035553Spatrick     {"backspace", 0x08},
23546035553Spatrick     {"c", 0x63},
23646035553Spatrick     {"carriage-return", 0x0d},
23746035553Spatrick     {"circumflex", 0x5e},
23846035553Spatrick     {"circumflex-accent", 0x5e},
23946035553Spatrick     {"colon", 0x3a},
24046035553Spatrick     {"comma", 0x2c},
24146035553Spatrick     {"commercial-at", 0x40},
24246035553Spatrick     {"d", 0x64},
24346035553Spatrick     {"dollar-sign", 0x24},
24446035553Spatrick     {"e", 0x65},
24546035553Spatrick     {"eight", 0x38},
24646035553Spatrick     {"equals-sign", 0x3d},
24746035553Spatrick     {"exclamation-mark", 0x21},
24846035553Spatrick     {"f", 0x66},
24946035553Spatrick     {"five", 0x35},
25046035553Spatrick     {"form-feed", 0x0c},
25146035553Spatrick     {"four", 0x34},
25246035553Spatrick     {"full-stop", 0x2e},
25346035553Spatrick     {"g", 0x67},
25446035553Spatrick     {"grave-accent", 0x60},
25546035553Spatrick     {"greater-than-sign", 0x3e},
25646035553Spatrick     {"h", 0x68},
25746035553Spatrick     {"hyphen", 0x2d},
25846035553Spatrick     {"hyphen-minus", 0x2d},
25946035553Spatrick     {"i", 0x69},
26046035553Spatrick     {"j", 0x6a},
26146035553Spatrick     {"k", 0x6b},
26246035553Spatrick     {"l", 0x6c},
26346035553Spatrick     {"left-brace", 0x7b},
26446035553Spatrick     {"left-curly-bracket", 0x7b},
26546035553Spatrick     {"left-parenthesis", 0x28},
26646035553Spatrick     {"left-square-bracket", 0x5b},
26746035553Spatrick     {"less-than-sign", 0x3c},
26846035553Spatrick     {"low-line", 0x5f},
26946035553Spatrick     {"m", 0x6d},
27046035553Spatrick     {"n", 0x6e},
27146035553Spatrick     {"newline", 0x0a},
27246035553Spatrick     {"nine", 0x39},
27346035553Spatrick     {"number-sign", 0x23},
27446035553Spatrick     {"o", 0x6f},
27546035553Spatrick     {"one", 0x31},
27646035553Spatrick     {"p", 0x70},
27746035553Spatrick     {"percent-sign", 0x25},
27846035553Spatrick     {"period", 0x2e},
27946035553Spatrick     {"plus-sign", 0x2b},
28046035553Spatrick     {"q", 0x71},
28146035553Spatrick     {"question-mark", 0x3f},
28246035553Spatrick     {"quotation-mark", 0x22},
28346035553Spatrick     {"r", 0x72},
28446035553Spatrick     {"reverse-solidus", 0x5c},
28546035553Spatrick     {"right-brace", 0x7d},
28646035553Spatrick     {"right-curly-bracket", 0x7d},
28746035553Spatrick     {"right-parenthesis", 0x29},
28846035553Spatrick     {"right-square-bracket", 0x5d},
28946035553Spatrick     {"s", 0x73},
29046035553Spatrick     {"semicolon", 0x3b},
29146035553Spatrick     {"seven", 0x37},
29246035553Spatrick     {"six", 0x36},
29346035553Spatrick     {"slash", 0x2f},
29446035553Spatrick     {"solidus", 0x2f},
29546035553Spatrick     {"space", 0x20},
29646035553Spatrick     {"t", 0x74},
29746035553Spatrick     {"tab", 0x09},
29846035553Spatrick     {"three", 0x33},
29946035553Spatrick     {"tilde", 0x7e},
30046035553Spatrick     {"two", 0x32},
30146035553Spatrick     {"u", 0x75},
30246035553Spatrick     {"underscore", 0x5f},
30346035553Spatrick     {"v", 0x76},
30446035553Spatrick     {"vertical-line", 0x7c},
30546035553Spatrick     {"vertical-tab", 0x0b},
30646035553Spatrick     {"w", 0x77},
30746035553Spatrick     {"x", 0x78},
30846035553Spatrick     {"y", 0x79},
30946035553Spatrick     {"z", 0x7a},
31046035553Spatrick     {"zero", 0x30}
31146035553Spatrick };
312*4bdff4beSrobert #endif
31346035553Spatrick 
31446035553Spatrick struct classnames
31546035553Spatrick {
31646035553Spatrick     const char* elem_;
31746035553Spatrick     regex_traits<char>::char_class_type mask_;
31846035553Spatrick };
31946035553Spatrick 
32046035553Spatrick const classnames ClassNames[] =
32146035553Spatrick {
32246035553Spatrick     {"alnum",  ctype_base::alnum},
32346035553Spatrick     {"alpha",  ctype_base::alpha},
32446035553Spatrick     {"blank",  ctype_base::blank},
32546035553Spatrick     {"cntrl",  ctype_base::cntrl},
32646035553Spatrick     {"d",      ctype_base::digit},
32746035553Spatrick     {"digit",  ctype_base::digit},
32846035553Spatrick     {"graph",  ctype_base::graph},
32946035553Spatrick     {"lower",  ctype_base::lower},
33046035553Spatrick     {"print",  ctype_base::print},
33146035553Spatrick     {"punct",  ctype_base::punct},
33246035553Spatrick     {"s",      ctype_base::space},
33346035553Spatrick     {"space",  ctype_base::space},
33446035553Spatrick     {"upper",  ctype_base::upper},
33546035553Spatrick     {"w",      regex_traits<char>::__regex_word},
33646035553Spatrick     {"xdigit", ctype_base::xdigit}
33746035553Spatrick };
33846035553Spatrick 
33946035553Spatrick struct use_strcmp
34046035553Spatrick {
operator ()__anon418b8ea20111::use_strcmp34146035553Spatrick     bool operator()(const collationnames& x, const char* y)
34246035553Spatrick         {return strcmp(x.elem_, y) < 0;}
operator ()__anon418b8ea20111::use_strcmp34346035553Spatrick     bool operator()(const classnames& x, const char* y)
34446035553Spatrick         {return strcmp(x.elem_, y) < 0;}
34546035553Spatrick };
34646035553Spatrick 
34746035553Spatrick }
34846035553Spatrick 
34946035553Spatrick string
__get_collation_name(const char * s)35046035553Spatrick __get_collation_name(const char* s)
35146035553Spatrick {
35246035553Spatrick     const collationnames* i =
35346035553Spatrick             _VSTD::lower_bound(begin(collatenames), end(collatenames), s, use_strcmp());
35446035553Spatrick     string r;
35546035553Spatrick     if (i != end(collatenames) && strcmp(s, i->elem_) == 0)
35646035553Spatrick         r = char(i->char_);
35746035553Spatrick     return r;
35846035553Spatrick }
35946035553Spatrick 
36046035553Spatrick regex_traits<char>::char_class_type
__get_classname(const char * s,bool __icase)36146035553Spatrick __get_classname(const char* s, bool __icase)
36246035553Spatrick {
36346035553Spatrick     const classnames* i =
36446035553Spatrick             _VSTD::lower_bound(begin(ClassNames), end(ClassNames), s, use_strcmp());
36546035553Spatrick     regex_traits<char>::char_class_type r = 0;
36646035553Spatrick     if (i != end(ClassNames) && strcmp(s, i->elem_) == 0)
36746035553Spatrick     {
36846035553Spatrick         r = i->mask_;
36946035553Spatrick         if (r == regex_traits<char>::__regex_word)
37046035553Spatrick             r |= ctype_base::alnum | ctype_base::upper | ctype_base::lower;
37146035553Spatrick         else if (__icase)
37246035553Spatrick         {
37346035553Spatrick             if (r & (ctype_base::lower | ctype_base::upper))
37446035553Spatrick                 r |= ctype_base::alpha;
37546035553Spatrick         }
37646035553Spatrick     }
37746035553Spatrick     return r;
37846035553Spatrick }
37946035553Spatrick 
38046035553Spatrick template <>
38146035553Spatrick void
__exec(__state & __s) const38246035553Spatrick __match_any_but_newline<char>::__exec(__state& __s) const
38346035553Spatrick {
38446035553Spatrick     if (__s.__current_ != __s.__last_)
38546035553Spatrick     {
38646035553Spatrick         switch (*__s.__current_)
38746035553Spatrick         {
38846035553Spatrick         case '\r':
38946035553Spatrick         case '\n':
39046035553Spatrick             __s.__do_ = __state::__reject;
39146035553Spatrick             __s.__node_ = nullptr;
39246035553Spatrick             break;
39346035553Spatrick         default:
39446035553Spatrick             __s.__do_ = __state::__accept_and_consume;
39546035553Spatrick             ++__s.__current_;
39646035553Spatrick             __s.__node_ = this->first();
39746035553Spatrick             break;
39846035553Spatrick         }
39946035553Spatrick     }
40046035553Spatrick     else
40146035553Spatrick     {
40246035553Spatrick         __s.__do_ = __state::__reject;
40346035553Spatrick         __s.__node_ = nullptr;
40446035553Spatrick     }
40546035553Spatrick }
40646035553Spatrick 
40746035553Spatrick template <>
40846035553Spatrick void
__exec(__state & __s) const40946035553Spatrick __match_any_but_newline<wchar_t>::__exec(__state& __s) const
41046035553Spatrick {
41146035553Spatrick     if (__s.__current_ != __s.__last_)
41246035553Spatrick     {
41346035553Spatrick         switch (*__s.__current_)
41446035553Spatrick         {
41546035553Spatrick         case '\r':
41646035553Spatrick         case '\n':
41746035553Spatrick         case 0x2028:
41846035553Spatrick         case 0x2029:
41946035553Spatrick             __s.__do_ = __state::__reject;
42046035553Spatrick             __s.__node_ = nullptr;
42146035553Spatrick             break;
42246035553Spatrick         default:
42346035553Spatrick             __s.__do_ = __state::__accept_and_consume;
42446035553Spatrick             ++__s.__current_;
42546035553Spatrick             __s.__node_ = this->first();
42646035553Spatrick             break;
42746035553Spatrick         }
42846035553Spatrick     }
42946035553Spatrick     else
43046035553Spatrick     {
43146035553Spatrick         __s.__do_ = __state::__reject;
43246035553Spatrick         __s.__node_ = nullptr;
43346035553Spatrick     }
43446035553Spatrick }
43546035553Spatrick 
43646035553Spatrick _LIBCPP_END_NAMESPACE_STD
437