cpp_regex_traits.hpp 41 KB

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