cpp_regex_traits.hpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/pending/object_cache.hpp>
  37. #endif
  38. #include <istream>
  39. #include <ios>
  40. #include <climits>
  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);
  81. typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
  82. typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
  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. { 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 specialisation 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.size() && (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. // specialised version for narrow characters:
  330. //
  331. template <>
  332. class BOOST_REGEX_DECL 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_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.size() && (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_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(__BORLANDC__, < 0x580)
  583. result.erase(result.find(charT(0)));
  584. #else
  585. //
  586. // some implementations (Dinkumware) append unnecessary trailing \0's:
  587. while(result.size() && (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_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.size())
  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(__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(__BORLANDC__, <= 0x0551)
  644. if(name.size())
  645. return string_type(name.begin(), name.end());
  646. #else
  647. if(name.size())
  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.size() && (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.size())
  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.size())
  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_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. } // boost
  1071. #ifdef BOOST_MSVC
  1072. #pragma warning(pop)
  1073. #endif
  1074. #ifdef BOOST_MSVC
  1075. #pragma warning(push)
  1076. #pragma warning(disable: 4103)
  1077. #endif
  1078. #ifdef BOOST_HAS_ABI_HEADERS
  1079. # include BOOST_ABI_SUFFIX
  1080. #endif
  1081. #ifdef BOOST_MSVC
  1082. #pragma warning(pop)
  1083. #endif
  1084. #endif
  1085. #endif