From 947c50911043dbe9176dca15538bd942a8dc3b0b Mon Sep 17 00:00:00 2001 From: Mel Zuser Date: Wed, 15 May 2024 21:20:08 -0700 Subject: [PATCH] libexpr: prefer Value::str() over Value::c_str() at call sites that already cast to string_view to take advantage of the stored length Change-Id: I525c68e83e1c71bdc5ad2c3ed0bd0863ad20aaf2 --- src/libexpr/eval.cc | 4 ++-- src/libexpr/flake/flake.cc | 2 +- src/libexpr/primops.cc | 2 +- src/libexpr/print-ambiguous.cc | 2 +- src/libexpr/print.cc | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index f727f9a7b..3e00a130b 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -264,7 +264,7 @@ static Symbol getName(const AttrName & name, EvalState & state, Env & env) Value nameValue; name.expr->eval(state, env, nameValue); state.forceStringNoCtx(nameValue, name.expr->getPos(), "while evaluating an attribute name"); - return state.symbols.create(nameValue.c_str()); + return state.symbols.create(nameValue.str()); } } @@ -1348,7 +1348,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v) if (nameVal.type() == nNull) continue; state.forceStringNoCtx(nameVal, i.pos, "while evaluating the name of a dynamic attribute"); - auto nameSym = state.symbols.create(nameVal.c_str()); + auto nameSym = state.symbols.create(nameVal.str()); Bindings::iterator j = v.attrs->find(nameSym); if (j != v.attrs->end()) state.error("dynamic attribute '%1%' already defined at %2%", state.symbols[nameSym], state.positions[j->pos]).atPos(i.pos).withFrame(env, *this).debugThrow(); diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc index 20bbd1898..107bc43a0 100644 --- a/src/libexpr/flake/flake.cc +++ b/src/libexpr/flake/flake.cc @@ -122,7 +122,7 @@ static FlakeInput parseFlakeInput(EvalState & state, input.overrides = parseFlakeInputs(state, attr.value, attr.pos, baseDir, lockRootPath, depth + 1); } else if (attr.name == sFollows) { expectType(state, nString, *attr.value, attr.pos); - auto follows(parseInputPath(attr.value->c_str())); + auto follows(parseInputPath(attr.value->str())); follows.insert(follows.begin(), lockRootPath.begin(), lockRootPath.end()); input.follows = follows; } else { diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 6aafe23f5..943d0cdd8 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -2590,7 +2590,7 @@ static void prim_removeAttrs(EvalState & state, const PosIdx pos, Value * * args names.reserve(args[1]->listSize()); for (auto elem : args[1]->listItems()) { state.forceStringNoCtx(*elem, pos, "while evaluating the values of the second argument passed to builtins.removeAttrs"); - names.emplace_back(state.symbols.create(elem->c_str()), nullptr); + names.emplace_back(state.symbols.create(elem->str()), nullptr); } std::sort(names.begin(), names.end()); diff --git a/src/libexpr/print-ambiguous.cc b/src/libexpr/print-ambiguous.cc index 022358053..79e1adfc1 100644 --- a/src/libexpr/print-ambiguous.cc +++ b/src/libexpr/print-ambiguous.cc @@ -28,7 +28,7 @@ void printAmbiguous( printLiteralBool(str, v.boolean); break; case nString: - escapeString(str, v.c_str()); + escapeString(str, v.str()); break; case nPath: str << v.path().to_string(); // !!! escaping? diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index 6b91ed272..34879d7b2 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -200,7 +200,7 @@ private: { escapeString( output, - v.c_str(), + v.str(), { .maxLength = options.maxStringLength, .outputAnsiColors = options.ansiColors,