sub_match.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. *
  3. * Copyright (c) 1998-2002
  4. * John Maddock
  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 sub_match.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares template class sub_match.
  16. */
  17. #ifndef BOOST_REGEX_V5_SUB_MATCH_HPP
  18. #define BOOST_REGEX_V5_SUB_MATCH_HPP
  19. namespace boost{
  20. template <class BidiIterator>
  21. struct sub_match : public std::pair<BidiIterator, BidiIterator>
  22. {
  23. typedef typename std::iterator_traits<BidiIterator>::value_type value_type;
  24. typedef typename std::iterator_traits<BidiIterator>::difference_type difference_type;
  25. typedef BidiIterator iterator_type;
  26. typedef BidiIterator iterator;
  27. typedef BidiIterator const_iterator;
  28. bool matched;
  29. sub_match() : std::pair<BidiIterator, BidiIterator>(), matched(false) {}
  30. sub_match(BidiIterator i) : std::pair<BidiIterator, BidiIterator>(i, i), matched(false) {}
  31. template <class T, class A>
  32. operator std::basic_string<value_type, T, A> ()const
  33. {
  34. return matched ? std::basic_string<value_type, T, A>(this->first, this->second) : std::basic_string<value_type, T, A>();
  35. }
  36. difference_type length()const
  37. {
  38. difference_type n = matched ? std::distance((BidiIterator)this->first, (BidiIterator)this->second) : 0;
  39. return n;
  40. }
  41. std::basic_string<value_type> str()const
  42. {
  43. std::basic_string<value_type> result;
  44. if(matched)
  45. {
  46. std::size_t len = std::distance((BidiIterator)this->first, (BidiIterator)this->second);
  47. result.reserve(len);
  48. BidiIterator i = this->first;
  49. while(i != this->second)
  50. {
  51. result.append(1, *i);
  52. ++i;
  53. }
  54. }
  55. return result;
  56. }
  57. int compare(const sub_match& s)const
  58. {
  59. if(matched != s.matched)
  60. return static_cast<int>(matched) - static_cast<int>(s.matched);
  61. return str().compare(s.str());
  62. }
  63. int compare(const std::basic_string<value_type>& s)const
  64. {
  65. return str().compare(s);
  66. }
  67. int compare(const value_type* p)const
  68. {
  69. return str().compare(p);
  70. }
  71. bool operator==(const sub_match& that)const
  72. { return compare(that) == 0; }
  73. bool operator !=(const sub_match& that)const
  74. { return compare(that) != 0; }
  75. bool operator<(const sub_match& that)const
  76. { return compare(that) < 0; }
  77. bool operator>(const sub_match& that)const
  78. { return compare(that) > 0; }
  79. bool operator<=(const sub_match& that)const
  80. { return compare(that) <= 0; }
  81. bool operator>=(const sub_match& that)const
  82. { return compare(that) >= 0; }
  83. #ifdef BOOST_REGEX_MATCH_EXTRA
  84. typedef std::vector<sub_match<BidiIterator> > capture_sequence_type;
  85. const capture_sequence_type& captures()const
  86. {
  87. if(!m_captures)
  88. m_captures.reset(new capture_sequence_type());
  89. return *m_captures;
  90. }
  91. //
  92. // Private implementation API: DO NOT USE!
  93. //
  94. capture_sequence_type& get_captures()const
  95. {
  96. if(!m_captures)
  97. m_captures.reset(new capture_sequence_type());
  98. return *m_captures;
  99. }
  100. private:
  101. mutable std::unique_ptr<capture_sequence_type> m_captures;
  102. public:
  103. #endif
  104. sub_match(const sub_match& that, bool
  105. #ifdef BOOST_REGEX_MATCH_EXTRA
  106. deep_copy
  107. #endif
  108. = true
  109. )
  110. : std::pair<BidiIterator, BidiIterator>(that),
  111. matched(that.matched)
  112. {
  113. #ifdef BOOST_REGEX_MATCH_EXTRA
  114. if(that.m_captures)
  115. if(deep_copy)
  116. m_captures.reset(new capture_sequence_type(*(that.m_captures)));
  117. #endif
  118. }
  119. sub_match& operator=(const sub_match& that)
  120. {
  121. this->first = that.first;
  122. this->second = that.second;
  123. matched = that.matched;
  124. #ifdef BOOST_REGEX_MATCH_EXTRA
  125. if(that.m_captures)
  126. get_captures() = *(that.m_captures);
  127. #endif
  128. return *this;
  129. }
  130. //
  131. // Make this type a range, for both Boost.Range, and C++11:
  132. //
  133. BidiIterator begin()const { return this->first; }
  134. BidiIterator end()const { return this->second; }
  135. };
  136. typedef sub_match<const char*> csub_match;
  137. typedef sub_match<std::string::const_iterator> ssub_match;
  138. #ifndef BOOST_NO_WREGEX
  139. typedef sub_match<const wchar_t*> wcsub_match;
  140. typedef sub_match<std::wstring::const_iterator> wssub_match;
  141. #endif
  142. // comparison to std::basic_string<> part 1:
  143. template <class RandomAccessIterator, class traits, class Allocator>
  144. inline bool operator == (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  145. const sub_match<RandomAccessIterator>& m)
  146. { return s.compare(m.str()) == 0; }
  147. template <class RandomAccessIterator, class traits, class Allocator>
  148. inline bool operator != (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  149. const sub_match<RandomAccessIterator>& m)
  150. { return s.compare(m.str()) != 0; }
  151. template <class RandomAccessIterator, class traits, class Allocator>
  152. inline bool operator < (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  153. const sub_match<RandomAccessIterator>& m)
  154. { return s.compare(m.str()) < 0; }
  155. template <class RandomAccessIterator, class traits, class Allocator>
  156. inline bool operator <= (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  157. const sub_match<RandomAccessIterator>& m)
  158. { return s.compare(m.str()) <= 0; }
  159. template <class RandomAccessIterator, class traits, class Allocator>
  160. inline bool operator >= (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  161. const sub_match<RandomAccessIterator>& m)
  162. { return s.compare(m.str()) >= 0; }
  163. template <class RandomAccessIterator, class traits, class Allocator>
  164. inline bool operator > (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  165. const sub_match<RandomAccessIterator>& m)
  166. { return s.compare(m.str()) > 0; }
  167. // comparison to std::basic_string<> part 2:
  168. template <class RandomAccessIterator, class traits, class Allocator>
  169. inline bool operator == (const sub_match<RandomAccessIterator>& m,
  170. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  171. { return m.str().compare(s) == 0; }
  172. template <class RandomAccessIterator, class traits, class Allocator>
  173. inline bool operator != (const sub_match<RandomAccessIterator>& m,
  174. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  175. { return m.str().compare(s) != 0; }
  176. template <class RandomAccessIterator, class traits, class Allocator>
  177. inline bool operator < (const sub_match<RandomAccessIterator>& m,
  178. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  179. { return m.str().compare(s) < 0; }
  180. template <class RandomAccessIterator, class traits, class Allocator>
  181. inline bool operator > (const sub_match<RandomAccessIterator>& m,
  182. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  183. { return m.str().compare(s) > 0; }
  184. template <class RandomAccessIterator, class traits, class Allocator>
  185. inline bool operator <= (const sub_match<RandomAccessIterator>& m,
  186. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  187. { return m.str().compare(s) <= 0; }
  188. template <class RandomAccessIterator, class traits, class Allocator>
  189. inline bool operator >= (const sub_match<RandomAccessIterator>& m,
  190. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  191. { return m.str().compare(s) >= 0; }
  192. // comparison to const charT* part 1:
  193. template <class RandomAccessIterator>
  194. inline bool operator == (const sub_match<RandomAccessIterator>& m,
  195. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  196. { return m.str().compare(s) == 0; }
  197. template <class RandomAccessIterator>
  198. inline bool operator != (const sub_match<RandomAccessIterator>& m,
  199. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  200. { return m.str().compare(s) != 0; }
  201. template <class RandomAccessIterator>
  202. inline bool operator > (const sub_match<RandomAccessIterator>& m,
  203. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  204. { return m.str().compare(s) > 0; }
  205. template <class RandomAccessIterator>
  206. inline bool operator < (const sub_match<RandomAccessIterator>& m,
  207. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  208. { return m.str().compare(s) < 0; }
  209. template <class RandomAccessIterator>
  210. inline bool operator >= (const sub_match<RandomAccessIterator>& m,
  211. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  212. { return m.str().compare(s) >= 0; }
  213. template <class RandomAccessIterator>
  214. inline bool operator <= (const sub_match<RandomAccessIterator>& m,
  215. typename std::iterator_traits<RandomAccessIterator>::value_type const* s)
  216. { return m.str().compare(s) <= 0; }
  217. // comparison to const charT* part 2:
  218. template <class RandomAccessIterator>
  219. inline bool operator == (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  220. const sub_match<RandomAccessIterator>& m)
  221. { return m.str().compare(s) == 0; }
  222. template <class RandomAccessIterator>
  223. inline bool operator != (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  224. const sub_match<RandomAccessIterator>& m)
  225. { return m.str().compare(s) != 0; }
  226. template <class RandomAccessIterator>
  227. inline bool operator < (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  228. const sub_match<RandomAccessIterator>& m)
  229. { return m.str().compare(s) > 0; }
  230. template <class RandomAccessIterator>
  231. inline bool operator > (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  232. const sub_match<RandomAccessIterator>& m)
  233. { return m.str().compare(s) < 0; }
  234. template <class RandomAccessIterator>
  235. inline bool operator <= (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  236. const sub_match<RandomAccessIterator>& m)
  237. { return m.str().compare(s) >= 0; }
  238. template <class RandomAccessIterator>
  239. inline bool operator >= (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  240. const sub_match<RandomAccessIterator>& m)
  241. { return m.str().compare(s) <= 0; }
  242. // comparison to const charT& part 1:
  243. template <class RandomAccessIterator>
  244. inline bool operator == (const sub_match<RandomAccessIterator>& m,
  245. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  246. { return m.str().compare(0, m.length(), &s, 1) == 0; }
  247. template <class RandomAccessIterator>
  248. inline bool operator != (const sub_match<RandomAccessIterator>& m,
  249. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  250. { return m.str().compare(0, m.length(), &s, 1) != 0; }
  251. template <class RandomAccessIterator>
  252. inline bool operator > (const sub_match<RandomAccessIterator>& m,
  253. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  254. { return m.str().compare(0, m.length(), &s, 1) > 0; }
  255. template <class RandomAccessIterator>
  256. inline bool operator < (const sub_match<RandomAccessIterator>& m,
  257. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  258. { return m.str().compare(0, m.length(), &s, 1) < 0; }
  259. template <class RandomAccessIterator>
  260. inline bool operator >= (const sub_match<RandomAccessIterator>& m,
  261. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  262. { return m.str().compare(0, m.length(), &s, 1) >= 0; }
  263. template <class RandomAccessIterator>
  264. inline bool operator <= (const sub_match<RandomAccessIterator>& m,
  265. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  266. { return m.str().compare(0, m.length(), &s, 1) <= 0; }
  267. // comparison to const charT* part 2:
  268. template <class RandomAccessIterator>
  269. inline bool operator == (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  270. const sub_match<RandomAccessIterator>& m)
  271. { return m.str().compare(0, m.length(), &s, 1) == 0; }
  272. template <class RandomAccessIterator>
  273. inline bool operator != (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  274. const sub_match<RandomAccessIterator>& m)
  275. { return m.str().compare(0, m.length(), &s, 1) != 0; }
  276. template <class RandomAccessIterator>
  277. inline bool operator < (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  278. const sub_match<RandomAccessIterator>& m)
  279. { return m.str().compare(0, m.length(), &s, 1) > 0; }
  280. template <class RandomAccessIterator>
  281. inline bool operator > (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  282. const sub_match<RandomAccessIterator>& m)
  283. { return m.str().compare(0, m.length(), &s, 1) < 0; }
  284. template <class RandomAccessIterator>
  285. inline bool operator <= (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  286. const sub_match<RandomAccessIterator>& m)
  287. { return m.str().compare(0, m.length(), &s, 1) >= 0; }
  288. template <class RandomAccessIterator>
  289. inline bool operator >= (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  290. const sub_match<RandomAccessIterator>& m)
  291. { return m.str().compare(0, m.length(), &s, 1) <= 0; }
  292. // addition operators:
  293. template <class RandomAccessIterator, class traits, class Allocator>
  294. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
  295. operator + (const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s,
  296. const sub_match<RandomAccessIterator>& m)
  297. {
  298. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
  299. result.reserve(s.size() + m.length() + 1);
  300. return result.append(s).append(m.first, m.second);
  301. }
  302. template <class RandomAccessIterator, class traits, class Allocator>
  303. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>
  304. operator + (const sub_match<RandomAccessIterator>& m,
  305. const std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator>& s)
  306. {
  307. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type, traits, Allocator> result;
  308. result.reserve(s.size() + m.length() + 1);
  309. return result.append(m.first, m.second).append(s);
  310. }
  311. template <class RandomAccessIterator>
  312. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type>
  313. operator + (typename std::iterator_traits<RandomAccessIterator>::value_type const* s,
  314. const sub_match<RandomAccessIterator>& m)
  315. {
  316. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result;
  317. result.reserve(std::char_traits<typename std::iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
  318. return result.append(s).append(m.first, m.second);
  319. }
  320. template <class RandomAccessIterator>
  321. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type>
  322. operator + (const sub_match<RandomAccessIterator>& m,
  323. typename std::iterator_traits<RandomAccessIterator>::value_type const * s)
  324. {
  325. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result;
  326. result.reserve(std::char_traits<typename std::iterator_traits<RandomAccessIterator>::value_type>::length(s) + m.length() + 1);
  327. return result.append(m.first, m.second).append(s);
  328. }
  329. template <class RandomAccessIterator>
  330. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type>
  331. operator + (typename std::iterator_traits<RandomAccessIterator>::value_type const& s,
  332. const sub_match<RandomAccessIterator>& m)
  333. {
  334. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result;
  335. result.reserve(m.length() + 2);
  336. return result.append(1, s).append(m.first, m.second);
  337. }
  338. template <class RandomAccessIterator>
  339. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type>
  340. operator + (const sub_match<RandomAccessIterator>& m,
  341. typename std::iterator_traits<RandomAccessIterator>::value_type const& s)
  342. {
  343. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result;
  344. result.reserve(m.length() + 2);
  345. return result.append(m.first, m.second).append(1, s);
  346. }
  347. template <class RandomAccessIterator>
  348. inline std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type>
  349. operator + (const sub_match<RandomAccessIterator>& m1,
  350. const sub_match<RandomAccessIterator>& m2)
  351. {
  352. std::basic_string<typename std::iterator_traits<RandomAccessIterator>::value_type> result;
  353. result.reserve(m1.length() + m2.length() + 1);
  354. return result.append(m1.first, m1.second).append(m2.first, m2.second);
  355. }
  356. template <class charT, class traits, class RandomAccessIterator>
  357. std::basic_ostream<charT, traits>&
  358. operator << (std::basic_ostream<charT, traits>& os,
  359. const sub_match<RandomAccessIterator>& s)
  360. {
  361. return (os << s.str());
  362. }
  363. } // namespace boost
  364. #endif