1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 11:36:03 +01:00

Compare commits

...

1 commit

Author SHA1 Message Date
John Ericson
f029b14eaa Factor out type alias for string contexts on the heap
This will make it easier to change this type in the future.

See new TODO on naming. The thing we already so-named is a builder type
for string contexts, not the on-heap type.

Co-Authored-By: Taeer Bar-Yam <taeer@bar-yam.me
2025-11-03 19:23:21 -05:00
4 changed files with 32 additions and 8 deletions

View file

@ -136,14 +136,14 @@ struct AttrDb
});
}
AttrId setString(AttrKey key, std::string_view s, const char ** context = nullptr)
AttrId setString(AttrKey key, std::string_view s, Value::StringWithContext::Context context = nullptr)
{
return doSQLite([&]() {
auto state(_state->lock());
if (context) {
std::string ctx;
for (const char ** p = context; *p; ++p) {
for (Value::StringWithContext::Context p = context; *p; ++p) {
if (p != context)
ctx.push_back(' ');
ctx.append(*p);

View file

@ -821,11 +821,11 @@ void Value::mkString(std::string_view s)
mkStringNoCopy(makeImmutableString(s));
}
static const char ** encodeContext(const NixStringContext & context)
static Value::StringWithContext::Context encodeContext(const NixStringContext & context)
{
if (!context.empty()) {
size_t n = 0;
auto ctx = (const char **) allocBytes((context.size() + 1) * sizeof(char *));
auto ctx = (Value::StringWithContext::Context) allocBytes((context.size() + 1) * sizeof(char *));
for (auto & i : context) {
ctx[n++] = makeImmutableString({i.to_string()});
}
@ -2288,7 +2288,7 @@ 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)
for (Value::StringWithContext::Context p = v.context(); *p; ++p)
context.insert(NixStringContextElem::parse(*p, xpSettings));
}

View file

@ -220,7 +220,18 @@ struct ValueBase
struct StringWithContext
{
const char * c_str;
const char ** context; // must be in sorted order
/**
* The type of the context itself.
*
* Currently, it is pointer to an array of pointers to strings.
* The strings are specially formatted to represent a flattening
* of the recursive sum type that is s context element.
*
* @See NixStringContext for an more easily understood type,
* that of the "builder" for this data structure.
*/
using Context = const char **;
Context context; // must be in sorted order
};
struct Path
@ -991,7 +1002,7 @@ public:
setStorage(b);
}
void mkStringNoCopy(const char * s, const char ** context = 0) noexcept
void mkStringNoCopy(const char * s, Value::StringWithContext::Context context = 0) noexcept
{
setStorage(StringWithContext{.c_str = s, .context = context});
}
@ -1117,7 +1128,7 @@ public:
return getStorage<StringWithContext>().c_str;
}
const char ** context() const noexcept
Value::StringWithContext::Context context() const noexcept
{
return getStorage<StringWithContext>().context;
}

View file

@ -24,6 +24,14 @@ public:
}
};
/**
* @todo This should be reamed to `StringContextBuilderElem`, since:
*
* 1. We use `*Builder` for off-heap temporary data structures
*
* 2. The `Nix*` is totally redundant. (And my mistake from a long time
* ago.)
*/
struct NixStringContextElem
{
/**
@ -77,6 +85,11 @@ struct NixStringContextElem
std::string to_string() const;
};
/**
* @todo This should be renamed to `StringContextBuilder`.
*
* @see NixStringContextElem for explanation why.
*/
typedef std::set<NixStringContextElem> NixStringContext;
} // namespace nix