include/boost/regex/v5/regex_raw_buffer.hpp

100.0% Lines (45/45) 100.0% List of functions (9/9)
regex_raw_buffer.hpp
f(x) Functions (9)
Line TLA Hits Source Code
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 /*
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE regex_raw_buffer.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Raw character buffer for regex code.
17 * Note this is an internal header file included
18 * by regex.hpp, do not include on its own.
19 */
20
21 #ifndef BOOST_REGEX_RAW_BUFFER_HPP
22 #define BOOST_REGEX_RAW_BUFFER_HPP
23
24 #ifndef BOOST_REGEX_CONFIG_HPP
25 #include <boost/regex/config.hpp>
26 #endif
27
28 #ifndef BOOST_REGEX_AS_MODULE
29 #include <algorithm>
30 #include <cstddef>
31 #include <cstring>
32 #endif
33
34 namespace boost{
35 namespace BOOST_REGEX_DETAIL_NS{
36
37 struct empty_padding{};
38
39 union padding
40 {
41 void* p;
42 unsigned int i;
43 };
44
45 template <int N>
46 struct padding3
47 {
48 enum{
49 padding_size = 8,
50 padding_mask = 7
51 };
52 };
53
54 template<>
55 struct padding3<2>
56 {
57 enum{
58 padding_size = 2,
59 padding_mask = 1
60 };
61 };
62
63 template<>
64 struct padding3<4>
65 {
66 enum{
67 padding_size = 4,
68 padding_mask = 3
69 };
70 };
71
72 template<>
73 struct padding3<8>
74 {
75 enum{
76 padding_size = 8,
77 padding_mask = 7
78 };
79 };
80
81 template<>
82 struct padding3<16>
83 {
84 enum{
85 padding_size = 16,
86 padding_mask = 15
87 };
88 };
89
90 enum{
91 padding_size = padding3<sizeof(padding)>::padding_size,
92 padding_mask = padding3<sizeof(padding)>::padding_mask
93 };
94
95 //
96 // class raw_storage
97 // basically this is a simplified vector<unsigned char>
98 // this is used by basic_regex for expression storage
99 //
100
101 class raw_storage
102 {
103 public:
104 typedef std::size_t size_type;
105 typedef unsigned char* pointer;
106 private:
107 pointer last, start, end;
108 public:
109
110 raw_storage();
111 raw_storage(size_type n);
112
113 408299x ~raw_storage()
114 {
115 408299x ::operator delete(start);
116 408299x }
117
118 508201x void resize(size_type n)
119 {
120 508201x size_type newsize = start ? last - start : 1024;
121 609511x while (newsize < n)
122 101310x newsize *= 2;
123 508201x size_type datasize = end - start;
124 // extend newsize to WORD/DWORD boundary:
125 508201x newsize = (newsize + padding_mask) & ~(padding_mask);
126
127 // allocate and copy data:
128 508201x pointer ptr = static_cast<pointer>(::operator new(newsize));
129 42356x BOOST_REGEX_NOEH_ASSERT(ptr)
130 508201x if (start)
131 101310x std::memcpy(ptr, start, datasize);
132
133 // get rid of old buffer:
134 508201x ::operator delete(start);
135
136 // and set up pointers:
137 508201x start = ptr;
138 508201x end = ptr + datasize;
139 508201x last = ptr + newsize;
140 508201x }
141
142 5032187x void* extend(size_type n)
143 {
144 5032187x if(size_type(last - end) < n)
145 444441x resize(n + (end - start));
146 5032187x pointer result = end;
147 5032187x end += n;
148 5032187x return result;
149 }
150
151 693104x void* insert(size_type pos, size_type n)
152 {
153 693104x BOOST_REGEX_ASSERT(pos <= size_type(end - start));
154 693104x if (size_type(last - end) < n)
155 63760x resize(n + (end - start));
156 693104x void* result = start + pos;
157 693104x std::memmove(start + pos + n, start + pos, (end - start) - pos);
158 693104x end += n;
159 693104x return result;
160 }
161
162 7159179x size_type size()
163 {
164 7159179x return size_type(end - start);
165 }
166
167 size_type capacity()
168 {
169 return size_type(last - start);
170 }
171
172 10301202x void* data()const
173 {
174 10301202x return start;
175 }
176
177 size_type index(void* ptr)
178 {
179 return size_type(static_cast<pointer>(ptr) - static_cast<pointer>(data()));
180 }
181
182 408235x void clear()
183 {
184 408235x end = start;
185 408235x }
186
187 6355090x void align()
188 {
189 // move end up to a boundary:
190 6355090x end = start + (((end - start) + padding_mask) & ~padding_mask);
191 6355090x }
192 void swap(raw_storage& that)
193 {
194 std::swap(start, that.start);
195 std::swap(end, that.end);
196 std::swap(last, that.last);
197 }
198 };
199
200 408299x inline raw_storage::raw_storage()
201 {
202 408299x last = start = end = 0;
203 408299x }
204
205 inline raw_storage::raw_storage(size_type n)
206 {
207 start = end = static_cast<pointer>(::operator new(n));
208 BOOST_REGEX_NOEH_ASSERT(start)
209 last = start + n;
210 }
211
212 } // namespace BOOST_REGEX_DETAIL_NS
213 } // namespace boost
214
215 #endif
216
217