]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * | |
3 | * Copyright (c) 2004 | |
4 | * John Maddock | |
5 | * | |
6 | * Use, modification and distribution are subject to the | |
7 | * Boost Software License, Version 1.0. (See accompanying file | |
8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | * | |
10 | */ | |
11 | ||
12 | /* | |
13 | * LOCATION: see http://www.boost.org for most recent version. | |
14 | * FILE w32_regex_traits.hpp | |
15 | * VERSION see <boost/version.hpp> | |
16 | * DESCRIPTION: Declares regular expression traits class w32_regex_traits. | |
17 | */ | |
18 | ||
19 | #ifndef BOOST_W32_REGEX_TRAITS_HPP_INCLUDED | |
20 | #define BOOST_W32_REGEX_TRAITS_HPP_INCLUDED | |
21 | ||
22 | #ifndef BOOST_REGEX_NO_WIN32_LOCALE | |
23 | ||
24 | #ifndef BOOST_RE_PAT_EXCEPT_HPP | |
25 | #include <boost/regex/pattern_except.hpp> | |
26 | #endif | |
27 | #ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED | |
28 | #include <boost/regex/v4/regex_traits_defaults.hpp> | |
29 | #endif | |
30 | #ifdef BOOST_HAS_THREADS | |
31 | #include <boost/regex/pending/static_mutex.hpp> | |
32 | #endif | |
33 | #ifndef BOOST_REGEX_PRIMARY_TRANSFORM | |
34 | #include <boost/regex/v4/primary_transform.hpp> | |
35 | #endif | |
36 | #ifndef BOOST_REGEX_OBJECT_CACHE_HPP | |
37 | #include <boost/regex/pending/object_cache.hpp> | |
38 | #endif | |
39 | ||
40 | #ifdef BOOST_MSVC | |
41 | #pragma warning(push) | |
42 | #pragma warning(disable: 4103) | |
43 | #endif | |
44 | #ifdef BOOST_HAS_ABI_HEADERS | |
45 | # include BOOST_ABI_PREFIX | |
46 | #endif | |
47 | #ifdef BOOST_MSVC | |
48 | #pragma warning(pop) | |
49 | #endif | |
50 | ||
51 | #ifdef BOOST_MSVC | |
52 | #pragma warning(push) | |
53 | #pragma warning(disable:4786) | |
92f5a8d4 | 54 | #if BOOST_MSVC < 1910 |
7c673cae FG |
55 | #pragma warning(disable:4800) |
56 | #endif | |
92f5a8d4 | 57 | #endif |
7c673cae FG |
58 | |
59 | namespace boost{ | |
60 | ||
61 | // | |
62 | // forward declaration is needed by some compilers: | |
63 | // | |
64 | template <class charT> | |
65 | class w32_regex_traits; | |
66 | ||
67 | namespace BOOST_REGEX_DETAIL_NS{ | |
68 | ||
69 | // | |
70 | // start by typedeffing the types we'll need: | |
71 | // | |
72 | typedef ::boost::uint32_t lcid_type; // placeholder for LCID. | |
73 | typedef ::boost::shared_ptr<void> cat_type; // placeholder for dll HANDLE. | |
74 | ||
75 | // | |
76 | // then add wrappers around the actual Win32 API's (ie implementation hiding): | |
77 | // | |
78 | BOOST_REGEX_DECL lcid_type BOOST_REGEX_CALL w32_get_default_locale(); | |
79 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(char, lcid_type); | |
80 | #ifndef BOOST_NO_WREGEX | |
81 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(wchar_t, lcid_type); | |
82 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
83 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_lower(unsigned short ca, lcid_type state_id); | |
84 | #endif | |
85 | #endif | |
86 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(char, lcid_type); | |
87 | #ifndef BOOST_NO_WREGEX | |
88 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(wchar_t, lcid_type); | |
89 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
90 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is_upper(unsigned short ca, lcid_type state_id); | |
91 | #endif | |
92 | #endif | |
93 | BOOST_REGEX_DECL cat_type BOOST_REGEX_CALL w32_cat_open(const std::string& name); | |
94 | BOOST_REGEX_DECL std::string BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type state_id, int i, const std::string& def); | |
95 | #ifndef BOOST_NO_WREGEX | |
96 | BOOST_REGEX_DECL std::wstring BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type state_id, int i, const std::wstring& def); | |
97 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
98 | BOOST_REGEX_DECL std::basic_string<unsigned short> BOOST_REGEX_CALL w32_cat_get(const cat_type& cat, lcid_type, int i, const std::basic_string<unsigned short>& def); | |
99 | #endif | |
100 | #endif | |
101 | BOOST_REGEX_DECL std::string BOOST_REGEX_CALL w32_transform(lcid_type state_id, const char* p1, const char* p2); | |
102 | #ifndef BOOST_NO_WREGEX | |
103 | BOOST_REGEX_DECL std::wstring BOOST_REGEX_CALL w32_transform(lcid_type state_id, const wchar_t* p1, const wchar_t* p2); | |
104 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
105 | BOOST_REGEX_DECL std::basic_string<unsigned short> BOOST_REGEX_CALL w32_transform(lcid_type state_id, const unsigned short* p1, const unsigned short* p2); | |
106 | #endif | |
107 | #endif | |
108 | BOOST_REGEX_DECL char BOOST_REGEX_CALL w32_tolower(char c, lcid_type); | |
109 | #ifndef BOOST_NO_WREGEX | |
110 | BOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_tolower(wchar_t c, lcid_type); | |
111 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
112 | BOOST_REGEX_DECL unsigned short BOOST_REGEX_CALL w32_tolower(unsigned short c, lcid_type state_id); | |
113 | #endif | |
114 | #endif | |
115 | BOOST_REGEX_DECL char BOOST_REGEX_CALL w32_toupper(char c, lcid_type); | |
116 | #ifndef BOOST_NO_WREGEX | |
117 | BOOST_REGEX_DECL wchar_t BOOST_REGEX_CALL w32_toupper(wchar_t c, lcid_type); | |
118 | #endif | |
119 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type, boost::uint32_t mask, char c); | |
120 | #ifndef BOOST_NO_WREGEX | |
121 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type, boost::uint32_t mask, wchar_t c); | |
122 | #ifdef BOOST_REGEX_HAS_OTHER_WCHAR_T | |
123 | BOOST_REGEX_DECL bool BOOST_REGEX_CALL w32_is(lcid_type state_id, boost::uint32_t m, unsigned short c); | |
124 | #endif | |
125 | #endif | |
126 | // | |
127 | // class w32_regex_traits_base: | |
128 | // acts as a container for locale and the facets we are using. | |
129 | // | |
130 | template <class charT> | |
131 | struct w32_regex_traits_base | |
132 | { | |
133 | w32_regex_traits_base(lcid_type l) | |
134 | { imbue(l); } | |
135 | lcid_type imbue(lcid_type l); | |
136 | ||
137 | lcid_type m_locale; | |
138 | }; | |
139 | ||
140 | template <class charT> | |
141 | inline lcid_type w32_regex_traits_base<charT>::imbue(lcid_type l) | |
142 | { | |
143 | lcid_type result(m_locale); | |
144 | m_locale = l; | |
145 | return result; | |
146 | } | |
147 | ||
148 | // | |
149 | // class w32_regex_traits_char_layer: | |
150 | // implements methods that require specialisation for narrow characters: | |
151 | // | |
152 | template <class charT> | |
153 | class w32_regex_traits_char_layer : public w32_regex_traits_base<charT> | |
154 | { | |
155 | typedef std::basic_string<charT> string_type; | |
156 | typedef std::map<charT, regex_constants::syntax_type> map_type; | |
157 | typedef typename map_type::const_iterator map_iterator_type; | |
158 | public: | |
159 | w32_regex_traits_char_layer(const lcid_type l); | |
160 | ||
161 | regex_constants::syntax_type syntax_type(charT c)const | |
162 | { | |
163 | map_iterator_type i = m_char_map.find(c); | |
164 | return ((i == m_char_map.end()) ? 0 : i->second); | |
165 | } | |
166 | regex_constants::escape_syntax_type escape_syntax_type(charT c) const | |
167 | { | |
168 | map_iterator_type i = m_char_map.find(c); | |
169 | if(i == m_char_map.end()) | |
170 | { | |
171 | if(::boost::BOOST_REGEX_DETAIL_NS::w32_is_lower(c, this->m_locale)) return regex_constants::escape_type_class; | |
172 | if(::boost::BOOST_REGEX_DETAIL_NS::w32_is_upper(c, this->m_locale)) return regex_constants::escape_type_not_class; | |
173 | return 0; | |
174 | } | |
175 | return i->second; | |
176 | } | |
177 | charT tolower(charT c)const | |
178 | { | |
179 | return ::boost::BOOST_REGEX_DETAIL_NS::w32_tolower(c, this->m_locale); | |
180 | } | |
181 | bool isctype(boost::uint32_t mask, charT c)const | |
182 | { | |
183 | return ::boost::BOOST_REGEX_DETAIL_NS::w32_is(this->m_locale, mask, c); | |
184 | } | |
185 | ||
186 | private: | |
187 | string_type get_default_message(regex_constants::syntax_type); | |
188 | // TODO: use a hash table when available! | |
189 | map_type m_char_map; | |
190 | }; | |
191 | ||
192 | template <class charT> | |
193 | w32_regex_traits_char_layer<charT>::w32_regex_traits_char_layer(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l) | |
194 | : w32_regex_traits_base<charT>(l) | |
195 | { | |
196 | // we need to start by initialising our syntax map so we know which | |
197 | // character is used for which purpose: | |
198 | cat_type cat; | |
199 | std::string cat_name(w32_regex_traits<charT>::get_catalog_name()); | |
200 | if(cat_name.size()) | |
201 | { | |
202 | cat = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_open(cat_name); | |
203 | if(!cat) | |
204 | { | |
205 | std::string m("Unable to open message catalog: "); | |
206 | std::runtime_error err(m + cat_name); | |
207 | boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err); | |
208 | } | |
209 | } | |
210 | // | |
211 | // if we have a valid catalog then load our messages: | |
212 | // | |
213 | if(cat) | |
214 | { | |
215 | for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i) | |
216 | { | |
217 | string_type mss = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, i, get_default_message(i)); | |
218 | for(typename string_type::size_type j = 0; j < mss.size(); ++j) | |
219 | { | |
220 | this->m_char_map[mss[j]] = i; | |
221 | } | |
222 | } | |
223 | } | |
224 | else | |
225 | { | |
226 | for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i) | |
227 | { | |
228 | const char* ptr = get_default_syntax(i); | |
229 | while(ptr && *ptr) | |
230 | { | |
231 | this->m_char_map[static_cast<charT>(*ptr)] = i; | |
232 | ++ptr; | |
233 | } | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
238 | template <class charT> | |
239 | typename w32_regex_traits_char_layer<charT>::string_type | |
240 | w32_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i) | |
241 | { | |
242 | const char* ptr = get_default_syntax(i); | |
243 | string_type result; | |
244 | while(ptr && *ptr) | |
245 | { | |
246 | result.append(1, static_cast<charT>(*ptr)); | |
247 | ++ptr; | |
248 | } | |
249 | return result; | |
250 | } | |
251 | ||
252 | // | |
253 | // specialised version for narrow characters: | |
254 | // | |
255 | template <> | |
256 | class BOOST_REGEX_DECL w32_regex_traits_char_layer<char> : public w32_regex_traits_base<char> | |
257 | { | |
258 | typedef std::string string_type; | |
259 | public: | |
260 | w32_regex_traits_char_layer(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l) | |
261 | : w32_regex_traits_base<char>(l) | |
262 | { | |
263 | init(); | |
264 | } | |
265 | ||
266 | regex_constants::syntax_type syntax_type(char c)const | |
267 | { | |
268 | return m_char_map[static_cast<unsigned char>(c)]; | |
269 | } | |
270 | regex_constants::escape_syntax_type escape_syntax_type(char c) const | |
271 | { | |
272 | return m_char_map[static_cast<unsigned char>(c)]; | |
273 | } | |
274 | char tolower(char c)const | |
275 | { | |
276 | return m_lower_map[static_cast<unsigned char>(c)]; | |
277 | } | |
278 | bool isctype(boost::uint32_t mask, char c)const | |
279 | { | |
280 | return m_type_map[static_cast<unsigned char>(c)] & mask; | |
281 | } | |
282 | ||
283 | private: | |
284 | regex_constants::syntax_type m_char_map[1u << CHAR_BIT]; | |
285 | char m_lower_map[1u << CHAR_BIT]; | |
286 | boost::uint16_t m_type_map[1u << CHAR_BIT]; | |
287 | void init(); | |
288 | }; | |
289 | ||
290 | // | |
291 | // class w32_regex_traits_implementation: | |
292 | // provides pimpl implementation for w32_regex_traits. | |
293 | // | |
294 | template <class charT> | |
295 | class w32_regex_traits_implementation : public w32_regex_traits_char_layer<charT> | |
296 | { | |
297 | public: | |
298 | typedef typename w32_regex_traits<charT>::char_class_type char_class_type; | |
299 | BOOST_STATIC_CONSTANT(char_class_type, mask_word = 0x0400); // must be C1_DEFINED << 1 | |
300 | BOOST_STATIC_CONSTANT(char_class_type, mask_unicode = 0x0800); // must be C1_DEFINED << 2 | |
301 | BOOST_STATIC_CONSTANT(char_class_type, mask_horizontal = 0x1000); // must be C1_DEFINED << 3 | |
302 | BOOST_STATIC_CONSTANT(char_class_type, mask_vertical = 0x2000); // must be C1_DEFINED << 4 | |
303 | BOOST_STATIC_CONSTANT(char_class_type, mask_base = 0x3ff); // all the masks used by the CT_CTYPE1 group | |
304 | ||
305 | typedef std::basic_string<charT> string_type; | |
306 | typedef charT char_type; | |
307 | w32_regex_traits_implementation(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l); | |
308 | std::string error_string(regex_constants::error_type n) const | |
309 | { | |
310 | if(!m_error_strings.empty()) | |
311 | { | |
312 | std::map<int, std::string>::const_iterator p = m_error_strings.find(n); | |
313 | return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second; | |
314 | } | |
315 | return get_default_error_string(n); | |
316 | } | |
317 | char_class_type lookup_classname(const charT* p1, const charT* p2) const | |
318 | { | |
319 | char_class_type result = lookup_classname_imp(p1, p2); | |
320 | if(result == 0) | |
321 | { | |
322 | typedef typename string_type::size_type size_type; | |
323 | string_type temp(p1, p2); | |
324 | for(size_type i = 0; i < temp.size(); ++i) | |
325 | temp[i] = this->tolower(temp[i]); | |
326 | result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size()); | |
327 | } | |
328 | return result; | |
329 | } | |
330 | string_type lookup_collatename(const charT* p1, const charT* p2) const; | |
331 | string_type transform_primary(const charT* p1, const charT* p2) const; | |
332 | string_type transform(const charT* p1, const charT* p2) const | |
333 | { | |
334 | return ::boost::BOOST_REGEX_DETAIL_NS::w32_transform(this->m_locale, p1, p2); | |
335 | } | |
336 | private: | |
337 | std::map<int, std::string> m_error_strings; // error messages indexed by numberic ID | |
338 | std::map<string_type, char_class_type> m_custom_class_names; // character class names | |
339 | std::map<string_type, string_type> m_custom_collate_names; // collating element names | |
340 | unsigned m_collate_type; // the form of the collation string | |
341 | charT m_collate_delim; // the collation group delimiter | |
342 | // | |
343 | // helpers: | |
344 | // | |
345 | char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const; | |
346 | }; | |
347 | ||
348 | template <class charT> | |
349 | typename w32_regex_traits_implementation<charT>::string_type | |
350 | w32_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const | |
351 | { | |
352 | string_type result; | |
353 | // | |
354 | // What we do here depends upon the format of the sort key returned by | |
355 | // sort key returned by this->transform: | |
356 | // | |
357 | switch(m_collate_type) | |
358 | { | |
359 | case sort_C: | |
360 | case sort_unknown: | |
361 | // the best we can do is translate to lower case, then get a regular sort key: | |
362 | { | |
363 | result.assign(p1, p2); | |
364 | typedef typename string_type::size_type size_type; | |
365 | for(size_type i = 0; i < result.size(); ++i) | |
366 | result[i] = this->tolower(result[i]); | |
367 | result = this->transform(&*result.begin(), &*result.begin() + result.size()); | |
368 | break; | |
369 | } | |
370 | case sort_fixed: | |
371 | { | |
372 | // get a regular sort key, and then truncate it: | |
373 | result.assign(this->transform(p1, p2)); | |
374 | result.erase(this->m_collate_delim); | |
375 | break; | |
376 | } | |
377 | case sort_delim: | |
378 | // get a regular sort key, and then truncate everything after the delim: | |
379 | result.assign(this->transform(p1, p2)); | |
380 | std::size_t i; | |
381 | for(i = 0; i < result.size(); ++i) | |
382 | { | |
383 | if(result[i] == m_collate_delim) | |
384 | break; | |
385 | } | |
386 | result.erase(i); | |
387 | break; | |
388 | } | |
389 | if(result.empty()) | |
390 | result = string_type(1, charT(0)); | |
391 | return result; | |
392 | } | |
393 | ||
394 | template <class charT> | |
395 | typename w32_regex_traits_implementation<charT>::string_type | |
396 | w32_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const | |
397 | { | |
398 | typedef typename std::map<string_type, string_type>::const_iterator iter_type; | |
399 | if(m_custom_collate_names.size()) | |
400 | { | |
401 | iter_type pos = m_custom_collate_names.find(string_type(p1, p2)); | |
402 | if(pos != m_custom_collate_names.end()) | |
403 | return pos->second; | |
404 | } | |
405 | #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\ | |
406 | && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551) | |
407 | std::string name(p1, p2); | |
408 | #else | |
409 | std::string name; | |
410 | const charT* p0 = p1; | |
411 | while(p0 != p2) | |
412 | name.append(1, char(*p0++)); | |
413 | #endif | |
414 | name = lookup_default_collate_name(name); | |
415 | #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\ | |
416 | && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551) | |
417 | if(name.size()) | |
418 | return string_type(name.begin(), name.end()); | |
419 | #else | |
420 | if(name.size()) | |
421 | { | |
422 | string_type result; | |
423 | typedef std::string::const_iterator iter; | |
424 | iter b = name.begin(); | |
425 | iter e = name.end(); | |
426 | while(b != e) | |
427 | result.append(1, charT(*b++)); | |
428 | return result; | |
429 | } | |
430 | #endif | |
431 | if(p2 - p1 == 1) | |
432 | return string_type(1, *p1); | |
433 | return string_type(); | |
434 | } | |
435 | ||
436 | template <class charT> | |
437 | w32_regex_traits_implementation<charT>::w32_regex_traits_implementation(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l) | |
438 | : w32_regex_traits_char_layer<charT>(l) | |
439 | { | |
440 | cat_type cat; | |
441 | std::string cat_name(w32_regex_traits<charT>::get_catalog_name()); | |
442 | if(cat_name.size()) | |
443 | { | |
444 | cat = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_open(cat_name); | |
445 | if(!cat) | |
446 | { | |
447 | std::string m("Unable to open message catalog: "); | |
448 | std::runtime_error err(m + cat_name); | |
449 | boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err); | |
450 | } | |
451 | } | |
452 | // | |
453 | // if we have a valid catalog then load our messages: | |
454 | // | |
455 | if(cat) | |
456 | { | |
457 | // | |
458 | // Error messages: | |
459 | // | |
460 | for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0); | |
461 | i <= boost::regex_constants::error_unknown; | |
462 | i = static_cast<boost::regex_constants::error_type>(i + 1)) | |
463 | { | |
464 | const char* p = get_default_error_string(i); | |
465 | string_type default_message; | |
466 | while(*p) | |
467 | { | |
468 | default_message.append(1, static_cast<charT>(*p)); | |
469 | ++p; | |
470 | } | |
471 | string_type s = ::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, i+200, default_message); | |
472 | std::string result; | |
473 | for(std::string::size_type j = 0; j < s.size(); ++j) | |
474 | { | |
475 | result.append(1, static_cast<char>(s[j])); | |
476 | } | |
477 | m_error_strings[i] = result; | |
478 | } | |
479 | // | |
480 | // Custom class names: | |
481 | // | |
482 | static const char_class_type masks[14] = | |
483 | { | |
484 | 0x0104u, // C1_ALPHA | C1_DIGIT | |
485 | 0x0100u, // C1_ALPHA | |
486 | 0x0020u, // C1_CNTRL | |
487 | 0x0004u, // C1_DIGIT | |
488 | (~(0x0020u|0x0008u) & 0x01ffu) | 0x0400u, // not C1_CNTRL or C1_SPACE | |
489 | 0x0002u, // C1_LOWER | |
490 | (~0x0020u & 0x01ffu) | 0x0400, // not C1_CNTRL | |
491 | 0x0010u, // C1_PUNCT | |
492 | 0x0008u, // C1_SPACE | |
493 | 0x0001u, // C1_UPPER | |
494 | 0x0080u, // C1_XDIGIT | |
495 | 0x0040u, // C1_BLANK | |
496 | w32_regex_traits_implementation<charT>::mask_word, | |
497 | w32_regex_traits_implementation<charT>::mask_unicode, | |
498 | }; | |
499 | static const string_type null_string; | |
500 | for(unsigned int j = 0; j <= 13; ++j) | |
501 | { | |
502 | string_type s(::boost::BOOST_REGEX_DETAIL_NS::w32_cat_get(cat, this->m_locale, j+300, null_string)); | |
503 | if(s.size()) | |
504 | this->m_custom_class_names[s] = masks[j]; | |
505 | } | |
506 | } | |
507 | // | |
508 | // get the collation format used by m_pcollate: | |
509 | // | |
510 | m_collate_type = BOOST_REGEX_DETAIL_NS::find_sort_syntax(this, &m_collate_delim); | |
511 | } | |
512 | ||
513 | template <class charT> | |
514 | typename w32_regex_traits_implementation<charT>::char_class_type | |
515 | w32_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const | |
516 | { | |
517 | static const char_class_type masks[22] = | |
518 | { | |
519 | 0, | |
520 | 0x0104u, // C1_ALPHA | C1_DIGIT | |
521 | 0x0100u, // C1_ALPHA | |
522 | 0x0040u, // C1_BLANK | |
523 | 0x0020u, // C1_CNTRL | |
524 | 0x0004u, // C1_DIGIT | |
525 | 0x0004u, // C1_DIGIT | |
526 | (~(0x0020u|0x0008u|0x0040) & 0x01ffu) | 0x0400u, // not C1_CNTRL or C1_SPACE or C1_BLANK | |
527 | w32_regex_traits_implementation<charT>::mask_horizontal, | |
528 | 0x0002u, // C1_LOWER | |
529 | 0x0002u, // C1_LOWER | |
530 | (~0x0020u & 0x01ffu) | 0x0400, // not C1_CNTRL | |
531 | 0x0010u, // C1_PUNCT | |
532 | 0x0008u, // C1_SPACE | |
533 | 0x0008u, // C1_SPACE | |
534 | 0x0001u, // C1_UPPER | |
535 | w32_regex_traits_implementation<charT>::mask_unicode, | |
536 | 0x0001u, // C1_UPPER | |
537 | w32_regex_traits_implementation<charT>::mask_vertical, | |
538 | 0x0104u | w32_regex_traits_implementation<charT>::mask_word, | |
539 | 0x0104u | w32_regex_traits_implementation<charT>::mask_word, | |
540 | 0x0080u, // C1_XDIGIT | |
541 | }; | |
542 | if(m_custom_class_names.size()) | |
543 | { | |
544 | typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter; | |
545 | map_iter pos = m_custom_class_names.find(string_type(p1, p2)); | |
546 | if(pos != m_custom_class_names.end()) | |
547 | return pos->second; | |
548 | } | |
549 | std::size_t state_id = 1 + BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2); | |
550 | if(state_id < sizeof(masks) / sizeof(masks[0])) | |
551 | return masks[state_id]; | |
552 | return masks[0]; | |
553 | } | |
554 | ||
555 | ||
556 | template <class charT> | |
557 | boost::shared_ptr<const w32_regex_traits_implementation<charT> > create_w32_regex_traits(::boost::BOOST_REGEX_DETAIL_NS::lcid_type l) | |
558 | { | |
559 | // TODO: create a cache for previously constructed objects. | |
560 | return boost::object_cache< ::boost::BOOST_REGEX_DETAIL_NS::lcid_type, w32_regex_traits_implementation<charT> >::get(l, 5); | |
561 | } | |
562 | ||
563 | } // BOOST_REGEX_DETAIL_NS | |
564 | ||
565 | template <class charT> | |
566 | class w32_regex_traits | |
567 | { | |
568 | public: | |
569 | typedef charT char_type; | |
570 | typedef std::size_t size_type; | |
571 | typedef std::basic_string<char_type> string_type; | |
572 | typedef ::boost::BOOST_REGEX_DETAIL_NS::lcid_type locale_type; | |
573 | typedef boost::uint_least32_t char_class_type; | |
574 | ||
575 | struct boost_extensions_tag{}; | |
576 | ||
577 | w32_regex_traits() | |
578 | : m_pimpl(BOOST_REGEX_DETAIL_NS::create_w32_regex_traits<charT>(::boost::BOOST_REGEX_DETAIL_NS::w32_get_default_locale())) | |
579 | { } | |
580 | static size_type length(const char_type* p) | |
581 | { | |
582 | return std::char_traits<charT>::length(p); | |
583 | } | |
584 | regex_constants::syntax_type syntax_type(charT c)const | |
585 | { | |
586 | return m_pimpl->syntax_type(c); | |
587 | } | |
588 | regex_constants::escape_syntax_type escape_syntax_type(charT c) const | |
589 | { | |
590 | return m_pimpl->escape_syntax_type(c); | |
591 | } | |
592 | charT translate(charT c) const | |
593 | { | |
594 | return c; | |
595 | } | |
596 | charT translate_nocase(charT c) const | |
597 | { | |
598 | return this->m_pimpl->tolower(c); | |
599 | } | |
600 | charT translate(charT c, bool icase) const | |
601 | { | |
602 | return icase ? this->m_pimpl->tolower(c) : c; | |
603 | } | |
604 | charT tolower(charT c) const | |
605 | { | |
606 | return this->m_pimpl->tolower(c); | |
607 | } | |
608 | charT toupper(charT c) const | |
609 | { | |
610 | return ::boost::BOOST_REGEX_DETAIL_NS::w32_toupper(c, this->m_pimpl->m_locale); | |
611 | } | |
612 | string_type transform(const charT* p1, const charT* p2) const | |
613 | { | |
614 | return ::boost::BOOST_REGEX_DETAIL_NS::w32_transform(this->m_pimpl->m_locale, p1, p2); | |
615 | } | |
616 | string_type transform_primary(const charT* p1, const charT* p2) const | |
617 | { | |
618 | return m_pimpl->transform_primary(p1, p2); | |
619 | } | |
620 | char_class_type lookup_classname(const charT* p1, const charT* p2) const | |
621 | { | |
622 | return m_pimpl->lookup_classname(p1, p2); | |
623 | } | |
624 | string_type lookup_collatename(const charT* p1, const charT* p2) const | |
625 | { | |
626 | return m_pimpl->lookup_collatename(p1, p2); | |
627 | } | |
628 | bool isctype(charT c, char_class_type f) const | |
629 | { | |
630 | if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_base) | |
631 | && (this->m_pimpl->isctype(f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_base, c))) | |
632 | return true; | |
633 | else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_unicode) && BOOST_REGEX_DETAIL_NS::is_extended(c)) | |
634 | return true; | |
635 | else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_word) && (c == '_')) | |
636 | return true; | |
637 | else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_vertical) | |
638 | && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == '\v'))) | |
639 | return true; | |
640 | else if((f & BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_horizontal) | |
641 | && this->isctype(c, 0x0008u) && !this->isctype(c, BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT>::mask_vertical)) | |
642 | return true; | |
643 | return false; | |
644 | } | |
645 | boost::intmax_t toi(const charT*& p1, const charT* p2, int radix)const | |
646 | { | |
647 | return ::boost::BOOST_REGEX_DETAIL_NS::global_toi(p1, p2, radix, *this); | |
648 | } | |
649 | int value(charT c, int radix)const | |
650 | { | |
651 | int result = (int)::boost::BOOST_REGEX_DETAIL_NS::global_value(c); | |
652 | return result < radix ? result : -1; | |
653 | } | |
654 | locale_type imbue(locale_type l) | |
655 | { | |
656 | ::boost::BOOST_REGEX_DETAIL_NS::lcid_type result(getloc()); | |
657 | m_pimpl = BOOST_REGEX_DETAIL_NS::create_w32_regex_traits<charT>(l); | |
658 | return result; | |
659 | } | |
660 | locale_type getloc()const | |
661 | { | |
662 | return m_pimpl->m_locale; | |
663 | } | |
664 | std::string error_string(regex_constants::error_type n) const | |
665 | { | |
666 | return m_pimpl->error_string(n); | |
667 | } | |
668 | ||
669 | // | |
670 | // extension: | |
671 | // set the name of the message catalog in use (defaults to "boost_regex"). | |
672 | // | |
673 | static std::string catalog_name(const std::string& name); | |
674 | static std::string get_catalog_name(); | |
675 | ||
676 | private: | |
677 | boost::shared_ptr<const BOOST_REGEX_DETAIL_NS::w32_regex_traits_implementation<charT> > m_pimpl; | |
678 | // | |
679 | // catalog name handler: | |
680 | // | |
681 | static std::string& get_catalog_name_inst(); | |
682 | ||
683 | #ifdef BOOST_HAS_THREADS | |
684 | static static_mutex& get_mutex_inst(); | |
685 | #endif | |
686 | }; | |
687 | ||
688 | template <class charT> | |
689 | std::string w32_regex_traits<charT>::catalog_name(const std::string& name) | |
690 | { | |
691 | #ifdef BOOST_HAS_THREADS | |
692 | static_mutex::scoped_lock lk(get_mutex_inst()); | |
693 | #endif | |
694 | std::string result(get_catalog_name_inst()); | |
695 | get_catalog_name_inst() = name; | |
696 | return result; | |
697 | } | |
698 | ||
699 | template <class charT> | |
700 | std::string& w32_regex_traits<charT>::get_catalog_name_inst() | |
701 | { | |
702 | static std::string s_name; | |
703 | return s_name; | |
704 | } | |
705 | ||
706 | template <class charT> | |
707 | std::string w32_regex_traits<charT>::get_catalog_name() | |
708 | { | |
709 | #ifdef BOOST_HAS_THREADS | |
710 | static_mutex::scoped_lock lk(get_mutex_inst()); | |
711 | #endif | |
712 | std::string result(get_catalog_name_inst()); | |
713 | return result; | |
714 | } | |
715 | ||
716 | #ifdef BOOST_HAS_THREADS | |
717 | template <class charT> | |
718 | static_mutex& w32_regex_traits<charT>::get_mutex_inst() | |
719 | { | |
720 | static static_mutex s_mutex = BOOST_STATIC_MUTEX_INIT; | |
721 | return s_mutex; | |
722 | } | |
723 | #endif | |
724 | ||
725 | ||
726 | } // boost | |
727 | ||
728 | #ifdef BOOST_MSVC | |
729 | #pragma warning(pop) | |
730 | #endif | |
731 | ||
732 | #ifdef BOOST_MSVC | |
733 | #pragma warning(push) | |
734 | #pragma warning(disable: 4103) | |
735 | #endif | |
736 | #ifdef BOOST_HAS_ABI_HEADERS | |
737 | # include BOOST_ABI_SUFFIX | |
738 | #endif | |
739 | #ifdef BOOST_MSVC | |
740 | #pragma warning(pop) | |
741 | #endif | |
742 | ||
743 | #endif // BOOST_REGEX_NO_WIN32_LOCALE | |
744 | ||
745 | #endif |