blob: abb1b66853c9d55f86b40be97970527671a205c1 [file] [log] [blame]
Howard Hinnant70505302010-06-17 00:34:591//===-------------------------- regex.cpp ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "regex"
11#include "algorithm"
12#include "iterator"
13
Howard Hinnant70505302010-06-17 00:34:5914_LIBCPP_BEGIN_NAMESPACE_STD
15
16static
17const char*
18make_error_type_string(regex_constants::error_type ecode)
19{
20 switch (ecode)
21 {
22 case regex_constants::error_collate:
23 return "error_collate";
24 case regex_constants::error_ctype:
25 return "error_ctype";
26 case regex_constants::error_escape:
27 return "error_escape";
28 case regex_constants::error_backref:
29 return "error_backref";
30 case regex_constants::error_brack:
31 return "error_brack";
32 case regex_constants::error_paren:
33 return "error_paren";
34 case regex_constants::error_brace:
35 return "error_brace";
36 case regex_constants::error_badbrace:
37 return "error_badbrace";
38 case regex_constants::error_range:
39 return "error_range";
40 case regex_constants::error_space:
41 return "error_space";
42 case regex_constants::error_badrepeat:
43 return "error_badrepeat";
44 case regex_constants::error_complexity:
45 return "error_complexity";
46 case regex_constants::error_stack:
47 return "error_stack";
48 }
49 return "unknown error_type";
50}
51
52regex_error::regex_error(regex_constants::error_type ecode)
53 : runtime_error(make_error_type_string(ecode)),
54 __code_(ecode)
55{}
56
57regex_error::~regex_error() throw() {}
58
59namespace {
60
61struct collationnames
62{
63 const char* elem_;
64 char char_;
65};
66
67const collationnames collatenames[] =
68{
69 {"A", 0x41},
70 {"B", 0x42},
71 {"C", 0x43},
72 {"D", 0x44},
73 {"E", 0x45},
74 {"F", 0x46},
75 {"G", 0x47},
76 {"H", 0x48},
77 {"I", 0x49},
78 {"J", 0x4a},
79 {"K", 0x4b},
80 {"L", 0x4c},
81 {"M", 0x4d},
82 {"N", 0x4e},
83 {"NUL", 0x00},
84 {"O", 0x4f},
85 {"P", 0x50},
86 {"Q", 0x51},
87 {"R", 0x52},
88 {"S", 0x53},
89 {"T", 0x54},
90 {"U", 0x55},
91 {"V", 0x56},
92 {"W", 0x57},
93 {"X", 0x58},
94 {"Y", 0x59},
95 {"Z", 0x5a},
96 {"a", 0x61},
97 {"alert", 0x07},
98 {"ampersand", 0x26},
99 {"apostrophe", 0x27},
100 {"asterisk", 0x2a},
101 {"b", 0x62},
102 {"backslash", 0x5c},
103 {"backspace", 0x08},
104 {"c", 0x63},
105 {"carriage-return", 0x0d},
106 {"circumflex", 0x5e},
107 {"circumflex-accent", 0x5e},
108 {"colon", 0x3a},
109 {"comma", 0x2c},
110 {"commercial-at", 0x40},
111 {"d", 0x64},
112 {"dollar-sign", 0x24},
113 {"e", 0x65},
114 {"eight", 0x38},
115 {"equals-sign", 0x3d},
116 {"exclamation-mark", 0x21},
117 {"f", 0x66},
118 {"five", 0x35},
119 {"form-feed", 0x0c},
120 {"four", 0x34},
121 {"full-stop", 0x2e},
122 {"g", 0x67},
123 {"grave-accent", 0x60},
124 {"greater-than-sign", 0x3e},
125 {"h", 0x68},
126 {"hyphen", 0x2d},
127 {"hyphen-minus", 0x2d},
128 {"i", 0x69},
129 {"j", 0x6a},
130 {"k", 0x6b},
131 {"l", 0x6c},
132 {"left-brace", 0x7b},
133 {"left-curly-bracket", 0x7b},
134 {"left-parenthesis", 0x28},
135 {"left-square-bracket", 0x5b},
136 {"less-than-sign", 0x3c},
137 {"low-line", 0x5f},
138 {"m", 0x6d},
139 {"n", 0x6e},
140 {"newline", 0x0a},
141 {"nine", 0x39},
142 {"number-sign", 0x23},
143 {"o", 0x6f},
144 {"one", 0x31},
145 {"p", 0x70},
146 {"percent-sign", 0x25},
147 {"period", 0x2e},
148 {"plus-sign", 0x2b},
149 {"q", 0x71},
150 {"question-mark", 0x3f},
151 {"quotation-mark", 0x22},
152 {"r", 0x72},
153 {"reverse-solidus", 0x5c},
154 {"right-brace", 0x7d},
155 {"right-curly-bracket", 0x7d},
156 {"right-parenthesis", 0x29},
157 {"right-square-bracket", 0x5d},
158 {"s", 0x73},
159 {"semicolon", 0x3b},
160 {"seven", 0x37},
161 {"six", 0x36},
162 {"slash", 0x2f},
163 {"solidus", 0x2f},
164 {"space", 0x20},
165 {"t", 0x74},
166 {"tab", 0x09},
167 {"three", 0x33},
168 {"tilde", 0x7e},
169 {"two", 0x32},
170 {"u", 0x75},
171 {"underscore", 0x5f},
172 {"v", 0x76},
173 {"vertical-line", 0x7c},
174 {"vertical-tab", 0x0b},
175 {"w", 0x77},
176 {"x", 0x78},
177 {"y", 0x79},
178 {"z", 0x7a},
179 {"zero", 0x30}
180};
181
Howard Hinnant24757ff2010-06-21 21:01:43182struct classnames
183{
184 const char* elem_;
185 ctype_base::mask mask_;
186};
187
188const classnames ClassNames[] =
189{
190 {"alnum", ctype_base::alnum},
191 {"alpha", ctype_base::alpha},
192 {"blank", ctype_base::blank},
193 {"cntrl", ctype_base::cntrl},
194 {"d", ctype_base::digit},
195 {"digit", ctype_base::digit},
196 {"graph", ctype_base::graph},
197 {"lower", ctype_base::lower},
198 {"print", ctype_base::print},
199 {"punct", ctype_base::punct},
200 {"s", ctype_base::space},
201 {"space", ctype_base::space},
202 {"upper", ctype_base::upper},
203 {"w", regex_traits<char>::__regex_word},
204 {"xdigit", ctype_base::xdigit}
205};
206
Howard Hinnant70505302010-06-17 00:34:59207struct use_strcmp
208{
209 bool operator()(const collationnames& x, const char* y)
210 {return strcmp(x.elem_, y) < 0;}
Howard Hinnant24757ff2010-06-21 21:01:43211 bool operator()(const classnames& x, const char* y)
212 {return strcmp(x.elem_, y) < 0;}
Howard Hinnant70505302010-06-17 00:34:59213};
214
215}
216
217string
218__get_collation_name(const char* s)
219{
Howard Hinnant70505302010-06-17 00:34:59220 const collationnames* i =
221 lower_bound(begin(collatenames), end(collatenames), s, use_strcmp());
222 string r;
223 if (i != end(collatenames) && strcmp(s, i->elem_) == 0)
224 r = char(i->char_);
225 return r;
226}
227
Howard Hinnant24757ff2010-06-21 21:01:43228ctype_base::mask
229__get_classname(const char* s, bool __icase)
230{
231 const classnames* i =
232 lower_bound(begin(ClassNames), end(ClassNames), s, use_strcmp());
233 ctype_base::mask r = 0;
234 if (i != end(ClassNames) && strcmp(s, i->elem_) == 0)
235 {
236 r = i->mask_;
237 if (r == regex_traits<char>::__regex_word)
238 r |= ctype_base::alnum | ctype_base::upper | ctype_base::lower;
239 else if (__icase)
240 {
241 if (r & (ctype_base::lower | ctype_base::upper))
242 r |= ctype_base::alpha;
243 }
244 }
245 return r;
246}
247
Howard Hinnant70505302010-06-17 00:34:59248_LIBCPP_END_NAMESPACE_STD