regex_format.hpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. *
  3. * Copyright (c) 1998-2009 John Maddock
  4. * Copyright 2008 Eric Niebler.
  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 regex_format.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides formatting output routines for search and replace
  16. * operations. Note this is an internal header file included
  17. * by regex.hpp, do not include on its own.
  18. */
  19. #ifndef BOOST_REGEX_FORMAT_HPP
  20. #define BOOST_REGEX_FORMAT_HPP
  21. #include <type_traits>
  22. #include <functional>
  23. namespace boost{
  24. //
  25. // Forward declaration:
  26. //
  27. template <class BidiIterator, class Allocator = typename std::vector<sub_match<BidiIterator> >::allocator_type >
  28. class match_results;
  29. namespace BOOST_REGEX_DETAIL_NS{
  30. //
  31. // struct trivial_format_traits:
  32. // defines minimum localisation support for formatting
  33. // in the case that the actual regex traits is unavailable.
  34. //
  35. template <class charT>
  36. struct trivial_format_traits
  37. {
  38. typedef charT char_type;
  39. static std::ptrdiff_t length(const charT* p)
  40. {
  41. return global_length(p);
  42. }
  43. static charT tolower(charT c)
  44. {
  45. return ::boost::BOOST_REGEX_DETAIL_NS::global_lower(c);
  46. }
  47. static charT toupper(charT c)
  48. {
  49. return ::boost::BOOST_REGEX_DETAIL_NS::global_upper(c);
  50. }
  51. static int value(const charT c, int radix)
  52. {
  53. int result = global_value(c);
  54. return result >= radix ? -1 : result;
  55. }
  56. int toi(const charT*& p1, const charT* p2, int radix)const
  57. {
  58. return (int)global_toi(p1, p2, radix, *this);
  59. }
  60. };
  61. #ifdef BOOST_REGEX_MSVC
  62. # pragma warning(push)
  63. #pragma warning(disable:26812)
  64. #endif
  65. template <class OutputIterator, class Results, class traits, class ForwardIter>
  66. class basic_regex_formatter
  67. {
  68. public:
  69. typedef typename traits::char_type char_type;
  70. basic_regex_formatter(OutputIterator o, const Results& r, const traits& t)
  71. : m_traits(t), m_results(r), m_out(o), m_position(), m_end(), m_flags(), m_state(output_copy), m_restore_state(output_copy), m_have_conditional(false) {}
  72. OutputIterator format(ForwardIter p1, ForwardIter p2, match_flag_type f);
  73. OutputIterator format(ForwardIter p1, match_flag_type f)
  74. {
  75. return format(p1, p1 + m_traits.length(p1), f);
  76. }
  77. private:
  78. typedef typename Results::value_type sub_match_type;
  79. enum output_state
  80. {
  81. output_copy,
  82. output_next_lower,
  83. output_next_upper,
  84. output_lower,
  85. output_upper,
  86. output_none
  87. };
  88. void put(char_type c);
  89. void put(const sub_match_type& sub);
  90. void format_all();
  91. void format_perl();
  92. void format_escape();
  93. void format_conditional();
  94. void format_until_scope_end();
  95. bool handle_perl_verb(bool have_brace);
  96. inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j, const std::integral_constant<bool, false>&)
  97. {
  98. std::vector<char_type> v(i, j);
  99. return (i != j) ? this->m_results.named_subexpression(&v[0], &v[0] + v.size())
  100. : this->m_results.named_subexpression(static_cast<const char_type*>(0), static_cast<const char_type*>(0));
  101. }
  102. inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j, const std::integral_constant<bool, true>&)
  103. {
  104. return this->m_results.named_subexpression(i, j);
  105. }
  106. inline typename Results::value_type const& get_named_sub(ForwardIter i, ForwardIter j)
  107. {
  108. typedef typename std::is_convertible<ForwardIter, const char_type*>::type tag_type;
  109. return get_named_sub(i, j, tag_type());
  110. }
  111. inline int get_named_sub_index(ForwardIter i, ForwardIter j, const std::integral_constant<bool, false>&)
  112. {
  113. std::vector<char_type> v(i, j);
  114. return (i != j) ? this->m_results.named_subexpression_index(&v[0], &v[0] + v.size())
  115. : this->m_results.named_subexpression_index(static_cast<const char_type*>(0), static_cast<const char_type*>(0));
  116. }
  117. inline int get_named_sub_index(ForwardIter i, ForwardIter j, const std::integral_constant<bool, true>&)
  118. {
  119. return this->m_results.named_subexpression_index(i, j);
  120. }
  121. inline int get_named_sub_index(ForwardIter i, ForwardIter j)
  122. {
  123. typedef typename std::is_convertible<ForwardIter, const char_type*>::type tag_type;
  124. return get_named_sub_index(i, j, tag_type());
  125. }
  126. #ifdef BOOST_REGEX_MSVC
  127. // msvc-8.0 issues a spurious warning on the call to std::advance here:
  128. #pragma warning(push)
  129. #pragma warning(disable:4244)
  130. #endif
  131. inline int toi(ForwardIter& i, ForwardIter j, int base, const std::integral_constant<bool, false>&)
  132. {
  133. if(i != j)
  134. {
  135. std::vector<char_type> v(i, j);
  136. const char_type* start = &v[0];
  137. const char_type* pos = start;
  138. int r = (int)m_traits.toi(pos, &v[0] + v.size(), base);
  139. std::advance(i, pos - start);
  140. return r;
  141. }
  142. return -1;
  143. }
  144. #ifdef BOOST_REGEX_MSVC
  145. #pragma warning(pop)
  146. #endif
  147. inline int toi(ForwardIter& i, ForwardIter j, int base, const std::integral_constant<bool, true>&)
  148. {
  149. return m_traits.toi(i, j, base);
  150. }
  151. inline int toi(ForwardIter& i, ForwardIter j, int base)
  152. {
  153. #if defined(_MSC_VER) && defined(__INTEL_COMPILER) && ((__INTEL_COMPILER == 9999) || (__INTEL_COMPILER == 1210))
  154. // Workaround for Intel support issue #656654.
  155. // See also https://svn.boost.org/trac/boost/ticket/6359
  156. return toi(i, j, base, std::integral_constant<bool, false>());
  157. #else
  158. typedef typename std::is_convertible<ForwardIter, const char_type*&>::type tag_type;
  159. return toi(i, j, base, tag_type());
  160. #endif
  161. }
  162. const traits& m_traits; // the traits class for localised formatting operations
  163. const Results& m_results; // the match_results being used.
  164. OutputIterator m_out; // where to send output.
  165. ForwardIter m_position; // format string, current position
  166. ForwardIter m_end; // format string end
  167. match_flag_type m_flags; // format flags to use
  168. output_state m_state; // what to do with the next character
  169. output_state m_restore_state; // what state to restore to.
  170. bool m_have_conditional; // we are parsing a conditional
  171. private:
  172. basic_regex_formatter(const basic_regex_formatter&);
  173. basic_regex_formatter& operator=(const basic_regex_formatter&);
  174. };
  175. #ifdef BOOST_REGEX_MSVC
  176. # pragma warning(pop)
  177. #endif
  178. template <class OutputIterator, class Results, class traits, class ForwardIter>
  179. OutputIterator basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format(ForwardIter p1, ForwardIter p2, match_flag_type f)
  180. {
  181. m_position = p1;
  182. m_end = p2;
  183. m_flags = f;
  184. format_all();
  185. return m_out;
  186. }
  187. template <class OutputIterator, class Results, class traits, class ForwardIter>
  188. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_all()
  189. {
  190. // over and over:
  191. while(m_position != m_end)
  192. {
  193. switch(*m_position)
  194. {
  195. case '&':
  196. if(m_flags & ::boost::regex_constants::format_sed)
  197. {
  198. ++m_position;
  199. put(m_results[0]);
  200. break;
  201. }
  202. put(*m_position++);
  203. break;
  204. case '\\':
  205. format_escape();
  206. break;
  207. case '(':
  208. if(m_flags & boost::regex_constants::format_all)
  209. {
  210. ++m_position;
  211. bool have_conditional = m_have_conditional;
  212. m_have_conditional = false;
  213. format_until_scope_end();
  214. m_have_conditional = have_conditional;
  215. if(m_position == m_end)
  216. return;
  217. BOOST_REGEX_ASSERT(*m_position == static_cast<char_type>(')'));
  218. ++m_position; // skip the closing ')'
  219. break;
  220. }
  221. put(*m_position);
  222. ++m_position;
  223. break;
  224. case ')':
  225. if(m_flags & boost::regex_constants::format_all)
  226. {
  227. return;
  228. }
  229. put(*m_position);
  230. ++m_position;
  231. break;
  232. case ':':
  233. if((m_flags & boost::regex_constants::format_all) && m_have_conditional)
  234. {
  235. return;
  236. }
  237. put(*m_position);
  238. ++m_position;
  239. break;
  240. case '?':
  241. if(m_flags & boost::regex_constants::format_all)
  242. {
  243. ++m_position;
  244. format_conditional();
  245. break;
  246. }
  247. put(*m_position);
  248. ++m_position;
  249. break;
  250. case '$':
  251. if((m_flags & format_sed) == 0)
  252. {
  253. format_perl();
  254. break;
  255. }
  256. // not a special character:
  257. BOOST_REGEX_FALLTHROUGH;
  258. default:
  259. put(*m_position);
  260. ++m_position;
  261. break;
  262. }
  263. }
  264. }
  265. template <class OutputIterator, class Results, class traits, class ForwardIter>
  266. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_perl()
  267. {
  268. //
  269. // On entry *m_position points to a '$' character
  270. // output the information that goes with it:
  271. //
  272. BOOST_REGEX_ASSERT(*m_position == '$');
  273. //
  274. // see if this is a trailing '$':
  275. //
  276. if(++m_position == m_end)
  277. {
  278. --m_position;
  279. put(*m_position);
  280. ++m_position;
  281. return;
  282. }
  283. //
  284. // OK find out what kind it is:
  285. //
  286. bool have_brace = false;
  287. ForwardIter save_position = m_position;
  288. switch(*m_position)
  289. {
  290. case '&':
  291. ++m_position;
  292. put(this->m_results[0]);
  293. break;
  294. case '`':
  295. ++m_position;
  296. put(this->m_results.prefix());
  297. break;
  298. case '\'':
  299. ++m_position;
  300. put(this->m_results.suffix());
  301. break;
  302. case '$':
  303. put(*m_position++);
  304. break;
  305. case '+':
  306. if((++m_position != m_end) && (*m_position == '{'))
  307. {
  308. ForwardIter base = ++m_position;
  309. while((m_position != m_end) && (*m_position != '}')) ++m_position;
  310. if(m_position != m_end)
  311. {
  312. // Named sub-expression:
  313. put(get_named_sub(base, m_position));
  314. ++m_position;
  315. break;
  316. }
  317. else
  318. {
  319. m_position = --base;
  320. }
  321. }
  322. put((this->m_results)[this->m_results.size() > 1 ? static_cast<int>(this->m_results.size() - 1) : 1]);
  323. break;
  324. case '{':
  325. have_brace = true;
  326. ++m_position;
  327. BOOST_REGEX_FALLTHROUGH;
  328. default:
  329. // see if we have a number:
  330. {
  331. std::ptrdiff_t len = std::distance(m_position, m_end);
  332. //len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
  333. int v = this->toi(m_position, m_position + len, 10);
  334. if((v < 0) || (have_brace && ((m_position == m_end) || (*m_position != '}'))))
  335. {
  336. // Look for a Perl-5.10 verb:
  337. if(!handle_perl_verb(have_brace))
  338. {
  339. // leave the $ as is, and carry on:
  340. m_position = --save_position;
  341. put(*m_position);
  342. ++m_position;
  343. }
  344. break;
  345. }
  346. // otherwise output sub v:
  347. put(this->m_results[v]);
  348. if(have_brace)
  349. ++m_position;
  350. }
  351. }
  352. }
  353. template <class OutputIterator, class Results, class traits, class ForwardIter>
  354. bool basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::handle_perl_verb(bool have_brace)
  355. {
  356. //
  357. // We may have a capitalised string containing a Perl action:
  358. //
  359. static const char_type MATCH[] = { 'M', 'A', 'T', 'C', 'H' };
  360. static const char_type PREMATCH[] = { 'P', 'R', 'E', 'M', 'A', 'T', 'C', 'H' };
  361. static const char_type POSTMATCH[] = { 'P', 'O', 'S', 'T', 'M', 'A', 'T', 'C', 'H' };
  362. static const char_type LAST_PAREN_MATCH[] = { 'L', 'A', 'S', 'T', '_', 'P', 'A', 'R', 'E', 'N', '_', 'M', 'A', 'T', 'C', 'H' };
  363. static const char_type LAST_SUBMATCH_RESULT[] = { 'L', 'A', 'S', 'T', '_', 'S', 'U', 'B', 'M', 'A', 'T', 'C', 'H', '_', 'R', 'E', 'S', 'U', 'L', 'T' };
  364. static const char_type LAST_SUBMATCH_RESULT_ALT[] = { '^', 'N' };
  365. if(m_position == m_end)
  366. return false;
  367. if(have_brace && (*m_position == '^'))
  368. ++m_position;
  369. std::ptrdiff_t max_len = m_end - m_position;
  370. if((max_len >= 5) && std::equal(m_position, m_position + 5, MATCH))
  371. {
  372. m_position += 5;
  373. if(have_brace)
  374. {
  375. if((m_position != m_end) && (*m_position == '}'))
  376. ++m_position;
  377. else
  378. {
  379. m_position -= 5;
  380. return false;
  381. }
  382. }
  383. put(this->m_results[0]);
  384. return true;
  385. }
  386. if((max_len >= 8) && std::equal(m_position, m_position + 8, PREMATCH))
  387. {
  388. m_position += 8;
  389. if(have_brace)
  390. {
  391. if((m_position != m_end) && (*m_position == '}'))
  392. ++m_position;
  393. else
  394. {
  395. m_position -= 8;
  396. return false;
  397. }
  398. }
  399. put(this->m_results.prefix());
  400. return true;
  401. }
  402. if((max_len >= 9) && std::equal(m_position, m_position + 9, POSTMATCH))
  403. {
  404. m_position += 9;
  405. if(have_brace)
  406. {
  407. if((m_position != m_end) && (*m_position == '}'))
  408. ++m_position;
  409. else
  410. {
  411. m_position -= 9;
  412. return false;
  413. }
  414. }
  415. put(this->m_results.suffix());
  416. return true;
  417. }
  418. if((max_len >= 16) && std::equal(m_position, m_position + 16, LAST_PAREN_MATCH))
  419. {
  420. m_position += 16;
  421. if(have_brace)
  422. {
  423. if((m_position != m_end) && (*m_position == '}'))
  424. ++m_position;
  425. else
  426. {
  427. m_position -= 16;
  428. return false;
  429. }
  430. }
  431. put((this->m_results)[this->m_results.size() > 1 ? static_cast<int>(this->m_results.size() - 1) : 1]);
  432. return true;
  433. }
  434. if((max_len >= 20) && std::equal(m_position, m_position + 20, LAST_SUBMATCH_RESULT))
  435. {
  436. m_position += 20;
  437. if(have_brace)
  438. {
  439. if((m_position != m_end) && (*m_position == '}'))
  440. ++m_position;
  441. else
  442. {
  443. m_position -= 20;
  444. return false;
  445. }
  446. }
  447. put(this->m_results.get_last_closed_paren());
  448. return true;
  449. }
  450. if((max_len >= 2) && std::equal(m_position, m_position + 2, LAST_SUBMATCH_RESULT_ALT))
  451. {
  452. m_position += 2;
  453. if(have_brace)
  454. {
  455. if((m_position != m_end) && (*m_position == '}'))
  456. ++m_position;
  457. else
  458. {
  459. m_position -= 2;
  460. return false;
  461. }
  462. }
  463. put(this->m_results.get_last_closed_paren());
  464. return true;
  465. }
  466. return false;
  467. }
  468. template <class OutputIterator, class Results, class traits, class ForwardIter>
  469. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_escape()
  470. {
  471. // skip the escape and check for trailing escape:
  472. if(++m_position == m_end)
  473. {
  474. put(static_cast<char_type>('\\'));
  475. return;
  476. }
  477. // now switch on the escape type:
  478. switch(*m_position)
  479. {
  480. case 'a':
  481. put(static_cast<char_type>('\a'));
  482. ++m_position;
  483. break;
  484. case 'f':
  485. put(static_cast<char_type>('\f'));
  486. ++m_position;
  487. break;
  488. case 'n':
  489. put(static_cast<char_type>('\n'));
  490. ++m_position;
  491. break;
  492. case 'r':
  493. put(static_cast<char_type>('\r'));
  494. ++m_position;
  495. break;
  496. case 't':
  497. put(static_cast<char_type>('\t'));
  498. ++m_position;
  499. break;
  500. case 'v':
  501. put(static_cast<char_type>('\v'));
  502. ++m_position;
  503. break;
  504. case 'x':
  505. if(++m_position == m_end)
  506. {
  507. put(static_cast<char_type>('x'));
  508. return;
  509. }
  510. // maybe have \x{ddd}
  511. if(*m_position == static_cast<char_type>('{'))
  512. {
  513. ++m_position;
  514. int val = this->toi(m_position, m_end, 16);
  515. if(val < 0)
  516. {
  517. // invalid value treat everything as literals:
  518. put(static_cast<char_type>('x'));
  519. put(static_cast<char_type>('{'));
  520. return;
  521. }
  522. if((m_position == m_end) || (*m_position != static_cast<char_type>('}')))
  523. {
  524. --m_position;
  525. while(*m_position != static_cast<char_type>('\\'))
  526. --m_position;
  527. ++m_position;
  528. put(*m_position++);
  529. return;
  530. }
  531. ++m_position;
  532. put(static_cast<char_type>(val));
  533. return;
  534. }
  535. else
  536. {
  537. std::ptrdiff_t len = std::distance(m_position, m_end);
  538. len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
  539. int val = this->toi(m_position, m_position + len, 16);
  540. if(val < 0)
  541. {
  542. --m_position;
  543. put(*m_position++);
  544. return;
  545. }
  546. put(static_cast<char_type>(val));
  547. }
  548. break;
  549. case 'c':
  550. if(++m_position == m_end)
  551. {
  552. --m_position;
  553. put(*m_position++);
  554. return;
  555. }
  556. put(static_cast<char_type>(*m_position++ % 32));
  557. break;
  558. case 'e':
  559. put(static_cast<char_type>(27));
  560. ++m_position;
  561. break;
  562. default:
  563. // see if we have a perl specific escape:
  564. if((m_flags & boost::regex_constants::format_sed) == 0)
  565. {
  566. bool breakout = false;
  567. switch(*m_position)
  568. {
  569. case 'l':
  570. ++m_position;
  571. m_restore_state = m_state;
  572. m_state = output_next_lower;
  573. breakout = true;
  574. break;
  575. case 'L':
  576. ++m_position;
  577. m_state = output_lower;
  578. breakout = true;
  579. break;
  580. case 'u':
  581. ++m_position;
  582. m_restore_state = m_state;
  583. m_state = output_next_upper;
  584. breakout = true;
  585. break;
  586. case 'U':
  587. ++m_position;
  588. m_state = output_upper;
  589. breakout = true;
  590. break;
  591. case 'E':
  592. ++m_position;
  593. m_state = output_copy;
  594. breakout = true;
  595. break;
  596. }
  597. if(breakout)
  598. break;
  599. }
  600. // see if we have a \n sed style backreference:
  601. std::ptrdiff_t len = std::distance(m_position, m_end);
  602. len = (std::min)(static_cast<std::ptrdiff_t>(1), len);
  603. int v = this->toi(m_position, m_position+len, 10);
  604. if((v > 0) || ((v == 0) && (m_flags & ::boost::regex_constants::format_sed)))
  605. {
  606. put(m_results[v]);
  607. break;
  608. }
  609. else if(v == 0)
  610. {
  611. // octal ecape sequence:
  612. --m_position;
  613. len = std::distance(m_position, m_end);
  614. len = (std::min)(static_cast<std::ptrdiff_t>(4), len);
  615. v = this->toi(m_position, m_position + len, 8);
  616. BOOST_REGEX_ASSERT(v >= 0);
  617. put(static_cast<char_type>(v));
  618. break;
  619. }
  620. // Otherwise output the character "as is":
  621. put(*m_position++);
  622. break;
  623. }
  624. }
  625. template <class OutputIterator, class Results, class traits, class ForwardIter>
  626. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_conditional()
  627. {
  628. if(m_position == m_end)
  629. {
  630. // oops trailing '?':
  631. put(static_cast<char_type>('?'));
  632. return;
  633. }
  634. int v;
  635. if(*m_position == '{')
  636. {
  637. ForwardIter base = m_position;
  638. ++m_position;
  639. v = this->toi(m_position, m_end, 10);
  640. if(v < 0)
  641. {
  642. // Try a named subexpression:
  643. while((m_position != m_end) && (*m_position != '}'))
  644. ++m_position;
  645. v = this->get_named_sub_index(base + 1, m_position);
  646. }
  647. if((v < 0) || (*m_position != '}'))
  648. {
  649. m_position = base;
  650. // oops trailing '?':
  651. put(static_cast<char_type>('?'));
  652. return;
  653. }
  654. // Skip trailing '}':
  655. ++m_position;
  656. }
  657. else
  658. {
  659. std::ptrdiff_t len = std::distance(m_position, m_end);
  660. len = (std::min)(static_cast<std::ptrdiff_t>(2), len);
  661. v = this->toi(m_position, m_position + len, 10);
  662. }
  663. if(v < 0)
  664. {
  665. // oops not a number:
  666. put(static_cast<char_type>('?'));
  667. return;
  668. }
  669. // output varies depending upon whether sub-expression v matched or not:
  670. if(m_results[v].matched)
  671. {
  672. m_have_conditional = true;
  673. format_all();
  674. m_have_conditional = false;
  675. if((m_position != m_end) && (*m_position == static_cast<char_type>(':')))
  676. {
  677. // skip the ':':
  678. ++m_position;
  679. // save output state, then turn it off:
  680. output_state saved_state = m_state;
  681. m_state = output_none;
  682. // format the rest of this scope:
  683. format_until_scope_end();
  684. // restore output state:
  685. m_state = saved_state;
  686. }
  687. }
  688. else
  689. {
  690. // save output state, then turn it off:
  691. output_state saved_state = m_state;
  692. m_state = output_none;
  693. // format until ':' or ')':
  694. m_have_conditional = true;
  695. format_all();
  696. m_have_conditional = false;
  697. // restore state:
  698. m_state = saved_state;
  699. if((m_position != m_end) && (*m_position == static_cast<char_type>(':')))
  700. {
  701. // skip the ':':
  702. ++m_position;
  703. // format the rest of this scope:
  704. format_until_scope_end();
  705. }
  706. }
  707. }
  708. template <class OutputIterator, class Results, class traits, class ForwardIter>
  709. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::format_until_scope_end()
  710. {
  711. do
  712. {
  713. format_all();
  714. if((m_position == m_end) || (*m_position == static_cast<char_type>(')')))
  715. return;
  716. put(*m_position++);
  717. }while(m_position != m_end);
  718. }
  719. template <class OutputIterator, class Results, class traits, class ForwardIter>
  720. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::put(char_type c)
  721. {
  722. // write a single character to output
  723. // according to which case translation mode we are in:
  724. switch(this->m_state)
  725. {
  726. case output_none:
  727. return;
  728. case output_next_lower:
  729. c = m_traits.tolower(c);
  730. this->m_state = m_restore_state;
  731. break;
  732. case output_next_upper:
  733. c = m_traits.toupper(c);
  734. this->m_state = m_restore_state;
  735. break;
  736. case output_lower:
  737. c = m_traits.tolower(c);
  738. break;
  739. case output_upper:
  740. c = m_traits.toupper(c);
  741. break;
  742. default:
  743. break;
  744. }
  745. *m_out = c;
  746. ++m_out;
  747. }
  748. template <class OutputIterator, class Results, class traits, class ForwardIter>
  749. void basic_regex_formatter<OutputIterator, Results, traits, ForwardIter>::put(const sub_match_type& sub)
  750. {
  751. typedef typename sub_match_type::iterator iterator_type;
  752. iterator_type i = sub.first;
  753. while(i != sub.second)
  754. {
  755. put(*i);
  756. ++i;
  757. }
  758. }
  759. template <class S>
  760. class string_out_iterator
  761. {
  762. S* out;
  763. public:
  764. string_out_iterator(S& s) : out(&s) {}
  765. string_out_iterator& operator++() { return *this; }
  766. string_out_iterator& operator++(int) { return *this; }
  767. string_out_iterator& operator*() { return *this; }
  768. string_out_iterator& operator=(typename S::value_type v)
  769. {
  770. out->append(1, v);
  771. return *this;
  772. }
  773. typedef std::ptrdiff_t difference_type;
  774. typedef typename S::value_type value_type;
  775. typedef value_type* pointer;
  776. typedef value_type& reference;
  777. typedef std::output_iterator_tag iterator_category;
  778. };
  779. template <class OutputIterator, class Iterator, class Alloc, class ForwardIter, class traits>
  780. OutputIterator regex_format_imp(OutputIterator out,
  781. const match_results<Iterator, Alloc>& m,
  782. ForwardIter p1, ForwardIter p2,
  783. match_flag_type flags,
  784. const traits& t
  785. )
  786. {
  787. if(flags & regex_constants::format_literal)
  788. {
  789. return BOOST_REGEX_DETAIL_NS::copy(p1, p2, out);
  790. }
  791. BOOST_REGEX_DETAIL_NS::basic_regex_formatter<
  792. OutputIterator,
  793. match_results<Iterator, Alloc>,
  794. traits, ForwardIter> f(out, m, t);
  795. return f.format(p1, p2, flags);
  796. }
  797. template <class T>
  798. struct has_const_iterator
  799. {
  800. template <class U>
  801. static typename U::const_iterator tester(U*);
  802. static char tester(...);
  803. static T* get();
  804. static const bool value = sizeof(tester(get())) != sizeof(char);
  805. };
  806. struct any_type
  807. {
  808. template <class T>
  809. any_type(const T&);
  810. template <class T, class U>
  811. any_type(const T&, const U&);
  812. template <class T, class U, class V>
  813. any_type(const T&, const U&, const V&);
  814. };
  815. typedef char no_type;
  816. typedef char (&unary_type)[2];
  817. typedef char (&binary_type)[3];
  818. typedef char (&ternary_type)[4];
  819. no_type check_is_formatter(unary_type, binary_type, ternary_type);
  820. template<typename T>
  821. unary_type check_is_formatter(T const &, binary_type, ternary_type);
  822. template<typename T>
  823. binary_type check_is_formatter(unary_type, T const &, ternary_type);
  824. template<typename T, typename U>
  825. binary_type check_is_formatter(T const &, U const &, ternary_type);
  826. template<typename T>
  827. ternary_type check_is_formatter(unary_type, binary_type, T const &);
  828. template<typename T, typename U>
  829. ternary_type check_is_formatter(T const &, binary_type, U const &);
  830. template<typename T, typename U>
  831. ternary_type check_is_formatter(unary_type, T const &, U const &);
  832. template<typename T, typename U, typename V>
  833. ternary_type check_is_formatter(T const &, U const &, V const &);
  834. struct unary_binary_ternary
  835. {
  836. typedef unary_type (*unary_fun)(any_type);
  837. typedef binary_type (*binary_fun)(any_type, any_type);
  838. typedef ternary_type (*ternary_fun)(any_type, any_type, any_type);
  839. operator unary_fun();
  840. operator binary_fun();
  841. operator ternary_fun();
  842. };
  843. template<typename Formatter, bool IsFunction = std::is_function<Formatter>::value>
  844. struct formatter_wrapper
  845. : Formatter
  846. , unary_binary_ternary
  847. {
  848. formatter_wrapper(){}
  849. };
  850. template<typename Formatter>
  851. struct formatter_wrapper<Formatter, true>
  852. : unary_binary_ternary
  853. {
  854. operator Formatter *();
  855. };
  856. template<typename Formatter>
  857. struct formatter_wrapper<Formatter *, false>
  858. : unary_binary_ternary
  859. {
  860. operator Formatter *();
  861. };
  862. template <class T>
  863. struct do_unwrap_reference
  864. {
  865. typedef T type;
  866. };
  867. template <class T>
  868. struct do_unwrap_reference<std::reference_wrapper<T> >
  869. {
  870. typedef T type;
  871. };
  872. template <class T>
  873. T& do_unwrap_ref(T& r) { return r; }
  874. template <class T>
  875. T& do_unwrap_ref(std::reference_wrapper<T> const& r) { return r.get(); }
  876. template <class F, class M, class O>
  877. struct format_traits_imp
  878. {
  879. private:
  880. //
  881. // F must be a pointer, a function, or a class with a function call operator:
  882. //
  883. static_assert((::std::is_pointer<F>::value || ::std::is_function<F>::value || ::std::is_class<F>::value), "The functor must be a pointer or a class with a function call operator");
  884. static formatter_wrapper<typename do_unwrap_reference<F>::type> f;
  885. static M m;
  886. static O out;
  887. static boost::regex_constants::match_flag_type flags;
  888. public:
  889. static const int value = sizeof(check_is_formatter(f(m), f(m, out), f(m, out, flags)));
  890. };
  891. template <class F, class M, class O>
  892. struct format_traits
  893. {
  894. public:
  895. //
  896. // Type is std::integral_constant<int, N> where N is one of:
  897. //
  898. // 0 : F is a pointer to a presumably null-terminated string.
  899. // 1 : F is a character-container such as a std::string.
  900. // 2 : F is a Unary Functor.
  901. // 3 : F is a Binary Functor.
  902. // 4 : F is a Ternary Functor.
  903. //
  904. typedef typename std::conditional<
  905. std::is_pointer<F>::value && !std::is_function<typename std::remove_pointer<F>::type>::value,
  906. std::integral_constant<int, 0>,
  907. typename std::conditional<
  908. has_const_iterator<F>::value,
  909. std::integral_constant<int, 1>,
  910. std::integral_constant<int, format_traits_imp<F, M, O>::value>
  911. >::type
  912. >::type type;
  913. //
  914. // This static assertion will fail if the functor passed does not accept
  915. // the same type of arguments passed.
  916. //
  917. static_assert( std::is_class<F>::value && !has_const_iterator<F>::value ? (type::value > 1) : true, "Argument mismatch in Functor type");
  918. };
  919. template <class Base, class Match>
  920. struct format_functor3
  921. {
  922. format_functor3(Base b) : func(b) {}
  923. template <class OutputIter>
  924. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f)
  925. {
  926. return do_unwrap_ref(func)(m, i, f);
  927. }
  928. template <class OutputIter, class Traits>
  929. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
  930. {
  931. return (*this)(m, i, f);
  932. }
  933. private:
  934. Base func;
  935. format_functor3(const format_functor3&);
  936. format_functor3& operator=(const format_functor3&);
  937. };
  938. template <class Base, class Match>
  939. struct format_functor2
  940. {
  941. format_functor2(Base b) : func(b) {}
  942. template <class OutputIter>
  943. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type /*f*/)
  944. {
  945. return do_unwrap_ref(func)(m, i);
  946. }
  947. template <class OutputIter, class Traits>
  948. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
  949. {
  950. return (*this)(m, i, f);
  951. }
  952. private:
  953. Base func;
  954. format_functor2(const format_functor2&);
  955. format_functor2& operator=(const format_functor2&);
  956. };
  957. template <class Base, class Match>
  958. struct format_functor1
  959. {
  960. format_functor1(Base b) : func(b) {}
  961. template <class S, class OutputIter>
  962. OutputIter do_format_string(const S& s, OutputIter i)
  963. {
  964. return std::copy(s.begin(), s.end(), i);
  965. }
  966. template <class S, class OutputIter>
  967. inline OutputIter do_format_string(const S* s, OutputIter i)
  968. {
  969. while(s && *s)
  970. {
  971. *i = *s;
  972. ++i;
  973. ++s;
  974. }
  975. return i;
  976. }
  977. template <class OutputIter>
  978. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type /*f*/)
  979. {
  980. return do_format_string(do_unwrap_ref(func)(m), i);
  981. }
  982. template <class OutputIter, class Traits>
  983. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits&)
  984. {
  985. return (*this)(m, i, f);
  986. }
  987. private:
  988. Base func;
  989. format_functor1(const format_functor1&);
  990. format_functor1& operator=(const format_functor1&);
  991. };
  992. template <class charT, class Match, class Traits>
  993. struct format_functor_c_string
  994. {
  995. format_functor_c_string(const charT* ps) : func(ps) {}
  996. template <class OutputIter>
  997. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits& t = Traits())
  998. {
  999. //typedef typename Match::char_type char_type;
  1000. const charT* end = func;
  1001. while(*end) ++end;
  1002. return regex_format_imp(i, m, func, end, f, t);
  1003. }
  1004. private:
  1005. const charT* func;
  1006. format_functor_c_string(const format_functor_c_string&);
  1007. format_functor_c_string& operator=(const format_functor_c_string&);
  1008. };
  1009. template <class Container, class Match, class Traits>
  1010. struct format_functor_container
  1011. {
  1012. format_functor_container(const Container& c) : func(c) {}
  1013. template <class OutputIter>
  1014. OutputIter operator()(const Match& m, OutputIter i, boost::regex_constants::match_flag_type f, const Traits& t = Traits())
  1015. {
  1016. //typedef typename Match::char_type char_type;
  1017. return BOOST_REGEX_DETAIL_NS::regex_format_imp(i, m, func.begin(), func.end(), f, t);
  1018. }
  1019. private:
  1020. const Container& func;
  1021. format_functor_container(const format_functor_container&);
  1022. format_functor_container& operator=(const format_functor_container&);
  1023. };
  1024. template <class Func, class Match, class OutputIterator, class Traits = BOOST_REGEX_DETAIL_NS::trivial_format_traits<typename Match::char_type> >
  1025. struct compute_functor_type
  1026. {
  1027. typedef typename format_traits<Func, Match, OutputIterator>::type tag;
  1028. typedef typename std::remove_cv< typename std::remove_pointer<Func>::type>::type maybe_char_type;
  1029. typedef typename std::conditional<
  1030. tag::value == 0, format_functor_c_string<maybe_char_type, Match, Traits>,
  1031. typename std::conditional<
  1032. tag::value == 1, format_functor_container<Func, Match, Traits>,
  1033. typename std::conditional<
  1034. tag::value == 2, format_functor1<Func, Match>,
  1035. typename std::conditional<
  1036. tag::value == 3, format_functor2<Func, Match>,
  1037. format_functor3<Func, Match>
  1038. >::type
  1039. >::type
  1040. >::type
  1041. >::type type;
  1042. };
  1043. } // namespace BOOST_REGEX_DETAIL_NS
  1044. template <class OutputIterator, class Iterator, class Allocator, class Functor>
  1045. inline OutputIterator regex_format(OutputIterator out,
  1046. const match_results<Iterator, Allocator>& m,
  1047. Functor fmt,
  1048. match_flag_type flags = format_all
  1049. )
  1050. {
  1051. return m.format(out, fmt, flags);
  1052. }
  1053. template <class Iterator, class Allocator, class Functor>
  1054. inline std::basic_string<typename match_results<Iterator, Allocator>::char_type> regex_format(const match_results<Iterator, Allocator>& m,
  1055. Functor fmt,
  1056. match_flag_type flags = format_all)
  1057. {
  1058. return m.format(fmt, flags);
  1059. }
  1060. } // namespace boost
  1061. #endif // BOOST_REGEX_FORMAT_HPP