chset.ipp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. Copyright (c) 2001-2003 Daniel Nuffer
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #ifndef BOOST_SPIRIT_CHSET_IPP
  10. #define BOOST_SPIRIT_CHSET_IPP
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include <boost/limits.hpp>
  13. #include <boost/spirit/home/classic/utility/chset.hpp>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //
  19. // chset class
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. namespace utility { namespace impl {
  23. template <typename CharT>
  24. inline void
  25. detach(boost::shared_ptr<basic_chset<CharT> >& ptr)
  26. {
  27. if (!ptr.unique())
  28. ptr = boost::shared_ptr<basic_chset<CharT> >
  29. (new basic_chset<CharT>(*ptr));
  30. }
  31. template <typename CharT>
  32. inline void
  33. detach_clear(boost::shared_ptr<basic_chset<CharT> >& ptr)
  34. {
  35. if (ptr.unique())
  36. ptr->clear();
  37. else
  38. ptr.reset(new basic_chset<CharT>());
  39. }
  40. template <typename CharT, typename CharT2>
  41. void construct_chset(boost::shared_ptr<basic_chset<CharT> >& ptr,
  42. CharT2 const* definition)
  43. {
  44. CharT2 ch = *definition++;
  45. while (ch)
  46. {
  47. CharT2 next = *definition++;
  48. if (next == '-')
  49. {
  50. next = *definition++;
  51. if (next == 0)
  52. {
  53. ptr->set(ch);
  54. ptr->set('-');
  55. break;
  56. }
  57. ptr->set(ch, next);
  58. }
  59. else
  60. {
  61. ptr->set(ch);
  62. }
  63. ch = next;
  64. }
  65. }
  66. }} // namespace utility::impl
  67. template <typename CharT>
  68. inline chset<CharT>::chset()
  69. : ptr(new basic_chset<CharT>()) {}
  70. template <typename CharT>
  71. inline chset<CharT>::chset(chset const& arg_)
  72. : ptr(new basic_chset<CharT>(*arg_.ptr)) {}
  73. template <typename CharT>
  74. inline chset<CharT>::chset(CharT arg_)
  75. : ptr(new basic_chset<CharT>())
  76. { ptr->set(arg_); }
  77. template <typename CharT>
  78. inline chset<CharT>::chset(anychar_parser /*arg*/)
  79. : ptr(new basic_chset<CharT>())
  80. {
  81. ptr->set(
  82. (std::numeric_limits<CharT>::min)(),
  83. (std::numeric_limits<CharT>::max)()
  84. );
  85. }
  86. template <typename CharT>
  87. inline chset<CharT>::chset(nothing_parser /*arg_*/)
  88. : ptr(new basic_chset<CharT>()) {}
  89. template <typename CharT>
  90. inline chset<CharT>::chset(chlit<CharT> const& arg_)
  91. : ptr(new basic_chset<CharT>())
  92. { ptr->set(arg_.ch); }
  93. template <typename CharT>
  94. inline chset<CharT>::chset(range<CharT> const& arg_)
  95. : ptr(new basic_chset<CharT>())
  96. { ptr->set(arg_.first, arg_.last); }
  97. template <typename CharT>
  98. inline chset<CharT>::chset(negated_char_parser<chlit<CharT> > const& arg_)
  99. : ptr(new basic_chset<CharT>())
  100. {
  101. set(arg_);
  102. }
  103. template <typename CharT>
  104. inline chset<CharT>::chset(negated_char_parser<range<CharT> > const& arg_)
  105. : ptr(new basic_chset<CharT>())
  106. {
  107. set(arg_);
  108. }
  109. template <typename CharT>
  110. inline chset<CharT>::~chset() {}
  111. template <typename CharT>
  112. inline chset<CharT>&
  113. chset<CharT>::operator=(chset const& rhs)
  114. {
  115. ptr = rhs.ptr;
  116. return *this;
  117. }
  118. template <typename CharT>
  119. inline chset<CharT>&
  120. chset<CharT>::operator=(CharT rhs)
  121. {
  122. utility::impl::detach_clear(ptr);
  123. ptr->set(rhs);
  124. return *this;
  125. }
  126. template <typename CharT>
  127. inline chset<CharT>&
  128. chset<CharT>::operator=(anychar_parser /*rhs*/)
  129. {
  130. utility::impl::detach_clear(ptr);
  131. ptr->set(
  132. (std::numeric_limits<CharT>::min)(),
  133. (std::numeric_limits<CharT>::max)()
  134. );
  135. return *this;
  136. }
  137. template <typename CharT>
  138. inline chset<CharT>&
  139. chset<CharT>::operator=(nothing_parser /*rhs*/)
  140. {
  141. utility::impl::detach_clear(ptr);
  142. return *this;
  143. }
  144. template <typename CharT>
  145. inline chset<CharT>&
  146. chset<CharT>::operator=(chlit<CharT> const& rhs)
  147. {
  148. utility::impl::detach_clear(ptr);
  149. ptr->set(rhs.ch);
  150. return *this;
  151. }
  152. template <typename CharT>
  153. inline chset<CharT>&
  154. chset<CharT>::operator=(range<CharT> const& rhs)
  155. {
  156. utility::impl::detach_clear(ptr);
  157. ptr->set(rhs.first, rhs.last);
  158. return *this;
  159. }
  160. template <typename CharT>
  161. inline chset<CharT>&
  162. chset<CharT>::operator=(negated_char_parser<chlit<CharT> > const& rhs)
  163. {
  164. utility::impl::detach_clear(ptr);
  165. set(rhs);
  166. return *this;
  167. }
  168. template <typename CharT>
  169. inline chset<CharT>&
  170. chset<CharT>::operator=(negated_char_parser<range<CharT> > const& rhs)
  171. {
  172. utility::impl::detach_clear(ptr);
  173. set(rhs);
  174. return *this;
  175. }
  176. template <typename CharT>
  177. inline void
  178. chset<CharT>::set(range<CharT> const& arg_)
  179. {
  180. utility::impl::detach(ptr);
  181. ptr->set(arg_.first, arg_.last);
  182. }
  183. template <typename CharT>
  184. inline void
  185. chset<CharT>::set(negated_char_parser<chlit<CharT> > const& arg_)
  186. {
  187. utility::impl::detach(ptr);
  188. if(arg_.positive.ch != (std::numeric_limits<CharT>::min)()) {
  189. ptr->set((std::numeric_limits<CharT>::min)(), arg_.positive.ch - 1);
  190. }
  191. if(arg_.positive.ch != (std::numeric_limits<CharT>::max)()) {
  192. ptr->set(arg_.positive.ch + 1, (std::numeric_limits<CharT>::max)());
  193. }
  194. }
  195. template <typename CharT>
  196. inline void
  197. chset<CharT>::set(negated_char_parser<range<CharT> > const& arg_)
  198. {
  199. utility::impl::detach(ptr);
  200. if(arg_.positive.first != (std::numeric_limits<CharT>::min)()) {
  201. ptr->set((std::numeric_limits<CharT>::min)(), arg_.positive.first - 1);
  202. }
  203. if(arg_.positive.last != (std::numeric_limits<CharT>::max)()) {
  204. ptr->set(arg_.positive.last + 1, (std::numeric_limits<CharT>::max)());
  205. }
  206. }
  207. template <typename CharT>
  208. inline void
  209. chset<CharT>::clear(range<CharT> const& arg_)
  210. {
  211. utility::impl::detach(ptr);
  212. ptr->clear(arg_.first, arg_.last);
  213. }
  214. template <typename CharT>
  215. inline void
  216. chset<CharT>::clear(negated_char_parser<range<CharT> > const& arg_)
  217. {
  218. utility::impl::detach(ptr);
  219. if(arg_.positive.first != (std::numeric_limits<CharT>::min)()) {
  220. ptr->clear((std::numeric_limits<CharT>::min)(), arg_.positive.first - 1);
  221. }
  222. if(arg_.positive.last != (std::numeric_limits<CharT>::max)()) {
  223. ptr->clear(arg_.positive.last + 1, (std::numeric_limits<CharT>::max)());
  224. }
  225. }
  226. template <typename CharT>
  227. inline bool
  228. chset<CharT>::test(CharT ch) const
  229. { return ptr->test(ch); }
  230. template <typename CharT>
  231. inline chset<CharT>&
  232. chset<CharT>::inverse()
  233. {
  234. utility::impl::detach(ptr);
  235. ptr->inverse();
  236. return *this;
  237. }
  238. template <typename CharT>
  239. inline void
  240. chset<CharT>::swap(chset& x)
  241. { ptr.swap(x.ptr); }
  242. template <typename CharT>
  243. inline chset<CharT>&
  244. chset<CharT>::operator|=(chset const& x)
  245. {
  246. utility::impl::detach(ptr);
  247. *ptr |= *x.ptr;
  248. return *this;
  249. }
  250. template <typename CharT>
  251. inline chset<CharT>&
  252. chset<CharT>::operator&=(chset const& x)
  253. {
  254. utility::impl::detach(ptr);
  255. *ptr &= *x.ptr;
  256. return *this;
  257. }
  258. template <typename CharT>
  259. inline chset<CharT>&
  260. chset<CharT>::operator-=(chset const& x)
  261. {
  262. utility::impl::detach(ptr);
  263. *ptr -= *x.ptr;
  264. return *this;
  265. }
  266. template <typename CharT>
  267. inline chset<CharT>&
  268. chset<CharT>::operator^=(chset const& x)
  269. {
  270. utility::impl::detach(ptr);
  271. *ptr ^= *x.ptr;
  272. return *this;
  273. }
  274. ///////////////////////////////////////////////////////////////////////////////
  275. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  276. }} // namespace boost::spirit
  277. #endif