3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
18 * FILE: c_regex_traits.cpp
19 * VERSION: see <boost/version.hpp>
20 * DESCRIPTION: Implements the cpp_regex_traits<charT> traits class
24 #define BOOST_REGEX_SOURCE
26 #include <boost/regex/config.hpp>
28 #ifndef BOOST_NO_STD_LOCALE
37 #include <boost/regex/regex_traits.hpp>
38 #include <boost/cregex.hpp>
39 #include <boost/scoped_array.hpp>
40 #include "primary_transform.hpp"
44 # pragma warning(disable:4786)
48 const unsigned int re_classes_max = 14;
49 const unsigned int char_set_size = CHAR_MAX - CHAR_MIN + 1;
51 boost::uint_fast32_t re_char_class_id[] = {
52 boost::re_detail::cpp_regex_traits_base::char_class_alnum,
53 boost::re_detail::cpp_regex_traits_base::char_class_alpha,
54 boost::re_detail::cpp_regex_traits_base::char_class_cntrl,
55 boost::re_detail::cpp_regex_traits_base::char_class_digit,
56 boost::re_detail::cpp_regex_traits_base::char_class_graph,
57 boost::re_detail::cpp_regex_traits_base::char_class_lower,
58 boost::re_detail::cpp_regex_traits_base::char_class_print,
59 boost::re_detail::cpp_regex_traits_base::char_class_punct,
60 boost::re_detail::cpp_regex_traits_base::char_class_space,
61 boost::re_detail::cpp_regex_traits_base::char_class_upper,
62 boost::re_detail::cpp_regex_traits_base::char_class_xdigit,
63 boost::re_detail::cpp_regex_traits_base::char_class_blank,
64 boost::re_detail::cpp_regex_traits_base::char_class_word,
65 boost::re_detail::cpp_regex_traits_base::char_class_unicode,
68 const char* re_char_class_names[] = {
85 template <class charT,
86 class traits = ::std::char_traits<charT> >
87 class parser_buf : public ::std::basic_streambuf<charT, traits>
89 typedef ::std::basic_streambuf<charT, traits> base_type;
90 typedef typename base_type::int_type int_type;
91 typedef typename base_type::char_type char_type;
92 typedef typename base_type::pos_type pos_type;
93 typedef ::std::streamsize streamsize;
94 typedef typename base_type::off_type off_type;
96 parser_buf() : base_type() { setbuf(0, 0); }
97 const charT* getnext() { return this->gptr(); }
99 std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
100 typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
101 typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
103 parser_buf& operator=(const parser_buf&);
104 parser_buf(const parser_buf&);
107 template<class charT, class traits>
108 std::basic_streambuf<charT, traits>*
109 parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
111 this->setg(s, s, s + n);
115 template<class charT, class traits>
116 typename parser_buf<charT, traits>::pos_type
117 parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
119 typedef typename parser_buf<charT, traits>::pos_type pos_type;
120 if(which & ::std::ios_base::out)
121 return pos_type(off_type(-1));
122 std::ptrdiff_t size = this->egptr() - this->eback();
123 std::ptrdiff_t pos = this->gptr() - this->eback();
124 charT* g = this->eback();
127 case ::std::ios_base::beg:
128 if((off < 0) || (off > size))
129 return pos_type(off_type(-1));
131 this->setg(g, g + off, g + size);
132 case ::std::ios_base::end:
133 if((off < 0) || (off > size))
134 return pos_type(off_type(-1));
136 this->setg(g, g + size - off, g + size);
137 case ::std::ios_base::cur:
139 std::ptrdiff_t newpos = pos + off;
140 if((newpos < 0) || (newpos > size))
141 return pos_type(off_type(-1));
143 this->setg(g, g + newpos, g + size);
146 return static_cast<pos_type>(this->gptr() - this->eback());
149 template<class charT, class traits>
150 typename parser_buf<charT, traits>::pos_type
151 parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
153 if(which & ::std::ios_base::out)
154 return pos_type(off_type(-1));
155 std::ptrdiff_t size = this->egptr() - this->eback();
156 charT* g = this->eback();
159 this->setg(g, g + ::std::streamsize(sp), g + size);
161 return pos_type(off_type(-1));
171 struct message_data<char>
173 unsigned char syntax_map[CHAR_MAX-CHAR_MIN];
174 std::map<std::string, std::string, std::less<std::string> > collating_elements;
175 std::map<std::string, std::size_t, std::less<std::string> > classes;
178 parser_buf<char> sbuf;
180 std::string error_strings[boost::REG_E_UNKNOWN+1];
182 message_data(const std::locale& l, const std::string& regex_message_catalogue);
184 message_data(const message_data&);
185 message_data& operator=(const message_data&);
189 message_data<char>::message_data(const std::locale& l, const std::string& regex_message_catalogue)
193 #ifndef BOOST_NO_STD_MESSAGES
195 const std::messages<char>* pm = 0;
196 std::messages<char>::catalog cat = -1;
197 if(regex_message_catalogue.size())
199 pm = &BOOST_USE_FACET(std::messages<char>, l);
200 cat = pm->open(regex_message_catalogue, l);
201 #ifndef BOOST_NO_EXCEPTIONS
204 std::string m("Unable to open message catalog: ");
205 throw std::runtime_error(m + regex_message_catalogue);
208 BOOST_REGEX_NOEH_ASSERT(cat >= 0);
212 std::memset(syntax_map, cpp_regex_traits<char>::syntax_char, 256);
214 scoped_array<char> a;
215 std::size_t array_size = 0;
216 std::size_t new_size;
217 for(i = 1; i < cpp_regex_traits<char>::syntax_max; ++i)
219 new_size = re_get_default_message(0, 0, i+100);
220 if(new_size > array_size)
222 a.reset(new char[new_size]);
223 array_size = new_size;
225 re_get_default_message(a.get(), array_size, i+100);
226 std::string s = a.get();
227 #ifndef BOOST_NO_STD_MESSAGES
229 s = pm->get(cat, 0, i+100, s);
231 for(std::size_t j = 0; j < s.size(); ++j)
233 syntax_map[s[j]] = (unsigned char)(i);
237 #ifndef BOOST_NO_STD_MESSAGES
238 // load any custom collate names:
240 // for some reason Borland C++ Builder 6 won't let us use
241 // std::isspace(char, std::locale) unless we call it
242 // unqualifed - weird. This seems to be affecting other
243 // STLport users as well (gcc3.1+STLport5), so enable the
244 // workaround for all STLport users...
246 #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
248 # define BOOST_REGEX_STD
250 # define BOOST_REGEX_STD std::
257 c2 = pm->get(cat, 0, i, c1);
260 const char* p1, *p2, *p3, *p4;;
262 while(*p1 && BOOST_REGEX_STD isspace((char)*p1, l))++p1;
264 while(*p2 && !BOOST_REGEX_STD isspace((char)*p2, l))++p2;
266 while(*p3 && BOOST_REGEX_STD isspace((char)*p3, l))++p3;
268 while(*p4 && !BOOST_REGEX_STD isspace((char)*p4, l))++p4;
269 collating_elements[std::string(p1, p2)] = std::string(p3, p4);
272 c2 = pm->get(cat, 0, i, c1);
278 #ifndef BOOST_NO_STD_MESSAGES
281 for(i = 0; i < re_classes_max; ++i)
283 s = pm->get(cat, 0, i+300, m);
287 for(i = 0; i <= boost::REG_E_UNKNOWN ; ++i)
289 s = pm->get(cat, 0, i+200, m);
290 error_strings[i] = s;
299 std::string BOOST_REGEX_CALL cpp_regex_traits_base::set_message_catalogue(const std::string& l)
301 if(sizeof(regex_message_cat) <= l.size())
303 std::string old(regex_message_cat);
304 std::strcpy(regex_message_cat, l.c_str());
308 char cpp_regex_traits_base::regex_message_cat[BOOST_REGEX_MAX_PATH] = {0};
311 } // namespace re_detail
314 cpp_regex_traits<char>::cpp_regex_traits()
316 pmd = new re_detail::message_data<char>(locale_inst, regex_message_cat);
317 psyntax = pmd->syntax_map;
318 #ifndef BOOST_NO_EXCEPTIONS
321 lower_map = new char[char_set_size];
322 BOOST_REGEX_NOEH_ASSERT(lower_map)
323 #ifndef BOOST_NO_EXCEPTIONS
331 for(unsigned int i = 0; i < char_set_size; ++i)
332 lower_map[i] = static_cast<char>(i);
333 pctype = &BOOST_USE_FACET(std::ctype<char>, locale_inst);
334 pctype->tolower(&lower_map[0], &lower_map[char_set_size]);
335 pcollate = &BOOST_USE_FACET(std::collate<char>, locale_inst);
336 sort_type = re_detail::find_sort_syntax(this, &(this->sort_delim));
339 cpp_regex_traits<char>::~cpp_regex_traits()
345 int BOOST_REGEX_CALL cpp_regex_traits<char>::toi(char c)const
347 pmd->sbuf.pubsetbuf(&c, 1);
359 int BOOST_REGEX_CALL cpp_regex_traits<char>::toi(const char*& first, const char* last, int radix)const
361 pmd->sbuf.pubsetbuf(const_cast<char*>(first), static_cast<std::streamsize>(last-first));
363 if(std::abs(radix) == 16) pmd->is >> std::hex;
364 else if(std::abs(radix) == 8) pmd->is >> std::oct;
365 else pmd->is >> std::dec;
369 first = first + ((last - first) - pmd->sbuf.in_avail());
376 boost::uint_fast32_t BOOST_REGEX_CALL cpp_regex_traits<char>::lookup_classname(const char* first, const char* last)const
380 std::string s(first, last);
382 std::map<std::string, std::size_t, std::less<std::string> >::const_iterator pos = pmd->classes.find(s);
383 if(pos != pmd->classes.end())
384 return re_char_class_id[(*pos).second];
386 for(i = 0; i < re_classes_max; ++i)
388 if(s == re_char_class_names[i])
389 return re_char_class_id[i];
394 bool BOOST_REGEX_CALL cpp_regex_traits<char>::lookup_collatename(std::string& s, const char* first, const char* last)const
397 std::string name(first, last);
398 std::map<std::string, std::string, std::less<std::string > >::const_iterator pos = pmd->collating_elements.find(name);
399 if(pos != pmd->collating_elements.end())
404 return re_detail::re_lookup_def_collate_name(s, name.c_str());
407 void BOOST_REGEX_CALL cpp_regex_traits<char>::transform_primary(std::string& out, const std::string& in)const
412 case re_detail::sort_C:
413 case re_detail::sort_unknown:
415 case re_detail::sort_fixed:
416 if((unsigned)sort_delim < out.size())
417 out.erase((int)sort_delim);
419 case re_detail::sort_delim:
420 for(unsigned int i = 0; i < out.size(); ++i)
422 if((out[i] == sort_delim) && (i+1 < out.size()))
432 std::string BOOST_REGEX_CALL cpp_regex_traits<char>::error_string(unsigned id)const
434 if((id <= boost::REG_E_UNKNOWN) && (pmd->error_strings[id].size()))
435 return pmd->error_strings[id];
436 return boost::re_detail::re_default_error_messages[id];
439 cpp_regex_traits<char>::locale_type BOOST_REGEX_CALL cpp_regex_traits<char>::imbue(locale_type l)
441 locale_type old_l(locale_inst);
443 re_detail::message_data<char>* npmd = new re_detail::message_data<char>(locale_inst, regex_message_cat);
446 psyntax = pmd->syntax_map;
447 for(unsigned int i = 0; i < char_set_size; ++i)
448 lower_map[i] = static_cast<char>(i);
449 pctype = &BOOST_USE_FACET(std::ctype<char>, locale_inst);
450 pctype->tolower(&lower_map[0], &lower_map[char_set_size]);
451 pcollate = &BOOST_USE_FACET(std::collate<char>, locale_inst);
452 sort_type = re_detail::find_sort_syntax(this, &(this->sort_delim));
456 #ifndef BOOST_NO_WREGEX
460 std::string BOOST_REGEX_CALL to_narrow(const std::basic_string<wchar_t>& is, const std::codecvt<wchar_t, char, std::mbstate_t>& cvt)
463 std::basic_string<wchar_t>::size_type bufsize = is.size() * 2;
465 // declare buffer first as VC6 workaround for internal compiler error!
466 char* pc = new char[bufsize];
467 scoped_array<char> t(pc);
468 #if defined(BOOST_MSVC) && !defined(DINKUMWARE_CE)
469 std::mbstate_t state = 0;
471 std::mbstate_t state = std::mbstate_t();
474 const wchar_t* next_in;
478 switch(cvt.out(state, is.c_str(), is.c_str() + is.size(), next_in, t.get(), t.get() + bufsize, next_out))
480 case std::codecvt_base::ok:
481 return std::string(t.get(), next_out);
482 case std::codecvt_base::partial:
484 t.reset(new char[bufsize]);
486 case std::codecvt_base::error:
487 // not much we can do here but guess:
488 case std::codecvt_base::noconv:
490 for(unsigned i = 0; i < is.size(); ++i)
492 out.append(1, (char)is[i]);
499 std::wstring BOOST_REGEX_CALL to_wide(const std::string& is, const std::codecvt<wchar_t, char, std::mbstate_t>& cvt)
502 std::string::size_type bufsize = is.size() + 2;
503 std::string::size_type maxsize = is.size() * 100;
505 // declare buffer first as VC6 workaround for internal compiler error!
506 wchar_t* pc = new wchar_t[bufsize];
507 scoped_array<wchar_t> t(pc);
508 #if defined(BOOST_MSVC) && !defined(DINKUMWARE_CE)
509 std::mbstate_t state = 0;
511 std::mbstate_t state = std::mbstate_t();
519 switch(cvt.in(state, is.c_str(), is.c_str() + is.size(), next_in, t.get(), t.get() + bufsize, next_out))
521 case std::codecvt_base::ok:
522 return std::wstring(t.get(), next_out);
523 case std::codecvt_base::partial:
525 if(bufsize < maxsize)
527 t.reset(new wchar_t[bufsize]);
531 // error fall through:
532 case std::codecvt_base::error:
533 // not much we can do here but guess:
534 case std::codecvt_base::noconv:
536 for(unsigned i = 0; i < is.size(); ++i)
538 out.append(1, is[i]);
548 struct message_data<wchar_t>
550 #ifndef BOOST_NO_STD_MESSAGES
551 typedef std::messages<wchar_t>::string_type string_type;
553 typedef std::wstring string_type;
564 std::list<syntax_map> syntax;
565 std::map<string_type, std::size_t> classes;
566 std::map<string_type, string_type> collating_elements;
567 unsigned char syntax_[CHAR_MAX-CHAR_MIN+1];
569 parser_buf<wchar_t> sbuf;
571 std::string error_strings[boost::REG_E_UNKNOWN+1];
573 message_data(const std::locale& l, const std::string& regex_message_catalogue);
575 message_data(const message_data&);
576 message_data& operator=(const message_data&);
579 message_data<wchar_t>::message_data(const std::locale& l, const std::string& regex_message_catalogue)
584 typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_type;
585 const cvt_type& cvt = BOOST_USE_FACET(cvt_type, l);
586 #ifndef BOOST_NO_STD_MESSAGES
587 const std::messages<wchar_t>& msgs = BOOST_USE_FACET(std::messages<wchar_t>, l);
588 std::messages<wchar_t>::catalog cat = -1;
589 if(regex_message_catalogue.size())
591 cat = msgs.open(regex_message_catalogue, l);
592 #ifndef BOOST_NO_EXCEPTIONS
595 std::string m("Unable to open message catalog: ");
596 throw std::runtime_error(m + regex_message_catalogue);
599 BOOST_REGEX_NOEH_ASSERT(cat >= 0);
603 scoped_array<char> a;
604 std::size_t array_size = 0;
605 std::size_t new_size;
607 std::memset(syntax_, cpp_regex_traits<wchar_t>::syntax_char, sizeof(syntax_));
608 for(i = 1; i < cpp_regex_traits<wchar_t>::syntax_max; ++i)
610 new_size = re_get_default_message(0, 0, i+100);
611 if(new_size > array_size)
613 a.reset(new char[new_size]);
614 array_size = new_size;
616 re_get_default_message(a.get(), array_size, i+100);
617 std::string ns = a.get();
618 string_type s = to_wide(ns, cvt);
619 #ifndef BOOST_NO_STD_MESSAGES
621 s = BOOST_USE_FACET(std::messages<wchar_t>, l).get(cat, 0, (int)i+100, s);
623 for(unsigned int j = 0; j < s.size(); ++j)
625 if((s[j] <= UCHAR_MAX) && (s[j] >= 0))
626 syntax_[s[j]] = static_cast<unsigned char>(i);
630 m.type = static_cast<unsigned int>(i);
636 #ifndef BOOST_NO_STD_MESSAGES
637 // load any custom collate names:
642 c2 = msgs.get(cat, 0, (int)i, c1);
645 const wchar_t* p1, *p2, *p3, *p4;;
647 while(*p1 && BOOST_REGEX_STD isspace((wchar_t)*p1, l))++p1;
649 while(*p2 && !BOOST_REGEX_STD isspace((wchar_t)*p2, l))++p2;
651 while(*p3 && BOOST_REGEX_STD isspace((wchar_t)*p3, l))++p3;
653 while(*p4 && !BOOST_REGEX_STD isspace((wchar_t)*p4, l))++p4;
654 collating_elements[std::basic_string<wchar_t>(p1, p2)] = std::basic_string<wchar_t>(p3, p4);
657 c2 = msgs.get(cat, 0, (int)i, c1);
664 for(i = 0; i < re_classes_max; ++i)
666 c1 = msgs.get(cat, 0, static_cast<int>(i+300), c2);
670 for(i = 0; i <= boost::REG_E_UNKNOWN ; ++i)
672 c1 = msgs.get(cat, 0, static_cast<int>(i+200), c2);
673 error_strings[i] = to_narrow(c1, cvt);
682 } // namespace re_detail
684 unsigned int BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::do_syntax_type(size_type c)const
686 std::list<re_detail::message_data<wchar_t>::syntax_map>::const_iterator i, j;
687 i = pmd->syntax.begin();
688 j = pmd->syntax.end();
691 if(((uchar_type)(*i).c) == c)
698 void BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::transform_primary(std::basic_string<wchar_t>& out, const std::basic_string<wchar_t>& in)const
703 case re_detail::sort_C:
704 case re_detail::sort_unknown:
706 case re_detail::sort_fixed:
707 if((unsigned)sort_delim < out.size())
708 out.erase((int)sort_delim);
710 case re_detail::sort_delim:
711 for(unsigned int i = 0; i < out.size(); ++i)
713 if((out[i] == sort_delim) && (i+1 < out.size()))
722 int BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::toi(wchar_t c)const
724 pmd->sbuf.pubsetbuf(&c, 1);
736 int BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::toi(const wchar_t*& first, const wchar_t* last, int radix)const
738 pmd->sbuf.pubsetbuf(const_cast<wchar_t*>(first), static_cast<std::streamsize>(last-first));
740 if(std::abs(radix) == 16) pmd->is >> std::hex;
741 else if(std::abs(radix) == 8) pmd->is >> std::oct;
742 else pmd->is >> std::dec;
746 first = first + ((last - first) - pmd->sbuf.in_avail());
753 boost::uint_fast32_t BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::lookup_classname(const wchar_t* first, const wchar_t* last)const
757 std::wstring s(first, last);
759 std::map<std::wstring, std::size_t>::const_iterator pos = pmd->classes.find(s);
760 if(pos != pmd->classes.end())
761 return re_char_class_id[(*pos).second];
763 std::string ns = re_detail::to_narrow(s, *pcdv);
765 for(i = 0; i < re_classes_max; ++i)
767 if(ns == re_char_class_names[i])
768 return re_char_class_id[i];
773 bool BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::lookup_collatename(std::basic_string<wchar_t>& s, const wchar_t* first, const wchar_t* last)const
776 std::wstring name(first, last);
777 std::map<std::wstring, std::wstring>::const_iterator pos = pmd->collating_elements.find(name);
778 if(pos != pmd->collating_elements.end())
783 std::string ns = re_detail::to_narrow(name, *pcdv);
785 bool result = re_detail::re_lookup_def_collate_name(ns2, ns.c_str());
786 s = re_detail::to_wide(ns2, *pcdv);
790 std::string BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::error_string(unsigned id)const
792 if((id <= boost::REG_E_UNKNOWN) && (pmd->error_strings[id].size()))
793 return pmd->error_strings[id];
794 return boost::re_detail::re_default_error_messages[id];
797 cpp_regex_traits<wchar_t>::cpp_regex_traits()
799 pmd = new re_detail::message_data<wchar_t>(locale_inst, std::string(regex_message_cat));
800 psyntax = pmd->syntax_;
801 #ifndef BOOST_NO_EXCEPTIONS
804 lower_map = new wchar_t[char_set_size];
805 BOOST_REGEX_NOEH_ASSERT(lower_map)
806 #ifndef BOOST_NO_EXCEPTIONS
814 for(unsigned int i = 0; i < char_set_size; ++i)
815 lower_map[i] = static_cast<wchar_t>(i);
816 pctype = &BOOST_USE_FACET(std::ctype<wchar_t>, locale_inst);
817 pctype->tolower(&lower_map[0], &lower_map[char_set_size]);
818 pcollate = &BOOST_USE_FACET(std::collate<wchar_t>, locale_inst);
819 typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_t;
820 pcdv = &BOOST_USE_FACET(cvt_t, locale_inst);
821 sort_type = re_detail::find_sort_syntax(this, &(this->sort_delim));
824 cpp_regex_traits<wchar_t>::~cpp_regex_traits()
830 cpp_regex_traits<wchar_t>::locale_type BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::imbue(locale_type l)
832 locale_type old_l(locale_inst);
834 re_detail::message_data<wchar_t>* npmd = new re_detail::message_data<wchar_t>(locale_inst, std::string(regex_message_cat));
837 psyntax = pmd->syntax_;
838 for(unsigned int i = 0; i < char_set_size; ++i)
839 lower_map[i] = static_cast<wchar_t>(i);
840 pctype = &BOOST_USE_FACET(std::ctype<wchar_t>, locale_inst);
841 pctype->tolower(&lower_map[0], &lower_map[char_set_size]);
842 pcollate = &BOOST_USE_FACET(std::collate<wchar_t>, locale_inst);
843 typedef std::codecvt<wchar_t, char, std::mbstate_t> cvt_t;
844 pcdv = &BOOST_USE_FACET(cvt_t, locale_inst);
845 sort_type = re_detail::find_sort_syntax(this, &(this->sort_delim));
849 std::size_t BOOST_REGEX_CALL cpp_regex_traits<wchar_t>::strwiden(wchar_t *s1, std::size_t len, const char *s2)const
852 std::wstring ws = re_detail::to_wide(s2, *pcdv);
854 std::wcscpy(s1, ws.c_str());
858 #endif // BOOST_NO_WREGEX