Skip to content

Commit

Permalink
copy context correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bacchanalia committed Jan 12, 2024
1 parent 27de83a commit 824cffe
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,15 @@ void Value::mkString(std::string_view s, const NixStringContext & context)
copyContextToValue(*this, context);
}

void Value::mkEmptyString(const Value & other)
{
mkString("");

NixStringContext context;
copyContext(other, context);
copyContextToValue(*this, context);
}

void Value::mkStringMove(const char * s, const NixStringContext & context)
{
mkString(s);
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3724,10 +3724,10 @@ static void prim_substring(EvalState & state, const PosIdx pos, Value * * args,

// Special-case on empty substring to avoid O(n) strlen
// This allows for the use of empty substrings to efficently capture string context
if (len <= 0) {
if (len == 0) {
state.forceValue(*args[2], pos);
if (args[2]->type() == nString) {
v.mkString("", args[2]->string.context);
v.mkEmptyString(*args[2]);
return;
}
}
Expand Down
Binary file added src/libexpr/tests/libnixexpr-tests
Binary file not shown.
2 changes: 2 additions & 0 deletions src/libexpr/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ public:

void mkStringMove(const char * s, const NixStringContext & context);

void mkEmptyString(const Value & other);

inline void mkString(const Symbol & s)
{
mkString(((const std::string &) s).c_str());
Expand Down
Binary file added src/libstore/tests/libnixstore-tests
Binary file not shown.
Binary file added src/libutil/tests/libnixutil-tests
Binary file not shown.

0 comments on commit 824cffe

Please sign in to comment.