basic_regex.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. *
  3. * Copyright (c) 1998-2004
  4. * John Maddock
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or copy at
  8. * http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org/ for most recent version.
  13. * FILE basic_regex.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares template class basic_regex.
  16. */
  17. #ifndef BOOST_REGEX_V4_BASIC_REGEX_HPP
  18. #define BOOST_REGEX_V4_BASIC_REGEX_HPP
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/functional/hash.hpp>
  21. #ifdef BOOST_MSVC
  22. #pragma warning(push)
  23. #pragma warning(disable: 4103)
  24. #endif
  25. #ifdef BOOST_HAS_ABI_HEADERS
  26. # include BOOST_ABI_PREFIX
  27. #endif
  28. #ifdef BOOST_MSVC
  29. #pragma warning(pop)
  30. #endif
  31. namespace boost{
  32. #ifdef BOOST_MSVC
  33. #pragma warning(push)
  34. #pragma warning(disable : 4251 4231 4660 4800)
  35. #endif
  36. namespace re_detail{
  37. //
  38. // forward declaration, we will need this one later:
  39. //
  40. template <class charT, class traits>
  41. class basic_regex_parser;
  42. template <class I>
  43. void bubble_down_one(I first, I last)
  44. {
  45. if(first != last)
  46. {
  47. I next = last - 1;
  48. while((next != first) && !(*(next-1) < *next))
  49. {
  50. (next-1)->swap(*next);
  51. --next;
  52. }
  53. }
  54. }
  55. //
  56. // Class named_subexpressions
  57. // Contains information about named subexpressions within the regex.
  58. //
  59. template <class charT>
  60. class named_subexpressions_base
  61. {
  62. public:
  63. virtual int get_id(const charT* i, const charT* j)const = 0;
  64. virtual int get_id(std::size_t hash)const = 0;
  65. #ifdef __GNUC__
  66. // warning supression:
  67. virtual ~named_subexpressions_base(){}
  68. #endif
  69. };
  70. template <class Iterator>
  71. inline std::size_t hash_value_from_capture_name(Iterator i, Iterator j)
  72. {
  73. std::size_t r = boost::hash_range(i, j);
  74. r %= ((std::numeric_limits<int>::max)() - 10001);
  75. r += 10000;
  76. return r;
  77. }
  78. template <class charT>
  79. class named_subexpressions : public named_subexpressions_base<charT>
  80. {
  81. struct name
  82. {
  83. name(const charT* i, const charT* j, int idx)
  84. : /*n(i, j), */ index(idx)
  85. {
  86. hash = hash_value_from_capture_name(i, j);
  87. }
  88. name(std::size_t h, int idx)
  89. : index(idx), hash(h)
  90. {
  91. }
  92. //std::vector<charT> n;
  93. int index;
  94. std::size_t hash;
  95. bool operator < (const name& other)const
  96. {
  97. return hash < other.hash; //std::lexicographical_compare(n.begin(), n.end(), other.n.begin(), other.n.end());
  98. }
  99. bool operator == (const name& other)const
  100. {
  101. return hash == other.hash; //n == other.n;
  102. }
  103. void swap(name& other)
  104. {
  105. //n.swap(other.n);
  106. std::swap(index, other.index);
  107. std::swap(hash, other.hash);
  108. }
  109. };
  110. public:
  111. named_subexpressions(){}
  112. void set_name(const charT* i, const charT* j, int index)
  113. {
  114. m_sub_names.push_back(name(i, j, index));
  115. bubble_down_one(m_sub_names.begin(), m_sub_names.end());
  116. }
  117. int get_id(const charT* i, const charT* j)const
  118. {
  119. name t(i, j, 0);
  120. typename std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
  121. if((pos != m_sub_names.end()) && (*pos == t))
  122. {
  123. return pos->index;
  124. }
  125. return -1;
  126. }
  127. int get_id(std::size_t h)const
  128. {
  129. name t(h, 0);
  130. typename std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
  131. if((pos != m_sub_names.end()) && (*pos == t))
  132. {
  133. return pos->index;
  134. }
  135. return -1;
  136. }
  137. private:
  138. std::vector<name> m_sub_names;
  139. };
  140. template <class charT, class Other>
  141. class named_subexpressions_converter : public named_subexpressions_base<charT>
  142. {
  143. boost::shared_ptr<named_subexpressions<Other> > m_converter;
  144. public:
  145. named_subexpressions_converter(boost::shared_ptr<named_subexpressions<Other> > s)
  146. : m_converter(s) {}
  147. int get_id(const charT* i, const charT* j)const
  148. {
  149. if(i == j)
  150. return -1;
  151. std::vector<Other> v;
  152. while(i != j)
  153. {
  154. v.push_back(*i);
  155. ++i;
  156. }
  157. return m_converter->get_id(&v[0], &v[0] + v.size());
  158. }
  159. int get_id(std::size_t h)const
  160. {
  161. return m_converter->get_id(h);
  162. }
  163. };
  164. template <class To>
  165. inline boost::shared_ptr<named_subexpressions_base<To> > convert_to_named_subs_imp(
  166. boost::shared_ptr<named_subexpressions<To> > s,
  167. boost::integral_constant<bool,true> const&)
  168. {
  169. return s;
  170. }
  171. template <class To, class From>
  172. inline boost::shared_ptr<named_subexpressions_base<To> > convert_to_named_subs_imp(
  173. boost::shared_ptr<named_subexpressions<From> > s,
  174. boost::integral_constant<bool,false> const&)
  175. {
  176. return boost::shared_ptr<named_subexpressions_converter<To, From> >(new named_subexpressions_converter<To, From>(s));
  177. }
  178. template <class To, class From>
  179. inline boost::shared_ptr<named_subexpressions_base<To> > convert_to_named_subs(
  180. boost::shared_ptr<named_subexpressions<From> > s)
  181. {
  182. typedef typename boost::is_same<To, From>::type tag_type;
  183. return convert_to_named_subs_imp<To>(s, tag_type());
  184. }
  185. //
  186. // class regex_data:
  187. // represents the data we wish to expose to the matching algorithms.
  188. //
  189. template <class charT, class traits>
  190. struct regex_data : public named_subexpressions<charT>
  191. {
  192. typedef regex_constants::syntax_option_type flag_type;
  193. typedef std::size_t size_type;
  194. regex_data(const ::boost::shared_ptr<
  195. ::boost::regex_traits_wrapper<traits> >& t)
  196. : m_ptraits(t), m_expression(0), m_expression_len(0) {}
  197. regex_data()
  198. : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0) {}
  199. ::boost::shared_ptr<
  200. ::boost::regex_traits_wrapper<traits>
  201. > m_ptraits; // traits class instance
  202. flag_type m_flags; // flags with which we were compiled
  203. int m_status; // error code (0 implies OK).
  204. const charT* m_expression; // the original expression
  205. std::ptrdiff_t m_expression_len; // the length of the original expression
  206. size_type m_mark_count; // the number of marked sub-expressions
  207. re_detail::re_syntax_base* m_first_state; // the first state of the machine
  208. unsigned m_restart_type; // search optimisation type
  209. unsigned char m_startmap[1 << CHAR_BIT]; // which characters can start a match
  210. unsigned int m_can_be_null; // whether we can match a null string
  211. re_detail::raw_storage m_data; // the buffer in which our states are constructed
  212. typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
  213. std::vector<
  214. std::pair<
  215. std::size_t, std::size_t> > m_subs; // Position of sub-expressions within the *string*.
  216. bool m_has_recursions; // whether we have recursive expressions;
  217. };
  218. //
  219. // class basic_regex_implementation
  220. // pimpl implementation class for basic_regex.
  221. //
  222. template <class charT, class traits>
  223. class basic_regex_implementation
  224. : public regex_data<charT, traits>
  225. {
  226. public:
  227. typedef regex_constants::syntax_option_type flag_type;
  228. typedef std::ptrdiff_t difference_type;
  229. typedef std::size_t size_type;
  230. typedef typename traits::locale_type locale_type;
  231. typedef const charT* const_iterator;
  232. basic_regex_implementation(){}
  233. basic_regex_implementation(const ::boost::shared_ptr<
  234. ::boost::regex_traits_wrapper<traits> >& t)
  235. : regex_data<charT, traits>(t) {}
  236. void assign(const charT* arg_first,
  237. const charT* arg_last,
  238. flag_type f)
  239. {
  240. regex_data<charT, traits>* pdat = this;
  241. basic_regex_parser<charT, traits> parser(pdat);
  242. parser.parse(arg_first, arg_last, f);
  243. }
  244. locale_type BOOST_REGEX_CALL imbue(locale_type l)
  245. {
  246. return this->m_ptraits->imbue(l);
  247. }
  248. locale_type BOOST_REGEX_CALL getloc()const
  249. {
  250. return this->m_ptraits->getloc();
  251. }
  252. std::basic_string<charT> BOOST_REGEX_CALL str()const
  253. {
  254. std::basic_string<charT> result;
  255. if(this->m_status == 0)
  256. result = std::basic_string<charT>(this->m_expression, this->m_expression_len);
  257. return result;
  258. }
  259. const_iterator BOOST_REGEX_CALL expression()const
  260. {
  261. return this->m_expression;
  262. }
  263. std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
  264. {
  265. if(n == 0)
  266. throw std::out_of_range("0 is not a valid subexpression index.");
  267. const std::pair<std::size_t, std::size_t>& pi = this->m_subs.at(n - 1);
  268. std::pair<const_iterator, const_iterator> p(expression() + pi.first, expression() + pi.second);
  269. return p;
  270. }
  271. //
  272. // begin, end:
  273. const_iterator BOOST_REGEX_CALL begin()const
  274. {
  275. return (!this->m_status ? 0 : this->m_expression);
  276. }
  277. const_iterator BOOST_REGEX_CALL end()const
  278. {
  279. return (!this->m_status ? 0 : this->m_expression + this->m_expression_len);
  280. }
  281. flag_type BOOST_REGEX_CALL flags()const
  282. {
  283. return this->m_flags;
  284. }
  285. size_type BOOST_REGEX_CALL size()const
  286. {
  287. return this->m_expression_len;
  288. }
  289. int BOOST_REGEX_CALL status()const
  290. {
  291. return this->m_status;
  292. }
  293. size_type BOOST_REGEX_CALL mark_count()const
  294. {
  295. return this->m_mark_count;
  296. }
  297. const re_detail::re_syntax_base* get_first_state()const
  298. {
  299. return this->m_first_state;
  300. }
  301. unsigned get_restart_type()const
  302. {
  303. return this->m_restart_type;
  304. }
  305. const unsigned char* get_map()const
  306. {
  307. return this->m_startmap;
  308. }
  309. const ::boost::regex_traits_wrapper<traits>& get_traits()const
  310. {
  311. return *(this->m_ptraits);
  312. }
  313. bool can_be_null()const
  314. {
  315. return this->m_can_be_null;
  316. }
  317. const regex_data<charT, traits>& get_data()const
  318. {
  319. basic_regex_implementation<charT, traits> const* p = this;
  320. return *static_cast<const regex_data<charT, traits>*>(p);
  321. }
  322. };
  323. } // namespace re_detail
  324. //
  325. // class basic_regex:
  326. // represents the compiled
  327. // regular expression:
  328. //
  329. #ifdef BOOST_REGEX_NO_FWD
  330. template <class charT, class traits = regex_traits<charT> >
  331. #else
  332. template <class charT, class traits >
  333. #endif
  334. class basic_regex : public regbase
  335. {
  336. public:
  337. // typedefs:
  338. typedef std::size_t traits_size_type;
  339. typedef typename traits::string_type traits_string_type;
  340. typedef charT char_type;
  341. typedef traits traits_type;
  342. typedef charT value_type;
  343. typedef charT& reference;
  344. typedef const charT& const_reference;
  345. typedef const charT* const_iterator;
  346. typedef const_iterator iterator;
  347. typedef std::ptrdiff_t difference_type;
  348. typedef std::size_t size_type;
  349. typedef regex_constants::syntax_option_type flag_type;
  350. // locale_type
  351. // placeholder for actual locale type used by the
  352. // traits class to localise *this.
  353. typedef typename traits::locale_type locale_type;
  354. public:
  355. explicit basic_regex(){}
  356. explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
  357. {
  358. assign(p, f);
  359. }
  360. basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
  361. {
  362. assign(p1, p2, f);
  363. }
  364. basic_regex(const charT* p, size_type len, flag_type f)
  365. {
  366. assign(p, len, f);
  367. }
  368. basic_regex(const basic_regex& that)
  369. : m_pimpl(that.m_pimpl) {}
  370. ~basic_regex(){}
  371. basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
  372. {
  373. return assign(that);
  374. }
  375. basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
  376. {
  377. return assign(ptr);
  378. }
  379. //
  380. // assign:
  381. basic_regex& assign(const basic_regex& that)
  382. {
  383. m_pimpl = that.m_pimpl;
  384. return *this;
  385. }
  386. basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
  387. {
  388. return assign(p, p + traits::length(p), f);
  389. }
  390. basic_regex& assign(const charT* p, size_type len, flag_type f)
  391. {
  392. return assign(p, p + len, f);
  393. }
  394. private:
  395. basic_regex& do_assign(const charT* p1,
  396. const charT* p2,
  397. flag_type f);
  398. public:
  399. basic_regex& assign(const charT* p1,
  400. const charT* p2,
  401. flag_type f = regex_constants::normal)
  402. {
  403. return do_assign(p1, p2, f);
  404. }
  405. #if !defined(BOOST_NO_MEMBER_TEMPLATES)
  406. template <class ST, class SA>
  407. unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
  408. {
  409. return set_expression(p.data(), p.data() + p.size(), f);
  410. }
  411. template <class ST, class SA>
  412. explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
  413. {
  414. assign(p, f);
  415. }
  416. template <class InputIterator>
  417. basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
  418. {
  419. typedef typename traits::string_type seq_type;
  420. seq_type a(arg_first, arg_last);
  421. if(a.size())
  422. assign(&*a.begin(), &*a.begin() + a.size(), f);
  423. else
  424. assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
  425. }
  426. template <class ST, class SA>
  427. basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
  428. {
  429. return assign(p.data(), p.data() + p.size(), regex_constants::normal);
  430. }
  431. template <class string_traits, class A>
  432. basic_regex& BOOST_REGEX_CALL assign(
  433. const std::basic_string<charT, string_traits, A>& s,
  434. flag_type f = regex_constants::normal)
  435. {
  436. return assign(s.data(), s.data() + s.size(), f);
  437. }
  438. template <class InputIterator>
  439. basic_regex& BOOST_REGEX_CALL assign(InputIterator arg_first,
  440. InputIterator arg_last,
  441. flag_type f = regex_constants::normal)
  442. {
  443. typedef typename traits::string_type seq_type;
  444. seq_type a(arg_first, arg_last);
  445. if(a.size())
  446. {
  447. const charT* p1 = &*a.begin();
  448. const charT* p2 = &*a.begin() + a.size();
  449. return assign(p1, p2, f);
  450. }
  451. return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
  452. }
  453. #else
  454. unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
  455. {
  456. return set_expression(p.data(), p.data() + p.size(), f);
  457. }
  458. basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
  459. {
  460. assign(p, f);
  461. }
  462. basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
  463. {
  464. return assign(p.data(), p.data() + p.size(), regex_constants::normal);
  465. }
  466. basic_regex& BOOST_REGEX_CALL assign(
  467. const std::basic_string<charT>& s,
  468. flag_type f = regex_constants::normal)
  469. {
  470. return assign(s.data(), s.data() + s.size(), f);
  471. }
  472. #endif
  473. //
  474. // locale:
  475. locale_type BOOST_REGEX_CALL imbue(locale_type l);
  476. locale_type BOOST_REGEX_CALL getloc()const
  477. {
  478. return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
  479. }
  480. //
  481. // getflags:
  482. // retained for backwards compatibility only, "flags"
  483. // is now the preferred name:
  484. flag_type BOOST_REGEX_CALL getflags()const
  485. {
  486. return flags();
  487. }
  488. flag_type BOOST_REGEX_CALL flags()const
  489. {
  490. return m_pimpl.get() ? m_pimpl->flags() : 0;
  491. }
  492. //
  493. // str:
  494. std::basic_string<charT> BOOST_REGEX_CALL str()const
  495. {
  496. return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
  497. }
  498. //
  499. // begin, end, subexpression:
  500. std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
  501. {
  502. if(!m_pimpl.get())
  503. throw std::logic_error("Can't access subexpressions in an invalid regex.");
  504. return m_pimpl->subexpression(n);
  505. }
  506. const_iterator BOOST_REGEX_CALL begin()const
  507. {
  508. return (m_pimpl.get() ? m_pimpl->begin() : 0);
  509. }
  510. const_iterator BOOST_REGEX_CALL end()const
  511. {
  512. return (m_pimpl.get() ? m_pimpl->end() : 0);
  513. }
  514. //
  515. // swap:
  516. void BOOST_REGEX_CALL swap(basic_regex& that)throw()
  517. {
  518. m_pimpl.swap(that.m_pimpl);
  519. }
  520. //
  521. // size:
  522. size_type BOOST_REGEX_CALL size()const
  523. {
  524. return (m_pimpl.get() ? m_pimpl->size() : 0);
  525. }
  526. //
  527. // max_size:
  528. size_type BOOST_REGEX_CALL max_size()const
  529. {
  530. return UINT_MAX;
  531. }
  532. //
  533. // empty:
  534. bool BOOST_REGEX_CALL empty()const
  535. {
  536. return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
  537. }
  538. size_type BOOST_REGEX_CALL mark_count()const
  539. {
  540. return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
  541. }
  542. int status()const
  543. {
  544. return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
  545. }
  546. int BOOST_REGEX_CALL compare(const basic_regex& that) const
  547. {
  548. if(m_pimpl.get() == that.m_pimpl.get())
  549. return 0;
  550. if(!m_pimpl.get())
  551. return -1;
  552. if(!that.m_pimpl.get())
  553. return 1;
  554. if(status() != that.status())
  555. return status() - that.status();
  556. if(flags() != that.flags())
  557. return flags() - that.flags();
  558. return str().compare(that.str());
  559. }
  560. bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
  561. {
  562. return compare(e) == 0;
  563. }
  564. bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
  565. {
  566. return compare(e) != 0;
  567. }
  568. bool BOOST_REGEX_CALL operator<(const basic_regex& e)const
  569. {
  570. return compare(e) < 0;
  571. }
  572. bool BOOST_REGEX_CALL operator>(const basic_regex& e)const
  573. {
  574. return compare(e) > 0;
  575. }
  576. bool BOOST_REGEX_CALL operator<=(const basic_regex& e)const
  577. {
  578. return compare(e) <= 0;
  579. }
  580. bool BOOST_REGEX_CALL operator>=(const basic_regex& e)const
  581. {
  582. return compare(e) >= 0;
  583. }
  584. //
  585. // The following are deprecated as public interfaces
  586. // but are available for compatibility with earlier versions.
  587. const charT* BOOST_REGEX_CALL expression()const
  588. {
  589. return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
  590. }
  591. unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
  592. {
  593. assign(p1, p2, f | regex_constants::no_except);
  594. return status();
  595. }
  596. unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal)
  597. {
  598. assign(p, f | regex_constants::no_except);
  599. return status();
  600. }
  601. unsigned int BOOST_REGEX_CALL error_code()const
  602. {
  603. return status();
  604. }
  605. //
  606. // private access methods:
  607. //
  608. const re_detail::re_syntax_base* get_first_state()const
  609. {
  610. BOOST_ASSERT(0 != m_pimpl.get());
  611. return m_pimpl->get_first_state();
  612. }
  613. unsigned get_restart_type()const
  614. {
  615. BOOST_ASSERT(0 != m_pimpl.get());
  616. return m_pimpl->get_restart_type();
  617. }
  618. const unsigned char* get_map()const
  619. {
  620. BOOST_ASSERT(0 != m_pimpl.get());
  621. return m_pimpl->get_map();
  622. }
  623. const ::boost::regex_traits_wrapper<traits>& get_traits()const
  624. {
  625. BOOST_ASSERT(0 != m_pimpl.get());
  626. return m_pimpl->get_traits();
  627. }
  628. bool can_be_null()const
  629. {
  630. BOOST_ASSERT(0 != m_pimpl.get());
  631. return m_pimpl->can_be_null();
  632. }
  633. const re_detail::regex_data<charT, traits>& get_data()const
  634. {
  635. BOOST_ASSERT(0 != m_pimpl.get());
  636. return m_pimpl->get_data();
  637. }
  638. boost::shared_ptr<re_detail::named_subexpressions<charT> > get_named_subs()const
  639. {
  640. return m_pimpl;
  641. }
  642. private:
  643. shared_ptr<re_detail::basic_regex_implementation<charT, traits> > m_pimpl;
  644. };
  645. //
  646. // out of line members;
  647. // these are the only members that mutate the basic_regex object,
  648. // and are designed to provide the strong exception guarentee
  649. // (in the event of a throw, the state of the object remains unchanged).
  650. //
  651. template <class charT, class traits>
  652. basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
  653. const charT* p2,
  654. flag_type f)
  655. {
  656. shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp;
  657. if(!m_pimpl.get())
  658. {
  659. temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>());
  660. }
  661. else
  662. {
  663. temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
  664. }
  665. temp->assign(p1, p2, f);
  666. temp.swap(m_pimpl);
  667. return *this;
  668. }
  669. template <class charT, class traits>
  670. typename basic_regex<charT, traits>::locale_type BOOST_REGEX_CALL basic_regex<charT, traits>::imbue(locale_type l)
  671. {
  672. shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp(new re_detail::basic_regex_implementation<charT, traits>());
  673. locale_type result = temp->imbue(l);
  674. temp.swap(m_pimpl);
  675. return result;
  676. }
  677. //
  678. // non-members:
  679. //
  680. template <class charT, class traits>
  681. void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
  682. {
  683. e1.swap(e2);
  684. }
  685. #ifndef BOOST_NO_STD_LOCALE
  686. template <class charT, class traits, class traits2>
  687. std::basic_ostream<charT, traits>&
  688. operator << (std::basic_ostream<charT, traits>& os,
  689. const basic_regex<charT, traits2>& e)
  690. {
  691. return (os << e.str());
  692. }
  693. #else
  694. template <class traits>
  695. std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
  696. {
  697. return (os << e.str());
  698. }
  699. #endif
  700. //
  701. // class reg_expression:
  702. // this is provided for backwards compatibility only,
  703. // it is deprecated, no not use!
  704. //
  705. #ifdef BOOST_REGEX_NO_FWD
  706. template <class charT, class traits = regex_traits<charT> >
  707. #else
  708. template <class charT, class traits >
  709. #endif
  710. class reg_expression : public basic_regex<charT, traits>
  711. {
  712. public:
  713. typedef typename basic_regex<charT, traits>::flag_type flag_type;
  714. typedef typename basic_regex<charT, traits>::size_type size_type;
  715. explicit reg_expression(){}
  716. explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
  717. : basic_regex<charT, traits>(p, f){}
  718. reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
  719. : basic_regex<charT, traits>(p1, p2, f){}
  720. reg_expression(const charT* p, size_type len, flag_type f)
  721. : basic_regex<charT, traits>(p, len, f){}
  722. reg_expression(const reg_expression& that)
  723. : basic_regex<charT, traits>(that) {}
  724. ~reg_expression(){}
  725. reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
  726. {
  727. return this->assign(that);
  728. }
  729. #if !defined(BOOST_NO_MEMBER_TEMPLATES)
  730. template <class ST, class SA>
  731. explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
  732. : basic_regex<charT, traits>(p, f)
  733. {
  734. }
  735. template <class InputIterator>
  736. reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
  737. : basic_regex<charT, traits>(arg_first, arg_last, f)
  738. {
  739. }
  740. template <class ST, class SA>
  741. reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
  742. {
  743. this->assign(p);
  744. return *this;
  745. }
  746. #else
  747. explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
  748. : basic_regex<charT, traits>(p, f)
  749. {
  750. }
  751. reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
  752. {
  753. this->assign(p);
  754. return *this;
  755. }
  756. #endif
  757. };
  758. #ifdef BOOST_MSVC
  759. #pragma warning (pop)
  760. #endif
  761. } // namespace boost
  762. #ifdef BOOST_MSVC
  763. #pragma warning(push)
  764. #pragma warning(disable: 4103)
  765. #endif
  766. #ifdef BOOST_HAS_ABI_HEADERS
  767. # include BOOST_ABI_SUFFIX
  768. #endif
  769. #ifdef BOOST_MSVC
  770. #pragma warning(pop)
  771. #endif
  772. #endif