1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-11 21:16:02 +01:00

Merge pull request #14470 from NixOS/ctx-type-alias

Encapsulate and slightly optimize string contexts
This commit is contained in:
John Ericson 2025-11-09 21:21:15 +00:00 committed by GitHub
commit cbe8ec7bd7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 97 additions and 23 deletions

View file

@ -36,6 +36,7 @@
#include <sys/time.h>
#include <fstream>
#include <functional>
#include <ranges>
#include <nlohmann/json.hpp>
#include <boost/container/small_vector.hpp>
@ -821,28 +822,25 @@ void Value::mkString(std::string_view s)
mkStringNoCopy(makeImmutableString(s));
}
static const char ** encodeContext(const NixStringContext & context)
Value::StringWithContext::Context * Value::StringWithContext::Context::fromBuilder(const NixStringContext & context)
{
if (!context.empty()) {
size_t n = 0;
auto ctx = (const char **) allocBytes((context.size() + 1) * sizeof(char *));
for (auto & i : context) {
ctx[n++] = makeImmutableString({i.to_string()});
}
ctx[n] = nullptr;
return ctx;
} else
if (context.empty())
return nullptr;
auto ctx = new (allocBytes(sizeof(Context) + context.size() * sizeof(value_type))) Context(context.size());
std::ranges::transform(
context, ctx->elems, [](const NixStringContextElem & elt) { return makeImmutableString(elt.to_string()); });
return ctx;
}
void Value::mkString(std::string_view s, const NixStringContext & context)
{
mkStringNoCopy(makeImmutableString(s), encodeContext(context));
mkStringNoCopy(makeImmutableString(s), Value::StringWithContext::Context::fromBuilder(context));
}
void Value::mkStringMove(const char * s, const NixStringContext & context)
{
mkStringNoCopy(s, encodeContext(context));
mkStringNoCopy(s, Value::StringWithContext::Context::fromBuilder(context));
}
void Value::mkPath(const SourcePath & path)
@ -2288,9 +2286,9 @@ std::string_view EvalState::forceString(Value & v, const PosIdx pos, std::string
void copyContext(const Value & v, NixStringContext & context, const ExperimentalFeatureSettings & xpSettings)
{
if (v.context())
for (const char ** p = v.context(); *p; ++p)
context.insert(NixStringContextElem::parse(*p, xpSettings));
if (auto * ctx = v.context())
for (auto * elem : *ctx)
context.insert(NixStringContextElem::parse(elem, xpSettings));
}
std::string_view EvalState::forceString(
@ -2310,7 +2308,9 @@ std::string_view EvalState::forceStringNoCtx(Value & v, const PosIdx pos, std::s
auto s = forceString(v, pos, errorCtx);
if (v.context()) {
error<EvalError>(
"the string '%1%' is not allowed to refer to a store path (such as '%2%')", v.string_view(), v.context()[0])
"the string '%1%' is not allowed to refer to a store path (such as '%2%')",
v.string_view(),
*v.context()->begin())
.withTrace(pos, errorCtx)
.debugThrow();
}