libexpr: Consolidate implementation between Value string constructors

Change-Id: I5bf8d58b534cd652d89f7b93019378b66a6a6964
This commit is contained in:
skye
2026-02-28 15:53:10 -05:00
parent be3a40e5dc
commit debf5554e0
2 changed files with 18 additions and 34 deletions
+17 -11
View File
@@ -2,6 +2,7 @@
#include <ostream>
#include "libexpr/gc-alloc.hh"
#include "lix/libexpr/eval.hh"
#include "lix/libexpr/print.hh"
@@ -27,15 +28,18 @@ static_assert(alignof(Value::PrimOp) >= Value::Acb::TAG_ALIGN);
static_assert(alignof(Value::Int) >= Value::Acb::TAG_ALIGN);
static_assert(alignof(Value::Lambda) >= Value::Acb::TAG_ALIGN);
static void copyContextToValue(Value::String & s, const NixStringContext & context)
static char const ** copyContext(const NixStringContext & context)
{
if (!context.empty()) {
size_t n = 0;
s.context = gcAllocType<char const *>(context.size() + 1);
for (auto & i : context)
s.context[n++] = gcCopyStringIfNeeded(i.to_string());
s.context[n] = 0;
if (context.empty()) {
return nullptr;
}
auto contextPtr = gcAllocType<char const *>(context.size() + 1);
size_t n = 0;
for (auto & i : context) {
contextPtr[n++] = gcCopyStringIfNeeded(i.to_string());
}
contextPtr[n] = nullptr;
return contextPtr;
}
Value::Value(attrs_t, BindingsBuilder & bindings) : Value(NewValueAs::attrs, bindings.finish()) {}
@@ -62,11 +66,13 @@ void Value::mkPrimOp(PrimOp * p)
}
Value::Value(string_t, Str * s, const NixStringContext & context)
: Value(NewValueAs::string, s, copyContext(context))
{
}
Value::Value(string_t, std::string_view s, const NixStringContext & context)
: Value(NewValueAs::string, s, copyContext(context))
{
auto block = gcAllocType<String>();
*block = {.content = s, .context = nullptr};
raw = tag(tString, block);
copyContextToValue(*block, context);
}
#ifndef __APPLE__
+1 -23
View File
@@ -475,29 +475,7 @@ public:
///
/// The string data *is* copied from @ref copyFrom, and this constructor
/// performs a dynamic (GC) allocation to do so.
Value(string_t, std::string_view copyFrom, NixStringContext const & context = {})
{
auto block = gcAllocType<String>();
*block = {.content = Str::gcCopy(copyFrom), .context = nullptr};
raw = tag(tString, block);
if (context.empty()) {
// It stays nullptr.
return;
}
// Copy the context.
block->context = gcAllocType<char const *>(context.size() + 1);
size_t n = 0;
for (NixStringContextElem const & contextElem : context) {
block->context[n] = gcCopyStringIfNeeded(contextElem.to_string());
n += 1;
}
// Terminator sentinel.
block->context[n] = nullptr;
}
Value(string_t, std::string_view copyFrom, NixStringContext const & context = {});
/// Constructs a nix language value of type "path", with the value of the
/// C-string pointed to by @ref strPtr.