basic_chset.ipp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_BASIC_CHSET_IPP
  10. #define BOOST_SPIRIT_BASIC_CHSET_IPP
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include <bitset>
  13. #include <boost/spirit/home/classic/utility/impl/chset/basic_chset.hpp>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //
  19. // basic_chset: character set implementation
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. template <typename CharT>
  23. inline basic_chset<CharT>::basic_chset() {}
  24. //////////////////////////////////
  25. template <typename CharT>
  26. inline basic_chset<CharT>::basic_chset(basic_chset const& arg_)
  27. : rr(arg_.rr) {}
  28. //////////////////////////////////
  29. template <typename CharT>
  30. inline bool
  31. basic_chset<CharT>::test(CharT v) const
  32. { return rr.test(v); }
  33. //////////////////////////////////
  34. template <typename CharT>
  35. inline void
  36. basic_chset<CharT>::set(CharT from, CharT to)
  37. { rr.set(utility::impl::range<CharT>(from, to)); }
  38. //////////////////////////////////
  39. template <typename CharT>
  40. inline void
  41. basic_chset<CharT>::set(CharT c)
  42. { rr.set(utility::impl::range<CharT>(c, c)); }
  43. //////////////////////////////////
  44. template <typename CharT>
  45. inline void
  46. basic_chset<CharT>::clear(CharT from, CharT to)
  47. { rr.clear(utility::impl::range<CharT>(from, to)); }
  48. //////////////////////////////////
  49. template <typename CharT>
  50. inline void
  51. basic_chset<CharT>::clear()
  52. { rr.clear(); }
  53. /////////////////////////////////
  54. template <typename CharT>
  55. inline void
  56. basic_chset<CharT>::inverse()
  57. {
  58. basic_chset inv;
  59. inv.set(
  60. (std::numeric_limits<CharT>::min)(),
  61. (std::numeric_limits<CharT>::max)()
  62. );
  63. inv -= *this;
  64. swap(inv);
  65. }
  66. /////////////////////////////////
  67. template <typename CharT>
  68. inline void
  69. basic_chset<CharT>::swap(basic_chset& x)
  70. { rr.swap(x.rr); }
  71. /////////////////////////////////
  72. template <typename CharT>
  73. inline basic_chset<CharT>&
  74. basic_chset<CharT>::operator|=(basic_chset<CharT> const& x)
  75. {
  76. typedef typename utility::impl::range_run<CharT>::const_iterator const_iterator;
  77. for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter)
  78. rr.set(*iter);
  79. return *this;
  80. }
  81. /////////////////////////////////
  82. template <typename CharT>
  83. inline basic_chset<CharT>&
  84. basic_chset<CharT>::operator&=(basic_chset<CharT> const& x)
  85. {
  86. basic_chset inv;
  87. inv.set(
  88. (std::numeric_limits<CharT>::min)(),
  89. (std::numeric_limits<CharT>::max)()
  90. );
  91. inv -= x;
  92. *this -= inv;
  93. return *this;
  94. }
  95. /////////////////////////////////
  96. template <typename CharT>
  97. inline basic_chset<CharT>&
  98. basic_chset<CharT>::operator-=(basic_chset<CharT> const& x)
  99. {
  100. typedef typename utility::impl::range_run<CharT>::const_iterator const_iterator;
  101. for (const_iterator iter = x.rr.begin(); iter != x.rr.end(); ++iter)
  102. rr.clear(*iter);
  103. return *this;
  104. }
  105. /////////////////////////////////
  106. template <typename CharT>
  107. inline basic_chset<CharT>&
  108. basic_chset<CharT>::operator^=(basic_chset<CharT> const& x)
  109. {
  110. basic_chset bma = x;
  111. bma -= *this;
  112. *this -= x;
  113. *this |= bma;
  114. return *this;
  115. }
  116. #if (CHAR_BIT == 8)
  117. ///////////////////////////////////////////////////////////////////////////////
  118. //
  119. // basic_chset: specializations for 8 bit chars using std::bitset
  120. //
  121. ///////////////////////////////////////////////////////////////////////////////
  122. template <typename CharT>
  123. inline basic_chset_8bit<CharT>::basic_chset_8bit() {}
  124. /////////////////////////////////
  125. template <typename CharT>
  126. inline basic_chset_8bit<CharT>::basic_chset_8bit(basic_chset_8bit const& arg_)
  127. : bset(arg_.bset) {}
  128. /////////////////////////////////
  129. template <typename CharT>
  130. inline bool
  131. basic_chset_8bit<CharT>::test(CharT v) const
  132. { return bset.test((unsigned char)v); }
  133. /////////////////////////////////
  134. template <typename CharT>
  135. inline void
  136. basic_chset_8bit<CharT>::set(CharT from, CharT to)
  137. {
  138. for (int i = from; i <= to; ++i)
  139. bset.set((unsigned char)i);
  140. }
  141. /////////////////////////////////
  142. template <typename CharT>
  143. inline void
  144. basic_chset_8bit<CharT>::set(CharT c)
  145. { bset.set((unsigned char)c); }
  146. /////////////////////////////////
  147. template <typename CharT>
  148. inline void
  149. basic_chset_8bit<CharT>::clear(CharT from, CharT to)
  150. {
  151. for (int i = from; i <= to; ++i)
  152. bset.reset((unsigned char)i);
  153. }
  154. /////////////////////////////////
  155. template <typename CharT>
  156. inline void
  157. basic_chset_8bit<CharT>::clear(CharT c)
  158. { bset.reset((unsigned char)c); }
  159. /////////////////////////////////
  160. template <typename CharT>
  161. inline void
  162. basic_chset_8bit<CharT>::clear()
  163. { bset.reset(); }
  164. /////////////////////////////////
  165. template <typename CharT>
  166. inline void
  167. basic_chset_8bit<CharT>::inverse()
  168. { bset.flip(); }
  169. /////////////////////////////////
  170. template <typename CharT>
  171. inline void
  172. basic_chset_8bit<CharT>::swap(basic_chset_8bit& x)
  173. { std::swap(bset, x.bset); }
  174. /////////////////////////////////
  175. template <typename CharT>
  176. inline basic_chset_8bit<CharT>&
  177. basic_chset_8bit<CharT>::operator|=(basic_chset_8bit const& x)
  178. {
  179. bset |= x.bset;
  180. return *this;
  181. }
  182. /////////////////////////////////
  183. template <typename CharT>
  184. inline basic_chset_8bit<CharT>&
  185. basic_chset_8bit<CharT>::operator&=(basic_chset_8bit const& x)
  186. {
  187. bset &= x.bset;
  188. return *this;
  189. }
  190. /////////////////////////////////
  191. template <typename CharT>
  192. inline basic_chset_8bit<CharT>&
  193. basic_chset_8bit<CharT>::operator-=(basic_chset_8bit const& x)
  194. {
  195. bset &= ~x.bset;
  196. return *this;
  197. }
  198. /////////////////////////////////
  199. template <typename CharT>
  200. inline basic_chset_8bit<CharT>&
  201. basic_chset_8bit<CharT>::operator^=(basic_chset_8bit const& x)
  202. {
  203. bset ^= x.bset;
  204. return *this;
  205. }
  206. #endif
  207. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  208. }} // namespace boost::spirit
  209. #endif