cleanup: remove superfluous std::string copies

This commit is contained in:
Philipp Otterbein 2024-10-04 01:24:39 +02:00
parent 3e7b42dd89
commit 4c0c8e5428
8 changed files with 22 additions and 26 deletions

View file

@ -654,7 +654,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
ss << "No documentation found.\n\n";
}
auto markdown = ss.str();
auto markdown = ss.view();
logger->cout(trim(renderMarkdownToTerminal(markdown)));
} else

View file

@ -572,16 +572,13 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
s << docStr;
s << '\0'; // for making a c string below
std::string ss = s.str();
return Doc {
.pos = pos,
.name = name,
.arity = 0, // FIXME: figure out how deep by syntax only? It's not semantically useful though...
.args = {},
.doc =
// FIXME: this leaks; make the field std::string?
strdup(ss.data()),
.doc = makeImmutableString(s.view()), // NOTE: memory leak when compiled without GC
};
}
if (isFunctor(v)) {
@ -1805,11 +1802,9 @@ void ExprIf::eval(EvalState & state, Env & env, Value & v)
void ExprAssert::eval(EvalState & state, Env & env, Value & v)
{
if (!state.evalBool(env, cond, pos, "in the condition of the assert statement")) {
auto exprStr = ({
std::ostringstream out;
cond->show(state.symbols, out);
out.str();
});
std::ostringstream out;
cond->show(state.symbols, out);
auto exprStr = out.view();
if (auto eq = dynamic_cast<ExprOpEq *>(cond)) {
try {

View file

@ -374,11 +374,12 @@ static void getDerivations(EvalState & state, Value & vIn,
bound to the attribute with the "lower" name should take
precedence). */
for (auto & i : v.attrs()->lexicographicOrder(state.symbols)) {
std::string_view symbol{state.symbols[i->name]};
try {
debug("evaluating attribute '%1%'", state.symbols[i->name]);
if (!std::regex_match(std::string(state.symbols[i->name]), attrRegex))
debug("evaluating attribute '%1%'", symbol);
if (!std::regex_match(symbol.begin(), symbol.end(), attrRegex))
continue;
std::string pathPrefix2 = addToPath(pathPrefix, state.symbols[i->name]);
std::string pathPrefix2 = addToPath(pathPrefix, symbol);
if (combineChannels)
getDerivations(state, *i->value, pathPrefix2, autoArgs, drvs, done, ignoreAssertionFailures);
else if (getDerivation(state, *i->value, pathPrefix2, drvs, done, ignoreAssertionFailures)) {
@ -392,7 +393,7 @@ static void getDerivations(EvalState & state, Value & vIn,
}
}
} catch (Error & e) {
e.addTrace(state.positions[i->pos], "while evaluating the attribute '%s'", state.symbols[i->name]);
e.addTrace(state.positions[i->pos], "while evaluating the attribute '%s'", symbol);
throw;
}
}

View file

@ -2129,7 +2129,7 @@ static void prim_toXML(EvalState & state, const PosIdx pos, Value * * args, Valu
std::ostringstream out;
NixStringContext context;
printValueAsXML(state, true, false, *args[0], out, context, pos);
v.mkString(out.str(), context);
v.mkString(out.view(), context);
}
static RegisterPrimOp primop_toXML({
@ -2237,7 +2237,7 @@ static void prim_toJSON(EvalState & state, const PosIdx pos, Value * * args, Val
std::ostringstream out;
NixStringContext context;
printValueAsJSON(state, true, *args[0], pos, out, context);
v.mkString(out.str(), context);
v.mkString(out.view(), context);
}
static RegisterPrimOp primop_toJSON({

View file

@ -66,7 +66,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value * * args, V
attrs.alloc("_type").mkString("timestamp");
std::ostringstream s;
s << t;
attrs.alloc("value").mkString(s.str());
attrs.alloc("value").mkString(s.view());
v.mkAttrs(attrs);
} else {
throw std::runtime_error("Dates and times are not supported");

View file

@ -460,7 +460,7 @@ private:
std::ostringstream s;
s << state.positions[v.payload.lambda.fun->pos];
output << " @ " << filterANSIEscapes(s.str());
output << " @ " << filterANSIEscapes(s.view());
}
} else if (v.isPrimOp()) {
if (v.primOp())

View file

@ -260,9 +260,9 @@ static void main_nix_build(int argc, char * * argv)
// read the shebang to understand which packages to read from. Since
// this is handled via nix-shell -p, we wrap our ruby script execution
// in ruby -e 'load' which ignores the shebangs.
envCommand = fmt("exec %1% %2% -e 'load(ARGV.shift)' -- %3% %4%", execArgs, interpreter, shellEscape(script), joined.str());
envCommand = fmt("exec %1% %2% -e 'load(ARGV.shift)' -- %3% %4%", execArgs, interpreter, shellEscape(script), joined.view());
} else {
envCommand = fmt("exec %1% %2% %3% %4%", execArgs, interpreter, shellEscape(script), joined.str());
envCommand = fmt("exec %1% %2% %3% %4%", execArgs, interpreter, shellEscape(script), joined.view());
}
}

View file

@ -26,17 +26,17 @@ std::string formatProtocol(unsigned int proto)
return "unknown";
}
bool checkPass(const std::string & msg) {
bool checkPass(std::string_view msg) {
notice(ANSI_GREEN "[PASS] " ANSI_NORMAL + msg);
return true;
}
bool checkFail(const std::string & msg) {
bool checkFail(std::string_view msg) {
notice(ANSI_RED "[FAIL] " ANSI_NORMAL + msg);
return false;
}
void checkInfo(const std::string & msg) {
void checkInfo(std::string_view msg) {
notice(ANSI_BLUE "[INFO] " ANSI_NORMAL + msg);
}
@ -91,7 +91,7 @@ struct CmdConfigCheck : StoreCommand
ss << "Multiple versions of nix found in PATH:\n";
for (auto & dir : dirs)
ss << " " << dir << "\n";
return checkFail(ss.str());
return checkFail(ss.view());
}
return checkPass("PATH contains only one nix version.");
@ -132,7 +132,7 @@ struct CmdConfigCheck : StoreCommand
for (auto & dir : dirs)
ss << " " << dir << "\n";
ss << "\n";
return checkFail(ss.str());
return checkFail(ss.view());
}
return checkPass("All profiles are gcroots.");
@ -151,7 +151,7 @@ struct CmdConfigCheck : StoreCommand
<< "sync with the daemon.\n\n"
<< "Client protocol: " << formatProtocol(clientProto) << "\n"
<< "Store protocol: " << formatProtocol(storeProto) << "\n\n";
return checkFail(ss.str());
return checkFail(ss.view());
}
return checkPass("Client protocol matches store protocol.");