]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libc++/src/regex.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libc++ / src / regex.cpp
1 //===-------------------------- regex.cpp ---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "regex"
11 #include "algorithm"
12 #include "iterator"
13
14 _LIBCPP_BEGIN_NAMESPACE_STD
15
16 static
17 const char*
18 make_error_type_string(regex_constants::error_type ecode)
19 {
20     switch (ecode)
21     {
22     case regex_constants::error_collate:
23         return "The expression contained an invalid collating element name.";
24     case regex_constants::error_ctype:
25         return "The expression contained an invalid character class name.";
26     case regex_constants::error_escape:
27         return "The expression contained an invalid escaped character, or a "
28                "trailing escape.";
29     case regex_constants::error_backref:
30         return "The expression contained an invalid back reference.";
31     case regex_constants::error_brack:
32         return "The expression contained mismatched [ and ].";
33     case regex_constants::error_paren:
34         return "The expression contained mismatched ( and ).";
35     case regex_constants::error_brace:
36         return "The expression contained mismatched { and }.";
37     case regex_constants::error_badbrace:
38         return "The expression contained an invalid range in a {} expression.";
39     case regex_constants::error_range:
40         return "The expression contained an invalid character range, "
41                "such as [b-a] in most encodings.";
42     case regex_constants::error_space:
43         return "There was insufficient memory to convert the expression into "
44                "a finite state machine.";
45     case regex_constants::error_badrepeat:
46         return "One of *?+{ was not preceded by a valid regular expression.";
47     case regex_constants::error_complexity:
48         return "The complexity of an attempted match against a regular "
49                "expression exceeded a pre-set level.";
50     case regex_constants::error_stack:
51         return "There was insufficient memory to determine whether the regular "
52                "expression could match the specified character sequence.";
53     case regex_constants::__re_err_grammar:
54         return "An invalid regex grammar has been requested.";
55     case regex_constants::__re_err_empty:
56         return "An empty regex is not allowed in the POSIX grammar.";
57     default:
58         break;
59     }
60     return "Unknown error type";
61 }
62
63 regex_error::regex_error(regex_constants::error_type ecode)
64     : runtime_error(make_error_type_string(ecode)),
65       __code_(ecode)
66 {}
67
68 regex_error::~regex_error() throw() {}
69
70 namespace {
71
72 #pragma clang diagnostic push
73 #pragma clang diagnostic ignored "-Wpadded"
74
75 struct collationnames
76 {
77     const char* elem_;
78     char char_;
79 };
80
81 #pragma clang diagnostic pop
82
83 const collationnames collatenames[] =
84 {
85     {"A", 0x41},
86     {"B", 0x42},
87     {"C", 0x43},
88     {"D", 0x44},
89     {"E", 0x45},
90     {"F", 0x46},
91     {"G", 0x47},
92     {"H", 0x48},
93     {"I", 0x49},
94     {"J", 0x4a},
95     {"K", 0x4b},
96     {"L", 0x4c},
97     {"M", 0x4d},
98     {"N", 0x4e},
99     {"NUL", 0x00},
100     {"O", 0x4f},
101     {"P", 0x50},
102     {"Q", 0x51},
103     {"R", 0x52},
104     {"S", 0x53},
105     {"T", 0x54},
106     {"U", 0x55},
107     {"V", 0x56},
108     {"W", 0x57},
109     {"X", 0x58},
110     {"Y", 0x59},
111     {"Z", 0x5a},
112     {"a", 0x61},
113     {"alert", 0x07},
114     {"ampersand", 0x26},
115     {"apostrophe", 0x27},
116     {"asterisk", 0x2a},
117     {"b", 0x62},
118     {"backslash", 0x5c},
119     {"backspace", 0x08},
120     {"c", 0x63},
121     {"carriage-return", 0x0d},
122     {"circumflex", 0x5e},
123     {"circumflex-accent", 0x5e},
124     {"colon", 0x3a},
125     {"comma", 0x2c},
126     {"commercial-at", 0x40},
127     {"d", 0x64},
128     {"dollar-sign", 0x24},
129     {"e", 0x65},
130     {"eight", 0x38},
131     {"equals-sign", 0x3d},
132     {"exclamation-mark", 0x21},
133     {"f", 0x66},
134     {"five", 0x35},
135     {"form-feed", 0x0c},
136     {"four", 0x34},
137     {"full-stop", 0x2e},
138     {"g", 0x67},
139     {"grave-accent", 0x60},
140     {"greater-than-sign", 0x3e},
141     {"h", 0x68},
142     {"hyphen", 0x2d},
143     {"hyphen-minus", 0x2d},
144     {"i", 0x69},
145     {"j", 0x6a},
146     {"k", 0x6b},
147     {"l", 0x6c},
148     {"left-brace", 0x7b},
149     {"left-curly-bracket", 0x7b},
150     {"left-parenthesis", 0x28},
151     {"left-square-bracket", 0x5b},
152     {"less-than-sign", 0x3c},
153     {"low-line", 0x5f},
154     {"m", 0x6d},
155     {"n", 0x6e},
156     {"newline", 0x0a},
157     {"nine", 0x39},
158     {"number-sign", 0x23},
159     {"o", 0x6f},
160     {"one", 0x31},
161     {"p", 0x70},
162     {"percent-sign", 0x25},
163     {"period", 0x2e},
164     {"plus-sign", 0x2b},
165     {"q", 0x71},
166     {"question-mark", 0x3f},
167     {"quotation-mark", 0x22},
168     {"r", 0x72},
169     {"reverse-solidus", 0x5c},
170     {"right-brace", 0x7d},
171     {"right-curly-bracket", 0x7d},
172     {"right-parenthesis", 0x29},
173     {"right-square-bracket", 0x5d},
174     {"s", 0x73},
175     {"semicolon", 0x3b},
176     {"seven", 0x37},
177     {"six", 0x36},
178     {"slash", 0x2f},
179     {"solidus", 0x2f},
180     {"space", 0x20},
181     {"t", 0x74},
182     {"tab", 0x09},
183     {"three", 0x33},
184     {"tilde", 0x7e},
185     {"two", 0x32},
186     {"u", 0x75},
187     {"underscore", 0x5f},
188     {"v", 0x76},
189     {"vertical-line", 0x7c},
190     {"vertical-tab", 0x0b},
191     {"w", 0x77},
192     {"x", 0x78},
193     {"y", 0x79},
194     {"z", 0x7a},
195     {"zero", 0x30}
196 };
197
198 #pragma clang diagnostic push
199 #pragma clang diagnostic ignored "-Wpadded"
200
201 struct classnames
202 {
203     const char* elem_;
204     ctype_base::mask mask_;
205 };
206
207 #pragma clang diagnostic pop
208
209 const classnames ClassNames[] =
210 {
211     {"alnum",  ctype_base::alnum},
212     {"alpha",  ctype_base::alpha},
213     {"blank",  ctype_base::blank},
214     {"cntrl",  ctype_base::cntrl},
215     {"d",      ctype_base::digit},
216     {"digit",  ctype_base::digit},
217     {"graph",  ctype_base::graph},
218     {"lower",  ctype_base::lower},
219     {"print",  ctype_base::print},
220     {"punct",  ctype_base::punct},
221     {"s",      ctype_base::space},
222     {"space",  ctype_base::space},
223     {"upper",  ctype_base::upper},
224     {"w",      regex_traits<char>::__regex_word},
225     {"xdigit", ctype_base::xdigit}
226 };
227
228 struct use_strcmp
229 {
230     bool operator()(const collationnames& x, const char* y)
231         {return strcmp(x.elem_, y) < 0;}
232     bool operator()(const classnames& x, const char* y)
233         {return strcmp(x.elem_, y) < 0;}
234 };
235
236 }
237
238 string
239 __get_collation_name(const char* s)
240 {
241     const collationnames* i =
242             _VSTD::lower_bound(begin(collatenames), end(collatenames), s, use_strcmp());
243     string r;
244     if (i != end(collatenames) && strcmp(s, i->elem_) == 0)
245         r = char(i->char_);
246     return r;
247 }
248
249 ctype_base::mask
250 __get_classname(const char* s, bool __icase)
251 {
252     const classnames* i =
253             _VSTD::lower_bound(begin(ClassNames), end(ClassNames), s, use_strcmp());
254     ctype_base::mask r = 0;
255     if (i != end(ClassNames) && strcmp(s, i->elem_) == 0)
256     {
257         r = i->mask_;
258         if (r == regex_traits<char>::__regex_word)
259             r |= ctype_base::alnum | ctype_base::upper | ctype_base::lower;
260         else if (__icase)
261         {
262             if (r & (ctype_base::lower | ctype_base::upper))
263                 r |= ctype_base::alpha;
264         }
265     }
266     return r;
267 }
268
269 template <>
270 void
271 __match_any_but_newline<char>::__exec(__state& __s) const
272 {
273     if (__s.__current_ != __s.__last_)
274     {
275         switch (*__s.__current_)
276         {
277         case '\r':
278         case '\n':
279             __s.__do_ = __state::__reject;
280             __s.__node_ = nullptr;
281             break;
282         default:
283             __s.__do_ = __state::__accept_and_consume;
284             ++__s.__current_;
285             __s.__node_ = this->first();
286             break;
287         }
288     }
289     else
290     {
291         __s.__do_ = __state::__reject;
292         __s.__node_ = nullptr;
293     }
294 }
295
296 template <>
297 void
298 __match_any_but_newline<wchar_t>::__exec(__state& __s) const
299 {
300     if (__s.__current_ != __s.__last_)
301     {
302         switch (*__s.__current_)
303         {
304         case '\r':
305         case '\n':
306         case 0x2028:
307         case 0x2029:
308             __s.__do_ = __state::__reject;
309             __s.__node_ = nullptr;
310             break;
311         default:
312             __s.__do_ = __state::__accept_and_consume;
313             ++__s.__current_;
314             __s.__node_ = this->first();
315             break;
316         }
317     }
318     else
319     {
320         __s.__do_ = __state::__reject;
321         __s.__node_ = nullptr;
322     }
323 }
324
325 _LIBCPP_END_NAMESPACE_STD