|
3 | 3 |
|
4 | 4 | #include <bit> |
5 | 5 | #include <cassert> |
6 | | -#include <cstddef> |
7 | | -#include <cstring> |
8 | 6 | #include <memory> |
9 | | -#include <memory_resource> |
10 | 7 | #include <span> |
11 | 8 | #include <string_view> |
12 | 9 | #include <type_traits> |
13 | 10 | #include <concepts> |
14 | 11 |
|
15 | 12 | #include "nix/expr/eval-gc.hh" |
| 13 | +#include "nix/expr/string-data.hh" |
16 | 14 | #include "nix/expr/value/context.hh" |
17 | 15 | #include "nix/util/source-path.hh" |
18 | 16 | #include "nix/expr/print-options.hh" |
@@ -193,91 +191,6 @@ public: |
193 | 191 | friend struct Value; |
194 | 192 | }; |
195 | 193 |
|
196 | | -class StringData |
197 | | -{ |
198 | | -public: |
199 | | - using size_type = std::size_t; |
200 | | - |
201 | | - size_type size_; |
202 | | - char data_[]; |
203 | | - |
204 | | - /* |
205 | | - * This in particular ensures that we cannot have a `StringData` |
206 | | - * that we use by value, which is just what we want! |
207 | | - * |
208 | | - * Dynamically sized types aren't a thing in C++ and even flexible array |
209 | | - * members are a language extension and beyond the realm of standard C++. |
210 | | - * Technically, sizeof data_ member is 0 and the intended way to use flexible |
211 | | - * array members is to allocate sizeof(StrindData) + count * sizeof(char) bytes |
212 | | - * and the compiler will consider alignment restrictions for the FAM. |
213 | | - * |
214 | | - */ |
215 | | - |
216 | | - StringData(StringData &&) = delete; |
217 | | - StringData & operator=(StringData &&) = delete; |
218 | | - StringData(const StringData &) = delete; |
219 | | - StringData & operator=(const StringData &) = delete; |
220 | | - ~StringData() = default; |
221 | | - |
222 | | -private: |
223 | | - StringData() = delete; |
224 | | - |
225 | | - explicit StringData(size_type size) |
226 | | - : size_(size) |
227 | | - { |
228 | | - } |
229 | | - |
230 | | -public: |
231 | | - /** |
232 | | - * Allocate StringData on the (possibly) GC-managed heap and copy |
233 | | - * the contents of s to it. |
234 | | - */ |
235 | | - static const StringData & make(std::string_view s); |
236 | | - |
237 | | - /** |
238 | | - * Allocate StringData on the (possibly) GC-managed heap. |
239 | | - * @param size Length of the string (without the NUL terminator). |
240 | | - */ |
241 | | - static StringData & alloc(size_t size); |
242 | | - |
243 | | - size_t size() const |
244 | | - { |
245 | | - return size_; |
246 | | - } |
247 | | - |
248 | | - char * data() noexcept |
249 | | - { |
250 | | - return data_; |
251 | | - } |
252 | | - |
253 | | - const char * data() const noexcept |
254 | | - { |
255 | | - return data_; |
256 | | - } |
257 | | - |
258 | | - const char * c_str() const noexcept |
259 | | - { |
260 | | - return data_; |
261 | | - } |
262 | | - |
263 | | - constexpr std::string_view view() const noexcept |
264 | | - { |
265 | | - return std::string_view(data_, size_); |
266 | | - } |
267 | | - |
268 | | - template<size_t N> |
269 | | - struct Static; |
270 | | - |
271 | | - static StringData & make(std::pmr::memory_resource & resource, std::string_view s) |
272 | | - { |
273 | | - auto & res = |
274 | | - *new (resource.allocate(sizeof(StringData) + s.size() + 1, alignof(StringData))) StringData(s.size()); |
275 | | - std::memcpy(res.data_, s.data(), s.size()); |
276 | | - res.data_[s.size()] = '\0'; |
277 | | - return res; |
278 | | - } |
279 | | -}; |
280 | | - |
281 | 194 | namespace detail { |
282 | 195 |
|
283 | 196 | /** |
|
0 commit comments