1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-10-18 00:16:11 -04:00

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"; ss << "No documentation found.\n\n";
} }
auto markdown = ss.str(); auto markdown = ss.view();
logger->cout(trim(renderMarkdownToTerminal(markdown))); logger->cout(trim(renderMarkdownToTerminal(markdown)));
} else } else

View file

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

View file

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

View file

@ -2129,7 +2129,7 @@ static void prim_toXML(EvalState & state, const PosIdx pos, Value * * args, Valu
std::ostringstream out; std::ostringstream out;
NixStringContext context; NixStringContext context;
printValueAsXML(state, true, false, *args[0], out, context, pos); printValueAsXML(state, true, false, *args[0], out, context, pos);
v.mkString(out.str(), context); v.mkString(out.view(), context);
} }
static RegisterPrimOp primop_toXML({ static RegisterPrimOp primop_toXML({
@ -2237,7 +2237,7 @@ static void prim_toJSON(EvalState & state, const PosIdx pos, Value * * args, Val
std::ostringstream out; std::ostringstream out;
NixStringContext context; NixStringContext context;
printValueAsJSON(state, true, *args[0], pos, out, context); printValueAsJSON(state, true, *args[0], pos, out, context);
v.mkString(out.str(), context); v.mkString(out.view(), context);
} }
static RegisterPrimOp primop_toJSON({ 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"); attrs.alloc("_type").mkString("timestamp");
std::ostringstream s; std::ostringstream s;
s << t; s << t;
attrs.alloc("value").mkString(s.str()); attrs.alloc("value").mkString(s.view());
v.mkAttrs(attrs); v.mkAttrs(attrs);
} else { } else {
throw std::runtime_error("Dates and times are not supported"); throw std::runtime_error("Dates and times are not supported");

View file

@ -460,7 +460,7 @@ private:
std::ostringstream s; std::ostringstream s;
s << state.positions[v.payload.lambda.fun->pos]; s << state.positions[v.payload.lambda.fun->pos];
output << " @ " << filterANSIEscapes(s.str()); output << " @ " << filterANSIEscapes(s.view());
} }
} else if (v.isPrimOp()) { } else if (v.isPrimOp()) {
if (v.primOp()) 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 // read the shebang to understand which packages to read from. Since
// this is handled via nix-shell -p, we wrap our ruby script execution // this is handled via nix-shell -p, we wrap our ruby script execution
// in ruby -e 'load' which ignores the shebangs. // 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 { } 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"; return "unknown";
} }
bool checkPass(const std::string & msg) { bool checkPass(std::string_view msg) {
notice(ANSI_GREEN "[PASS] " ANSI_NORMAL + msg); notice(ANSI_GREEN "[PASS] " ANSI_NORMAL + msg);
return true; return true;
} }
bool checkFail(const std::string & msg) { bool checkFail(std::string_view msg) {
notice(ANSI_RED "[FAIL] " ANSI_NORMAL + msg); notice(ANSI_RED "[FAIL] " ANSI_NORMAL + msg);
return false; return false;
} }
void checkInfo(const std::string & msg) { void checkInfo(std::string_view msg) {
notice(ANSI_BLUE "[INFO] " ANSI_NORMAL + msg); notice(ANSI_BLUE "[INFO] " ANSI_NORMAL + msg);
} }
@ -91,7 +91,7 @@ struct CmdConfigCheck : StoreCommand
ss << "Multiple versions of nix found in PATH:\n"; ss << "Multiple versions of nix found in PATH:\n";
for (auto & dir : dirs) for (auto & dir : dirs)
ss << " " << dir << "\n"; ss << " " << dir << "\n";
return checkFail(ss.str()); return checkFail(ss.view());
} }
return checkPass("PATH contains only one nix version."); return checkPass("PATH contains only one nix version.");
@ -132,7 +132,7 @@ struct CmdConfigCheck : StoreCommand
for (auto & dir : dirs) for (auto & dir : dirs)
ss << " " << dir << "\n"; ss << " " << dir << "\n";
ss << "\n"; ss << "\n";
return checkFail(ss.str()); return checkFail(ss.view());
} }
return checkPass("All profiles are gcroots."); return checkPass("All profiles are gcroots.");
@ -151,7 +151,7 @@ struct CmdConfigCheck : StoreCommand
<< "sync with the daemon.\n\n" << "sync with the daemon.\n\n"
<< "Client protocol: " << formatProtocol(clientProto) << "\n" << "Client protocol: " << formatProtocol(clientProto) << "\n"
<< "Store protocol: " << formatProtocol(storeProto) << "\n\n"; << "Store protocol: " << formatProtocol(storeProto) << "\n\n";
return checkFail(ss.str()); return checkFail(ss.view());
} }
return checkPass("Client protocol matches store protocol."); return checkPass("Client protocol matches store protocol.");