source: NonGTP/Boost/boost/regex/v4/cpp_regex_traits.hpp @ 857

Revision 857, 32.9 KB checked in by igarcia, 18 years ago (diff)
Line 
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         cpp_regex_traits.hpp
15  *   VERSION      see <boost/version.hpp>
16  *   DESCRIPTION: Declares regular expression traits class cpp_regex_traits.
17  */
18
19#ifndef BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
20#define BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
21
22#include <boost/config.hpp>
23
24#ifndef BOOST_NO_STD_LOCALE
25
26#ifndef BOOST_RE_PAT_EXCEPT_HPP
27#include <boost/regex/pattern_except.hpp>
28#endif
29#ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED
30#include <boost/regex/v4/regex_traits_defaults.hpp>
31#endif
32#ifdef BOOST_HAS_THREADS
33#include <boost/regex/pending/static_mutex.hpp>
34#endif
35#ifndef BOOST_REGEX_PRIMARY_TRANSFORM
36#include <boost/regex/v4/primary_transform.hpp>
37#endif
38#ifndef BOOST_REGEX_OBJECT_CACHE_HPP
39#include <boost/regex/pending/object_cache.hpp>
40#endif
41
42#include <istream>
43#include <ios>
44
45#ifdef BOOST_HAS_ABI_HEADERS
46#  include BOOST_ABI_PREFIX
47#endif
48
49#ifdef BOOST_MSVC
50#pragma warning(push)
51#pragma warning(disable:4786)
52#endif
53
54namespace boost{
55
56//
57// forward declaration is needed by some compilers:
58//
59template <class charT>
60class cpp_regex_traits;
61   
62namespace re_detail{
63
64//
65// class parser_buf:
66// acts as a stream buffer which wraps around a pair of pointers:
67//
68template <class charT,
69          class traits = ::std::char_traits<charT> >
70class parser_buf : public ::std::basic_streambuf<charT, traits>
71{
72   typedef ::std::basic_streambuf<charT, traits> base_type;
73   typedef typename base_type::int_type int_type;
74   typedef typename base_type::char_type char_type;
75   typedef typename base_type::pos_type pos_type;
76   typedef ::std::streamsize streamsize;
77   typedef typename base_type::off_type off_type;
78public:
79   parser_buf() : base_type() { setbuf(0, 0); }
80   const charT* getnext() { return this->gptr(); }
81protected:
82   std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
83   typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
84   typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
85private:
86   parser_buf& operator=(const parser_buf&);
87   parser_buf(const parser_buf&);
88};
89
90template<class charT, class traits>
91std::basic_streambuf<charT, traits>*
92parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
93{
94   this->setg(s, s, s + n);
95   return this;
96}
97
98template<class charT, class traits>
99typename parser_buf<charT, traits>::pos_type
100parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
101{
102   if(which & ::std::ios_base::out)
103      return pos_type(off_type(-1));
104   std::ptrdiff_t size = this->egptr() - this->eback();
105   std::ptrdiff_t pos = this->gptr() - this->eback();
106   charT* g = this->eback();
107   switch(way)
108   {
109   case ::std::ios_base::beg:
110      if((off < 0) || (off > size))
111         return pos_type(off_type(-1));
112      else
113         this->setg(g, g + off, g + size);
114      break;
115   case ::std::ios_base::end:
116      if((off < 0) || (off > size))
117         return pos_type(off_type(-1));
118      else
119         this->setg(g, g + size - off, g + size);
120      break;
121   case ::std::ios_base::cur:
122   {
123      std::ptrdiff_t newpos = pos + off;
124      if((newpos < 0) || (newpos > size))
125         return pos_type(off_type(-1));
126      else
127         this->setg(g, g + newpos, g + size);
128      break;
129   }
130   default: ;
131   }
132#ifdef BOOST_MSVC
133#pragma warning(push)
134#pragma warning(disable:4244)
135#endif
136   return static_cast<pos_type>(this->gptr() - this->eback());
137#ifdef BOOST_MSVC
138#pragma warning(pop)
139#endif
140}
141
142template<class charT, class traits>
143typename parser_buf<charT, traits>::pos_type
144parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
145{
146   if(which & ::std::ios_base::out)
147      return pos_type(off_type(-1));
148   off_type size = static_cast<off_type>(this->egptr() - this->eback());
149   charT* g = this->eback();
150   if(off_type(sp) <= size)
151   {
152      this->setg(g, g + off_type(sp), g + size);
153   }
154   return pos_type(off_type(-1));
155}
156
157//
158// class cpp_regex_traits_base:
159// acts as a container for locale and the facets we are using.
160//
161template <class charT>
162struct cpp_regex_traits_base
163{
164   cpp_regex_traits_base(const std::locale& l)
165   { imbue(l); }
166   std::locale imbue(const std::locale& l);
167
168   std::locale m_locale;
169   std::ctype<charT> const* m_pctype;
170#ifndef BOOST_NO_STD_MESSAGES
171   std::messages<charT> const* m_pmessages;
172#endif
173   std::collate<charT> const* m_pcollate;
174
175   bool operator<(const cpp_regex_traits_base& b)const
176   {
177      if(m_pctype == b.m_pctype)
178      {
179#ifndef BOOST_NO_STD_MESSAGES
180         if(m_pmessages == b.m_pmessages)
181         {
182         }
183         return m_pmessages < b.m_pmessages;
184#else
185         return m_pcollate < b.m_pcollate;
186#endif
187      }
188      return m_pctype < b.m_pctype;
189   }
190   bool operator==(const cpp_regex_traits_base& b)const
191   {
192      return (m_pctype == b.m_pctype)
193#ifndef BOOST_NO_STD_MESSAGES
194         && (m_pmessages == b.m_pmessages)
195#endif
196         && (m_pcollate == b.m_pcollate);
197   }
198};
199
200template <class charT>
201std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
202{
203   std::locale result(m_locale);
204   m_locale = l;
205   m_pctype = &BOOST_USE_FACET(std::ctype<charT>, l);
206#ifndef BOOST_NO_STD_MESSAGES
207   m_pmessages = &BOOST_USE_FACET(std::messages<charT>, l);
208#endif
209   m_pcollate = &BOOST_USE_FACET(std::collate<charT>, l);
210   return result;
211}
212
213//
214// class cpp_regex_traits_char_layer:
215// implements methods that require specialisation for narrow characters:
216//
217template <class charT>
218class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
219{
220   typedef std::basic_string<charT> string_type;
221   typedef std::map<charT, regex_constants::syntax_type> map_type;
222   typedef typename map_type::const_iterator map_iterator_type;
223public:
224   cpp_regex_traits_char_layer(const std::locale& l)
225      : cpp_regex_traits_base<charT>(l)
226   {
227      init();
228   }
229   cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
230      : cpp_regex_traits_base<charT>(b)
231   {
232      init();
233   }
234   void init();
235
236   regex_constants::syntax_type syntax_type(charT c)const
237   {
238      map_iterator_type i = m_char_map.find(c);
239      return ((i == m_char_map.end()) ? 0 : i->second);
240   }
241   regex_constants::escape_syntax_type escape_syntax_type(charT c) const
242   {
243      map_iterator_type i = m_char_map.find(c);
244      if(i == m_char_map.end())
245      {
246         if(this->m_pctype->is(std::ctype_base::lower, c)) return regex_constants::escape_type_class;
247         if(this->m_pctype->is(std::ctype_base::upper, c)) return regex_constants::escape_type_not_class;
248         return 0;
249      }
250      return i->second;
251   }
252
253private:
254   string_type get_default_message(regex_constants::syntax_type);
255   // TODO: use a hash table when available!
256   map_type m_char_map;
257};
258
259template <class charT>
260void cpp_regex_traits_char_layer<charT>::init()
261{
262   // we need to start by initialising our syntax map so we know which
263   // character is used for which purpose:
264#ifndef BOOST_NO_STD_MESSAGES
265#ifndef __IBMCPP__
266   typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
267#else
268   typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
269#endif
270   std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
271   if(cat_name.size())
272   {
273      cat = this->m_pmessages->open(
274         cat_name,
275         this->m_locale);
276      if((int)cat < 0)
277      {
278         std::string m("Unable to open message catalog: ");
279         std::runtime_error err(m + cat_name);
280         boost::re_detail::raise_runtime_error(err);
281      }
282   }
283   //
284   // if we have a valid catalog then load our messages:
285   //
286   if((int)cat >= 0)
287   {
288      try{
289         for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
290         {
291            string_type mss = this->m_pmessages->get(cat, 0, i, get_default_message(i));
292            for(typename string_type::size_type j = 0; j < mss.size(); ++j)
293            {
294               m_char_map[mss[j]] = i;
295            }
296         }
297         this->m_pmessages->close(cat);
298      }
299      catch(...)
300      {
301         this->m_pmessages->close(cat);
302         throw;
303      }
304   }
305   else
306   {
307#endif
308      for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
309      {
310         const char* ptr = get_default_syntax(i);
311         while(ptr && *ptr)
312         {
313            m_char_map[this->m_pctype->widen(*ptr)] = i;
314            ++ptr;
315         }
316      }
317#ifndef BOOST_NO_STD_MESSAGES
318   }
319#endif
320}
321
322template <class charT>
323typename cpp_regex_traits_char_layer<charT>::string_type
324   cpp_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
325{
326   const char* ptr = get_default_syntax(i);
327   string_type result;
328   while(ptr && *ptr)
329   {
330      result.append(1, this->m_pctype->widen(*ptr));
331      ++ptr;
332   }
333   return result;
334}
335
336//
337// specialised version for narrow characters:
338//
339template <>
340class BOOST_REGEX_DECL cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
341{
342   typedef std::string string_type;
343public:
344   cpp_regex_traits_char_layer(const std::locale& l)
345   : cpp_regex_traits_base<char>(l)
346   {
347      init();
348   }
349   cpp_regex_traits_char_layer(const cpp_regex_traits_base<char>& l)
350   : cpp_regex_traits_base<char>(l)
351   {
352      init();
353   }
354
355   regex_constants::syntax_type syntax_type(char c)const
356   {
357      return m_char_map[static_cast<unsigned char>(c)];
358   }
359   regex_constants::escape_syntax_type escape_syntax_type(char c) const
360   {
361      return m_char_map[static_cast<unsigned char>(c)];
362   }
363
364private:
365   regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
366   void init();
367};
368
369#ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
370enum
371{
372   char_class_space=1<<0,
373   char_class_print=1<<1,
374   char_class_cntrl=1<<2,
375   char_class_upper=1<<3,
376   char_class_lower=1<<4,
377   char_class_alpha=1<<5,
378   char_class_digit=1<<6,
379   char_class_punct=1<<7,
380   char_class_xdigit=1<<8,
381   char_class_alnum=char_class_alpha|char_class_digit,
382   char_class_graph=char_class_alnum|char_class_punct,
383   char_class_blank=1<<9,
384   char_class_word=1<<10,
385   char_class_unicode=1<<11
386};
387
388#endif
389
390//
391// class cpp_regex_traits_implementation:
392// provides pimpl implementation for cpp_regex_traits.
393//
394template <class charT>
395class cpp_regex_traits_implementation : public cpp_regex_traits_char_layer<charT>
396{
397public:
398   typedef typename cpp_regex_traits<charT>::char_class_type char_class_type;
399   typedef typename std::ctype<charT>::mask                  native_mask_type;
400#ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
401   BOOST_STATIC_CONSTANT(char_class_type, mask_blank = 1u << 24);
402   BOOST_STATIC_CONSTANT(char_class_type, mask_word = 1u << 25);
403   BOOST_STATIC_CONSTANT(char_class_type, mask_unicode = 1u << 26);
404#endif
405
406   typedef std::basic_string<charT> string_type;
407   typedef charT char_type;
408   //cpp_regex_traits_implementation();
409   cpp_regex_traits_implementation(const std::locale& l)
410      : cpp_regex_traits_char_layer<charT>(l), m_is(&m_sbuf)
411   {
412      init();
413   }
414   cpp_regex_traits_implementation(const cpp_regex_traits_base<charT>& l)
415      : cpp_regex_traits_char_layer<charT>(l), m_is(&m_sbuf)
416   {
417      init();
418   }
419   std::string error_string(regex_constants::error_type n) const
420   {
421      if(!m_error_strings.empty())
422      {
423         std::map<int, std::string>::const_iterator p = m_error_strings.find(n);
424         return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second;
425      }
426      return get_default_error_string(n);
427   }
428   char_class_type lookup_classname(const charT* p1, const charT* p2) const
429   {
430      char_class_type result = lookup_classname_imp(p1, p2);
431      if(result == 0)
432      {
433         string_type temp(p1, p2);
434         this->m_pctype->tolower(&*temp.begin(), &*temp.begin() + temp.size());
435         result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size());
436      }
437      return result;
438   }
439   string_type lookup_collatename(const charT* p1, const charT* p2) const;
440   string_type transform_primary(const charT* p1, const charT* p2) const;
441   string_type transform(const charT* p1, const charT* p2) const;
442   re_detail::parser_buf<charT>   m_sbuf;            // buffer for parsing numbers.
443   std::basic_istream<charT>      m_is;              // stream for parsing numbers.
444private:
445   std::map<int, std::string>     m_error_strings;   // error messages indexed by numberic ID
446   std::map<string_type, char_class_type>  m_custom_class_names; // character class names
447   std::map<string_type, string_type>      m_custom_collate_names; // collating element names
448   unsigned                       m_collate_type;    // the form of the collation string
449   charT                          m_collate_delim;   // the collation group delimiter
450   //
451   // helpers:
452   //
453   char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
454   void init();
455#ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
456public:
457   bool isctype(charT c, char_class_type m)const;
458#endif
459};
460
461#ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
462#if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
463
464template <class charT>
465typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_blank;
466template <class charT>
467typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_word;
468template <class charT>
469typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_unicode;
470
471#endif
472#endif
473
474template <class charT>
475typename cpp_regex_traits_implementation<charT>::string_type
476   cpp_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const
477{
478   //
479   // PRECONDITIONS:
480   //
481   // A bug in gcc 3.2 (and maybe other versions as well) treats
482   // p1 as a null terminated string, for efficiency reasons
483   // we work around this elsewhere, but just assert here that
484   // we adhere to gcc's (buggy) preconditions...
485   //
486   BOOST_ASSERT(*p2 == 0);
487
488   string_type result;
489   //
490   // swallowing all exceptions here is a bad idea
491   // however at least one std lib will always throw
492   // std::bad_alloc for certain arguments...
493   //
494   try{
495      //
496      // What we do here depends upon the format of the sort key returned by
497      // sort key returned by this->transform:
498      //
499      switch(m_collate_type)
500      {
501      case sort_C:
502      case sort_unknown:
503         // the best we can do is translate to lower case, then get a regular sort key:
504         {
505            result.assign(p1, p2);
506            this->m_pctype->tolower(&*result.begin(), &*result.begin() + result.size());
507            result = this->m_pcollate->transform(&*result.begin(), &*result.begin() + result.size());
508            break;
509         }
510      case sort_fixed:
511         {
512            // get a regular sort key, and then truncate it:
513            result.assign(this->m_pcollate->transform(p1, p2));
514            result.erase(this->m_collate_delim);
515            break;
516         }
517      case sort_delim:
518            // get a regular sort key, and then truncate everything after the delim:
519            result.assign(this->m_pcollate->transform(p1, p2));
520            std::size_t i;
521            for(i = 0; i < result.size(); ++i)
522            {
523               if(result[i] == m_collate_delim)
524                  break;
525            }
526            result.erase(i);
527            break;
528      }
529   }catch(...){}
530   while(result.size() && (charT(0) == *result.rbegin()))
531      result.erase(result.size() - 1);
532   if(result.empty())
533   {
534      // character is ignorable at the primary level:
535      result = string_type(1, charT(0));
536   }
537   return result;
538}
539
540template <class charT>
541typename cpp_regex_traits_implementation<charT>::string_type
542   cpp_regex_traits_implementation<charT>::transform(const charT* p1, const charT* p2) const
543{
544   //
545   // PRECONDITIONS:
546   //
547   // A bug in gcc 3.2 (and maybe other versions as well) treats
548   // p1 as a null terminated string, for efficiency reasons
549   // we work around this elsewhere, but just assert here that
550   // we adhere to gcc's (buggy) preconditions...
551   //
552   BOOST_ASSERT(*p2 == 0);
553   //
554   // swallowing all exceptions here is a bad idea
555   // however at least one std lib will always throw
556   // std::bad_alloc for certain arguments...
557   //
558   string_type result;
559   try{
560      result = this->m_pcollate->transform(p1, p2);
561      //
562      // Borland's STLPort version returns a NULL-terminated
563      // string that has garbage at the end - each call to
564      // std::collate<wchar_t>::transform returns a different string!
565      // So as a workaround, we'll truncate the string at the first NULL
566      // which _seems_ to work....
567#if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
568      result.erase(result.find(charT(0)));
569#else
570      //
571      // some implementations (Dinkumware) append unnecessary trailing \0's:
572      while(result.size() && (charT(0) == *result.rbegin()))
573         result.erase(result.size() - 1);
574#endif
575      BOOST_ASSERT(std::find(result.begin(), result.end(), charT(0)) == result.end());
576   }
577   catch(...)
578   {
579   }
580   return result;
581}
582
583
584template <class charT>
585typename cpp_regex_traits_implementation<charT>::string_type
586   cpp_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const
587{
588   typedef typename std::map<string_type, string_type>::const_iterator iter_type;
589   if(m_custom_collate_names.size())
590   {
591      iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
592      if(pos != m_custom_collate_names.end())
593         return pos->second;
594   }
595#if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
596               && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
597               && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
598   std::string name(p1, p2);
599#else
600   std::string name;
601   const charT* p0 = p1;
602   while(p0 != p2)
603      name.append(1, char(*p0++));
604#endif
605   name = lookup_default_collate_name(name);
606#if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
607               && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
608               && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
609   if(name.size())
610      return string_type(name.begin(), name.end());
611#else
612   if(name.size())
613   {
614      string_type result;
615      typedef std::string::const_iterator iter;
616      iter b = name.begin();
617      iter e = name.end();
618      while(b != e)
619         result.append(1, charT(*b++));
620      return result;
621   }
622#endif
623   if(p2 - p1 == 1)
624      return string_type(1, *p1);
625   return string_type();
626}
627
628template <class charT>
629void cpp_regex_traits_implementation<charT>::init()
630{
631#ifndef BOOST_NO_STD_MESSAGES
632#ifndef __IBMCPP__
633   typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
634#else
635   typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
636#endif
637   std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
638   if(cat_name.size())
639   {
640      cat = this->m_pmessages->open(
641         cat_name,
642         this->m_locale);
643      if((int)cat < 0)
644      {
645         std::string m("Unable to open message catalog: ");
646         std::runtime_error err(m + cat_name);
647         boost::re_detail::raise_runtime_error(err);
648      }
649   }
650   //
651   // if we have a valid catalog then load our messages:
652   //
653   if((int)cat >= 0)
654   {
655      //
656      // Error messages:
657      //
658      for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0);
659         i <= boost::regex_constants::error_unknown;
660         i = static_cast<boost::regex_constants::error_type>(i + 1))
661      {
662         const char* p = get_default_error_string(i);
663         string_type default_message;
664         while(*p)
665         {
666            default_message.append(1, this->m_pctype->widen(*p));
667            ++p;
668         }
669         string_type s = this->m_pmessages->get(cat, 0, i+200, default_message);
670         std::string result;
671         for(std::string::size_type j = 0; j < s.size(); ++j)
672         {
673            result.append(1, this->m_pctype->narrow(s[j], 0));
674         }
675         m_error_strings[i] = result;
676      }
677      //
678      // Custom class names:
679      //
680#ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
681      static const char_class_type masks[14] =
682      {
683         std::ctype<charT>::alnum,
684         std::ctype<charT>::alpha,
685         std::ctype<charT>::cntrl,
686         std::ctype<charT>::digit,
687         std::ctype<charT>::graph,
688         std::ctype<charT>::lower,
689         std::ctype<charT>::print,
690         std::ctype<charT>::punct,
691         std::ctype<charT>::space,
692         std::ctype<charT>::upper,
693         std::ctype<charT>::xdigit,
694         cpp_regex_traits_implementation<charT>::mask_blank,
695         cpp_regex_traits_implementation<charT>::mask_word,
696         cpp_regex_traits_implementation<charT>::mask_unicode,
697      };
698#else
699      static const char_class_type masks[14] =
700      {
701         ::boost::re_detail::char_class_alnum,
702         ::boost::re_detail::char_class_alpha,
703         ::boost::re_detail::char_class_cntrl,
704         ::boost::re_detail::char_class_digit,
705         ::boost::re_detail::char_class_graph,
706         ::boost::re_detail::char_class_lower,
707         ::boost::re_detail::char_class_print,
708         ::boost::re_detail::char_class_punct,
709         ::boost::re_detail::char_class_space,
710         ::boost::re_detail::char_class_upper,
711         ::boost::re_detail::char_class_xdigit,
712         ::boost::re_detail::char_class_blank,
713         ::boost::re_detail::char_class_word,
714         ::boost::re_detail::char_class_unicode,
715      };
716#endif
717      static const string_type null_string;
718      for(unsigned int j = 0; j <= 13; ++j)
719      {
720         string_type s(this->m_pmessages->get(cat, 0, j+300, null_string));
721         if(s.size())
722            this->m_custom_class_names[s] = masks[j];
723      }
724   }
725#endif
726   //
727   // get the collation format used by m_pcollate:
728   //
729   m_collate_type = re_detail::find_sort_syntax(this, &m_collate_delim);
730}
731
732template <class charT>
733typename cpp_regex_traits_implementation<charT>::char_class_type
734   cpp_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const
735{
736#ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
737   static const char_class_type masks[20] =
738   {
739      0,
740      std::ctype<char>::alnum,
741      std::ctype<char>::alpha,
742      cpp_regex_traits_implementation<charT>::mask_blank,
743      std::ctype<char>::cntrl,
744      std::ctype<char>::digit,
745      std::ctype<char>::digit,
746      std::ctype<char>::graph,
747      std::ctype<char>::lower,
748      std::ctype<char>::lower,
749      std::ctype<char>::print,
750      std::ctype<char>::punct,
751      std::ctype<char>::space,
752      std::ctype<char>::space,
753      std::ctype<char>::upper,
754      cpp_regex_traits_implementation<charT>::mask_unicode,
755      std::ctype<char>::upper,
756      std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
757      std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
758      std::ctype<char>::xdigit,
759   };
760#else
761   static const char_class_type masks[20] =
762   {
763      0,
764      ::boost::re_detail::char_class_alnum,
765      ::boost::re_detail::char_class_alpha,
766      ::boost::re_detail::char_class_blank,
767      ::boost::re_detail::char_class_cntrl,
768      ::boost::re_detail::char_class_digit,
769      ::boost::re_detail::char_class_digit,
770      ::boost::re_detail::char_class_graph,
771      ::boost::re_detail::char_class_lower,
772      ::boost::re_detail::char_class_lower,
773      ::boost::re_detail::char_class_print,
774      ::boost::re_detail::char_class_punct,
775      ::boost::re_detail::char_class_space,
776      ::boost::re_detail::char_class_space,
777      ::boost::re_detail::char_class_upper,
778      ::boost::re_detail::char_class_unicode,
779      ::boost::re_detail::char_class_upper,
780      ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
781      ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
782      ::boost::re_detail::char_class_xdigit,
783   };
784#endif
785   if(m_custom_class_names.size())
786   {
787      typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter;
788      map_iter pos = m_custom_class_names.find(string_type(p1, p2));
789      if(pos != m_custom_class_names.end())
790         return pos->second;
791   }
792   std::size_t id = 1 + re_detail::get_default_class_id(p1, p2);
793   BOOST_ASSERT(id < sizeof(masks) / sizeof(masks[0]));
794   return masks[id];
795}
796
797#ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
798template <class charT>
799bool cpp_regex_traits_implementation<charT>::isctype(const charT c, char_class_type mask) const
800{
801   return
802      ((mask & ::boost::re_detail::char_class_space) && (m_pctype->is(std::ctype<charT>::space, c)))
803      || ((mask & ::boost::re_detail::char_class_print) && (m_pctype->is(std::ctype<charT>::print, c)))
804      || ((mask & ::boost::re_detail::char_class_cntrl) && (m_pctype->is(std::ctype<charT>::cntrl, c)))
805      || ((mask & ::boost::re_detail::char_class_upper) && (m_pctype->is(std::ctype<charT>::upper, c)))
806      || ((mask & ::boost::re_detail::char_class_lower) && (m_pctype->is(std::ctype<charT>::lower, c)))
807      || ((mask & ::boost::re_detail::char_class_alpha) && (m_pctype->is(std::ctype<charT>::alpha, c)))
808      || ((mask & ::boost::re_detail::char_class_digit) && (m_pctype->is(std::ctype<charT>::digit, c)))
809      || ((mask & ::boost::re_detail::char_class_punct) && (m_pctype->is(std::ctype<charT>::punct, c)))
810      || ((mask & ::boost::re_detail::char_class_xdigit) && (m_pctype->is(std::ctype<charT>::xdigit, c)))
811      || ((mask & ::boost::re_detail::char_class_blank) && (m_pctype->is(std::ctype<charT>::space, c)) && !::boost::re_detail::is_separator(c))
812      || ((mask & ::boost::re_detail::char_class_word) && (c == '_'))
813      || ((mask & ::boost::re_detail::char_class_unicode) && ::boost::re_detail::is_extended(c));
814}
815#endif
816
817
818template <class charT>
819inline boost::shared_ptr<cpp_regex_traits_implementation<charT> > create_cpp_regex_traits(const std::locale& l BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(charT))
820{
821   cpp_regex_traits_base<charT> key(l);
822   return ::boost::object_cache<cpp_regex_traits_base<charT>, cpp_regex_traits_implementation<charT> >::get(key, 5);
823}
824
825} // re_detail
826
827template <class charT>
828class cpp_regex_traits
829{
830private:
831   typedef std::ctype<charT>            ctype_type;
832public:
833   typedef charT                        char_type;
834   typedef std::size_t                  size_type;
835   typedef std::basic_string<char_type> string_type;
836   typedef std::locale                  locale_type;
837   typedef boost::uint_least32_t        char_class_type;
838
839   struct boost_extensions_tag{};
840
841   cpp_regex_traits()
842      : m_pimpl(re_detail::create_cpp_regex_traits<charT>(std::locale()))
843   { }
844   static size_type length(const char_type* p)
845   {
846      return std::char_traits<charT>::length(p);
847   }
848   regex_constants::syntax_type syntax_type(charT c)const
849   {
850      return m_pimpl->syntax_type(c);
851   }
852   regex_constants::escape_syntax_type escape_syntax_type(charT c) const
853   {
854      return m_pimpl->escape_syntax_type(c);
855   }
856   charT translate(charT c) const
857   {
858      return c;
859   }
860   charT translate_nocase(charT c) const
861   {
862      return m_pimpl->m_pctype->tolower(c);
863   }
864   charT translate(charT c, bool icase) const
865   {
866      return icase ? m_pimpl->m_pctype->tolower(c) : c;
867   }
868   charT tolower(charT c) const
869   {
870      return m_pimpl->m_pctype->tolower(c);
871   }
872   charT toupper(charT c) const
873   {
874      return m_pimpl->m_pctype->toupper(c);
875   }
876   string_type transform(const charT* p1, const charT* p2) const
877   {
878      return m_pimpl->transform(p1, p2);
879   }
880   string_type transform_primary(const charT* p1, const charT* p2) const
881   {
882      return m_pimpl->transform_primary(p1, p2);
883   }
884   char_class_type lookup_classname(const charT* p1, const charT* p2) const
885   {
886      return m_pimpl->lookup_classname(p1, p2);
887   }
888   string_type lookup_collatename(const charT* p1, const charT* p2) const
889   {
890      return m_pimpl->lookup_collatename(p1, p2);
891   }
892   bool isctype(charT c, char_class_type f) const
893   {
894#ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
895      typedef typename std::ctype<charT>::mask ctype_mask;
896
897      static const ctype_mask mask_base =
898         static_cast<ctype_mask>(
899            std::ctype<charT>::alnum
900            | std::ctype<charT>::alpha
901            | std::ctype<charT>::cntrl
902            | std::ctype<charT>::digit
903            | std::ctype<charT>::graph
904            | std::ctype<charT>::lower
905            | std::ctype<charT>::print
906            | std::ctype<charT>::punct
907            | std::ctype<charT>::space
908            | std::ctype<charT>::upper
909            | std::ctype<charT>::xdigit);
910
911      if((f & mask_base)
912         && (m_pimpl->m_pctype->is(
913            static_cast<ctype_mask>(f & mask_base), c)))
914         return true;
915      else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_unicode) && re_detail::is_extended(c))
916         return true;
917      else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_word) && (c == '_'))
918         return true;
919      else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_blank)
920         && m_pimpl->m_pctype->is(std::ctype<charT>::space, c)
921         && !re_detail::is_separator(c))
922         return true;
923      return false;
924#else
925      return m_pimpl->isctype(c, f);
926#endif
927   }
928   int toi(const charT*& p1, const charT* p2, int radix)const;
929   int value(charT c, int radix)const
930   {
931      const charT* pc = &c;
932      return toi(pc, pc + 1, radix);
933   }
934   locale_type imbue(locale_type l)
935   {
936      std::locale result(getloc());
937      m_pimpl = re_detail::create_cpp_regex_traits<charT>(l);
938      return result;
939   }
940   locale_type getloc()const
941   {
942      return m_pimpl->m_locale;
943   }
944   std::string error_string(regex_constants::error_type n) const
945   {
946      return m_pimpl->error_string(n);
947   }
948
949   //
950   // extension:
951   // set the name of the message catalog in use (defaults to "boost_regex").
952   //
953   static std::string catalog_name(const std::string& name);
954   static std::string get_catalog_name();
955
956private:
957   boost::shared_ptr<re_detail::cpp_regex_traits_implementation<charT> > m_pimpl;
958   //
959   // catalog name handler:
960   //
961   static std::string& get_catalog_name_inst();
962
963#ifdef BOOST_HAS_THREADS
964   static static_mutex& get_mutex_inst();
965#endif
966};
967
968
969template <class charT>
970int cpp_regex_traits<charT>::toi(const charT*& first, const charT* last, int radix)const
971{
972   // we do NOT want to parse any thousands separators inside the stream:
973   last = std::find(first, last, BOOST_USE_FACET(std::numpunct<charT>, m_pimpl->m_is.getloc()).thousands_sep());
974   m_pimpl->m_sbuf.pubsetbuf(const_cast<charT*>(static_cast<const charT*>(first)), static_cast<std::streamsize>(last-first));
975   m_pimpl->m_is.clear();
976   if(std::abs(radix) == 16) m_pimpl->m_is >> std::hex;
977   else if(std::abs(radix) == 8) m_pimpl->m_is >> std::oct;
978   else m_pimpl->m_is >> std::dec;
979   int val;
980   if(m_pimpl->m_is >> val)
981   {
982      first = first + ((last - first) - m_pimpl->m_sbuf.in_avail());
983      return val;
984   }
985   else
986      return -1;
987}
988
989template <class charT>
990std::string cpp_regex_traits<charT>::catalog_name(const std::string& name)
991{
992#ifdef BOOST_HAS_THREADS
993   static_mutex::scoped_lock lk(get_mutex_inst());
994#endif
995   std::string result(get_catalog_name_inst());
996   get_catalog_name_inst() = name;
997   return result;
998}
999
1000template <class charT>
1001std::string& cpp_regex_traits<charT>::get_catalog_name_inst()
1002{
1003   static std::string s_name;
1004   return s_name;
1005}
1006
1007template <class charT>
1008std::string cpp_regex_traits<charT>::get_catalog_name()
1009{
1010#ifdef BOOST_HAS_THREADS
1011   static_mutex::scoped_lock lk(get_mutex_inst());
1012#endif
1013   std::string result(get_catalog_name_inst());
1014   return result;
1015}
1016
1017#ifdef BOOST_HAS_THREADS
1018template <class charT>
1019static_mutex& cpp_regex_traits<charT>::get_mutex_inst()
1020{
1021   static static_mutex s_mutex = BOOST_STATIC_MUTEX_INIT;
1022   return s_mutex;
1023}
1024#endif
1025
1026
1027} // boost
1028
1029#ifdef BOOST_MSVC
1030#pragma warning(pop)
1031#endif
1032
1033#ifdef BOOST_HAS_ABI_HEADERS
1034#  include BOOST_ABI_SUFFIX
1035#endif
1036
1037#endif
1038
1039#endif
Note: See TracBrowser for help on using the repository browser.