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