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