basic_regex.hpp 22 KB

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