1 //===----------------------------------------------------------------------===//
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 // <regex>
11 
12 // template <class charT> struct regex_traits;
13 
14 // template <class ForwardIterator>
15 //   char_class_type
16 //   lookup_classname(ForwardIterator first, ForwardIterator last,
17 //                    bool icase = false) const;
18 
19 #include <regex>
20 #include <cassert>
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 
24 template <class char_type>
25 void
test(const char_type * A,typename std::regex_traits<char_type>::char_class_type expected,bool icase=false)26 test(const char_type* A,
27      typename std::regex_traits<char_type>::char_class_type expected,
28      bool icase = false)
29 {
30     typedef typename std::regex_traits<char_type>::char_class_type char_class_type;
31     std::regex_traits<char_type> t;
32     typedef forward_iterator<const char_type*> F;
33     char_class_type result = t.lookup_classname(F(A), F(A + t.length(A)), icase);
34     assert(result == expected);
35 }
36 
37 template <class char_type>
38 void
test_w(const char_type * A,typename std::regex_traits<char_type>::char_class_type expected,bool icase=false)39 test_w(const char_type* A,
40        typename std::regex_traits<char_type>::char_class_type expected,
41         bool icase = false)
42 {
43     typedef typename std::regex_traits<char_type>::char_class_type char_class_type;
44     std::regex_traits<char_type> t;
45     typedef forward_iterator<const char_type*> F;
46     char_class_type result = t.lookup_classname(F(A), F(A + t.length(A)), icase);
47     assert((result & expected) == expected);
48     LIBCPP_ASSERT((expected | std::regex_traits<char_type>::__regex_word) == result);
49 
50     const bool matches_underscore = t.isctype('_', result);
51     if (result != expected)
52       assert(matches_underscore && "expected to match underscore");
53     else
54       assert(!matches_underscore && "should not match underscore");
55 }
56 
main()57 int main()
58 {
59 //  if __regex_word is not distinct from all the classes, bad things happen
60 //  See https://bugs.llvm.org/show_bug.cgi?id=26476 for an example.
61     LIBCPP_ASSERT((std::ctype_base::space  & std::regex_traits<char>::__regex_word) == 0);
62     LIBCPP_ASSERT((std::ctype_base::print  & std::regex_traits<char>::__regex_word) == 0);
63     LIBCPP_ASSERT((std::ctype_base::cntrl  & std::regex_traits<char>::__regex_word) == 0);
64     LIBCPP_ASSERT((std::ctype_base::upper  & std::regex_traits<char>::__regex_word) == 0);
65     LIBCPP_ASSERT((std::ctype_base::lower  & std::regex_traits<char>::__regex_word) == 0);
66     LIBCPP_ASSERT((std::ctype_base::alpha  & std::regex_traits<char>::__regex_word) == 0);
67     LIBCPP_ASSERT((std::ctype_base::digit  & std::regex_traits<char>::__regex_word) == 0);
68     LIBCPP_ASSERT((std::ctype_base::punct  & std::regex_traits<char>::__regex_word) == 0);
69     LIBCPP_ASSERT((std::ctype_base::xdigit & std::regex_traits<char>::__regex_word) == 0);
70     LIBCPP_ASSERT((std::ctype_base::blank  & std::regex_traits<char>::__regex_word) == 0);
71 
72     test("d", std::ctype_base::digit);
73     test("D", std::ctype_base::digit);
74     test("d", std::ctype_base::digit, true);
75     test("D", std::ctype_base::digit, true);
76 
77     test_w("w", std::ctype_base::alnum
78               | std::ctype_base::upper | std::ctype_base::lower);
79     test_w("W", std::ctype_base::alnum
80               | std::ctype_base::upper | std::ctype_base::lower);
81     test_w("w", std::ctype_base::alnum
82               | std::ctype_base::upper | std::ctype_base::lower, true);
83     test_w("W", std::ctype_base::alnum
84               | std::ctype_base::upper | std::ctype_base::lower, true);
85 
86     test("s", std::ctype_base::space);
87     test("S", std::ctype_base::space);
88     test("s", std::ctype_base::space, true);
89     test("S", std::ctype_base::space, true);
90 
91     test("alnum", std::ctype_base::alnum);
92     test("AlNum", std::ctype_base::alnum);
93     test("alnum", std::ctype_base::alnum, true);
94     test("AlNum", std::ctype_base::alnum, true);
95 
96     test("alpha", std::ctype_base::alpha);
97     test("Alpha", std::ctype_base::alpha);
98     test("alpha", std::ctype_base::alpha, true);
99     test("Alpha", std::ctype_base::alpha, true);
100 
101     test("blank", std::ctype_base::blank);
102     test("Blank", std::ctype_base::blank);
103     test("blank", std::ctype_base::blank, true);
104     test("Blank", std::ctype_base::blank, true);
105 
106     test("cntrl", std::ctype_base::cntrl);
107     test("Cntrl", std::ctype_base::cntrl);
108     test("cntrl", std::ctype_base::cntrl, true);
109     test("Cntrl", std::ctype_base::cntrl, true);
110 
111     test("digit", std::ctype_base::digit);
112     test("Digit", std::ctype_base::digit);
113     test("digit", std::ctype_base::digit, true);
114     test("Digit", std::ctype_base::digit, true);
115 
116     test("digit", std::ctype_base::digit);
117     test("DIGIT", std::ctype_base::digit);
118     test("digit", std::ctype_base::digit, true);
119     test("Digit", std::ctype_base::digit, true);
120 
121     test("graph", std::ctype_base::graph);
122     test("GRAPH", std::ctype_base::graph);
123     test("graph", std::ctype_base::graph, true);
124     test("Graph", std::ctype_base::graph, true);
125 
126     test("lower", std::ctype_base::lower);
127     test("LOWER", std::ctype_base::lower);
128     test("lower", std::ctype_base::lower | std::ctype_base::alpha, true);
129     test("Lower", std::ctype_base::lower | std::ctype_base::alpha, true);
130 
131     test("print", std::ctype_base::print);
132     test("PRINT", std::ctype_base::print);
133     test("print", std::ctype_base::print, true);
134     test("Print", std::ctype_base::print, true);
135 
136     test("punct", std::ctype_base::punct);
137     test("PUNCT", std::ctype_base::punct);
138     test("punct", std::ctype_base::punct, true);
139     test("Punct", std::ctype_base::punct, true);
140 
141     test("space", std::ctype_base::space);
142     test("SPACE", std::ctype_base::space);
143     test("space", std::ctype_base::space, true);
144     test("Space", std::ctype_base::space, true);
145 
146     test("upper", std::ctype_base::upper);
147     test("UPPER", std::ctype_base::upper);
148     test("upper", std::ctype_base::upper | std::ctype_base::alpha, true);
149     test("Upper", std::ctype_base::upper | std::ctype_base::alpha, true);
150 
151     test("xdigit", std::ctype_base::xdigit);
152     test("XDIGIT", std::ctype_base::xdigit);
153     test("xdigit", std::ctype_base::xdigit, true);
154     test("Xdigit", std::ctype_base::xdigit, true);
155 
156     test("dig", std::ctype_base::mask());
157     test("", std::ctype_base::mask());
158     test("digits", std::ctype_base::mask());
159 
160     test(L"d", std::ctype_base::digit);
161     test(L"D", std::ctype_base::digit);
162     test(L"d", std::ctype_base::digit, true);
163     test(L"D", std::ctype_base::digit, true);
164 
165     test_w(L"w", std::ctype_base::alnum
166                       | std::ctype_base::upper | std::ctype_base::lower);
167     test_w(L"W", std::ctype_base::alnum
168                       | std::ctype_base::upper | std::ctype_base::lower);
169     test_w(L"w", std::ctype_base::alnum
170                       | std::ctype_base::upper | std::ctype_base::lower, true);
171     test_w(L"W", std::ctype_base::alnum
172                       | std::ctype_base::upper | std::ctype_base::lower, true);
173 
174     test(L"s", std::ctype_base::space);
175     test(L"S", std::ctype_base::space);
176     test(L"s", std::ctype_base::space, true);
177     test(L"S", std::ctype_base::space, true);
178 
179     test(L"alnum", std::ctype_base::alnum);
180     test(L"AlNum", std::ctype_base::alnum);
181     test(L"alnum", std::ctype_base::alnum, true);
182     test(L"AlNum", std::ctype_base::alnum, true);
183 
184     test(L"alpha", std::ctype_base::alpha);
185     test(L"Alpha", std::ctype_base::alpha);
186     test(L"alpha", std::ctype_base::alpha, true);
187     test(L"Alpha", std::ctype_base::alpha, true);
188 
189     test(L"blank", std::ctype_base::blank);
190     test(L"Blank", std::ctype_base::blank);
191     test(L"blank", std::ctype_base::blank, true);
192     test(L"Blank", std::ctype_base::blank, true);
193 
194     test(L"cntrl", std::ctype_base::cntrl);
195     test(L"Cntrl", std::ctype_base::cntrl);
196     test(L"cntrl", std::ctype_base::cntrl, true);
197     test(L"Cntrl", std::ctype_base::cntrl, true);
198 
199     test(L"digit", std::ctype_base::digit);
200     test(L"Digit", std::ctype_base::digit);
201     test(L"digit", std::ctype_base::digit, true);
202     test(L"Digit", std::ctype_base::digit, true);
203 
204     test(L"digit", std::ctype_base::digit);
205     test(L"DIGIT", std::ctype_base::digit);
206     test(L"digit", std::ctype_base::digit, true);
207     test(L"Digit", std::ctype_base::digit, true);
208 
209     test(L"graph", std::ctype_base::graph);
210     test(L"GRAPH", std::ctype_base::graph);
211     test(L"graph", std::ctype_base::graph, true);
212     test(L"Graph", std::ctype_base::graph, true);
213 
214     test(L"lower", std::ctype_base::lower);
215     test(L"LOWER", std::ctype_base::lower);
216     test(L"lower", std::ctype_base::lower | std::ctype_base::alpha, true);
217     test(L"Lower", std::ctype_base::lower | std::ctype_base::alpha, true);
218 
219     test(L"print", std::ctype_base::print);
220     test(L"PRINT", std::ctype_base::print);
221     test(L"print", std::ctype_base::print, true);
222     test(L"Print", std::ctype_base::print, true);
223 
224     test(L"punct", std::ctype_base::punct);
225     test(L"PUNCT", std::ctype_base::punct);
226     test(L"punct", std::ctype_base::punct, true);
227     test(L"Punct", std::ctype_base::punct, true);
228 
229     test(L"space", std::ctype_base::space);
230     test(L"SPACE", std::ctype_base::space);
231     test(L"space", std::ctype_base::space, true);
232     test(L"Space", std::ctype_base::space, true);
233 
234     test(L"upper", std::ctype_base::upper);
235     test(L"UPPER", std::ctype_base::upper);
236     test(L"upper", std::ctype_base::upper | std::ctype_base::alpha, true);
237     test(L"Upper", std::ctype_base::upper | std::ctype_base::alpha, true);
238 
239     test(L"xdigit", std::ctype_base::xdigit);
240     test(L"XDIGIT", std::ctype_base::xdigit);
241     test(L"xdigit", std::ctype_base::xdigit, true);
242     test(L"Xdigit", std::ctype_base::xdigit, true);
243 
244     test(L"dig", std::ctype_base::mask());
245     test(L"", std::ctype_base::mask());
246     test(L"digits", std::ctype_base::mask());
247 }
248