regex_raw_buffer.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 regex_raw_buffer.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Raw character buffer for regex code.
  16. * Note this is an internal header file included
  17. * by regex.hpp, do not include on its own.
  18. */
  19. #ifndef BOOST_REGEX_RAW_BUFFER_HPP
  20. #define BOOST_REGEX_RAW_BUFFER_HPP
  21. #ifndef BOOST_REGEX_CONFIG_HPP
  22. #include <boost/regex/config.hpp>
  23. #endif
  24. #include <algorithm>
  25. #include <cstddef>
  26. namespace boost{
  27. namespace BOOST_REGEX_DETAIL_NS{
  28. #ifdef BOOST_MSVC
  29. #pragma warning(push)
  30. #pragma warning(disable: 4103)
  31. #endif
  32. #ifdef BOOST_HAS_ABI_HEADERS
  33. # include BOOST_ABI_PREFIX
  34. #endif
  35. #ifdef BOOST_MSVC
  36. #pragma warning(pop)
  37. #endif
  38. struct empty_padding{};
  39. union padding
  40. {
  41. void* p;
  42. unsigned int i;
  43. };
  44. template <int N>
  45. struct padding3
  46. {
  47. enum{
  48. padding_size = 8,
  49. padding_mask = 7
  50. };
  51. };
  52. template<>
  53. struct padding3<2>
  54. {
  55. enum{
  56. padding_size = 2,
  57. padding_mask = 1
  58. };
  59. };
  60. template<>
  61. struct padding3<4>
  62. {
  63. enum{
  64. padding_size = 4,
  65. padding_mask = 3
  66. };
  67. };
  68. template<>
  69. struct padding3<8>
  70. {
  71. enum{
  72. padding_size = 8,
  73. padding_mask = 7
  74. };
  75. };
  76. template<>
  77. struct padding3<16>
  78. {
  79. enum{
  80. padding_size = 16,
  81. padding_mask = 15
  82. };
  83. };
  84. enum{
  85. padding_size = padding3<sizeof(padding)>::padding_size,
  86. padding_mask = padding3<sizeof(padding)>::padding_mask
  87. };
  88. //
  89. // class raw_storage
  90. // basically this is a simplified vector<unsigned char>
  91. // this is used by basic_regex for expression storage
  92. //
  93. class BOOST_REGEX_DECL raw_storage
  94. {
  95. public:
  96. typedef std::size_t size_type;
  97. typedef unsigned char* pointer;
  98. private:
  99. pointer last, start, end;
  100. public:
  101. raw_storage();
  102. raw_storage(size_type n);
  103. ~raw_storage()
  104. {
  105. ::operator delete(start);
  106. }
  107. void BOOST_REGEX_CALL resize(size_type n);
  108. void* BOOST_REGEX_CALL extend(size_type n)
  109. {
  110. if(size_type(last - end) < n)
  111. resize(n + (end - start));
  112. pointer result = end;
  113. end += n;
  114. return result;
  115. }
  116. void* BOOST_REGEX_CALL insert(size_type pos, size_type n);
  117. size_type BOOST_REGEX_CALL size()
  118. {
  119. return size_type(end - start);
  120. }
  121. size_type BOOST_REGEX_CALL capacity()
  122. {
  123. return size_type(last - start);
  124. }
  125. void* BOOST_REGEX_CALL data()const
  126. {
  127. return start;
  128. }
  129. size_type BOOST_REGEX_CALL index(void* ptr)
  130. {
  131. return size_type(static_cast<pointer>(ptr) - static_cast<pointer>(data()));
  132. }
  133. void BOOST_REGEX_CALL clear()
  134. {
  135. end = start;
  136. }
  137. void BOOST_REGEX_CALL align()
  138. {
  139. // move end up to a boundary:
  140. end = start + (((end - start) + padding_mask) & ~padding_mask);
  141. }
  142. void swap(raw_storage& that)
  143. {
  144. std::swap(start, that.start);
  145. std::swap(end, that.end);
  146. std::swap(last, that.last);
  147. }
  148. };
  149. inline raw_storage::raw_storage()
  150. {
  151. last = start = end = 0;
  152. }
  153. inline raw_storage::raw_storage(size_type n)
  154. {
  155. start = end = static_cast<pointer>(::operator new(n));
  156. BOOST_REGEX_NOEH_ASSERT(start)
  157. last = start + n;
  158. }
  159. #ifdef BOOST_MSVC
  160. #pragma warning(push)
  161. #pragma warning(disable: 4103)
  162. #endif
  163. #ifdef BOOST_HAS_ABI_HEADERS
  164. # include BOOST_ABI_SUFFIX
  165. #endif
  166. #ifdef BOOST_MSVC
  167. #pragma warning(pop)
  168. #endif
  169. } // namespace BOOST_REGEX_DETAIL_NS
  170. } // namespace boost
  171. #endif