cont. cleanup: remove superfluous std::string copies

This commit is contained in:
Philipp Otterbein 2024-10-07 01:05:17 +02:00
parent 4c0c8e5428
commit caf3b55891
6 changed files with 21 additions and 17 deletions

View file

@ -40,6 +40,13 @@ namespace nix {
* Miscellaneous
*************************************************************/
static inline Value * mkString(EvalState & state, const std::csub_match & match)
{
Value * v = state.allocValue();
v->mkString({match.first, match.second});
return v;
}
StringMap EvalState::realiseContext(const NixStringContext & context, StorePathSet * maybePathsOut, bool isIFD)
{
std::vector<DerivedPath::Built> drvs;
@ -4268,7 +4275,7 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
if (!match[i + 1].matched)
v2 = &state.vNull;
else
(v2 = state.allocValue())->mkString(match[i + 1].str());
v2 = mkString(state, match[i + 1]);
v.mkList(list);
} catch (std::regex_error & e) {
@ -4352,7 +4359,7 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
auto match = *i;
// Add a string for non-matched characters.
(list[idx++] = state.allocValue())->mkString(match.prefix().str());
list[idx++] = mkString(state, match.prefix());
// Add a list for matched substrings.
const size_t slen = match.size() - 1;
@ -4363,14 +4370,14 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
if (!match[si + 1].matched)
v2 = &state.vNull;
else
(v2 = state.allocValue())->mkString(match[si + 1].str());
v2 = mkString(state, match[si + 1]);
}
(list[idx++] = state.allocValue())->mkList(list2);
// Add a string for non-matched suffix characters.
if (idx == 2 * len)
(list[idx++] = state.allocValue())->mkString(match.suffix().str());
list[idx++] = mkString(state, match.suffix());
}
assert(idx == 2 * len + 1);

View file

@ -90,11 +90,11 @@ struct TunnelLogger : public Logger
{
if (ei.level > verbosity) return;
std::stringstream oss;
std::ostringstream oss;
showErrorInfo(oss, ei, false);
StringSink buf;
buf << STDERR_NEXT << oss.str();
buf << STDERR_NEXT << oss.view();
enqueueMsg(buf.s);
}

View file

@ -30,16 +30,15 @@ std::optional<OutputsSpec> OutputsSpec::parseOpt(std::string_view s)
{
static std::regex regex(std::string { outputSpecRegexStr });
std::smatch match;
std::string s2 { s }; // until some improves std::regex
if (!std::regex_match(s2, match, regex))
std::cmatch match;
if (!std::regex_match(s.cbegin(), s.cend(), match, regex))
return std::nullopt;
if (match[1].matched)
return { OutputsSpec::All {} };
if (match[2].matched)
return OutputsSpec::Names { tokenizeString<StringSet>(match[2].str(), ",") };
return OutputsSpec::Names { tokenizeString<StringSet>({match[2].first, match[2].second}, ",") };
assert(false);
}

View file

@ -293,7 +293,7 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
// We match one space after `nix` so that we preserve indentation.
// No space is necessary for an empty line. An empty line has basically no effect.
if (std::regex_match(line, match, std::regex("^#!\\s*nix(:? |$)(.*)$")))
shebangContent += match[2].str() + "\n";
shebangContent += std::string_view{match[2].first, match[2].second} + "\n";
}
for (const auto & word : parseShebangContent(shebangContent)) {
cmdline.push_back(word);

View file

@ -36,7 +36,7 @@ extern char * * environ __attribute__((weak));
/* Recreate the effect of the perl shellwords function, breaking up a
* string into arguments like a shell word, including escapes
*/
static std::vector<std::string> shellwords(const std::string & s)
static std::vector<std::string> shellwords(std::string_view s)
{
std::regex whitespace("^\\s+");
auto begin = s.cbegin();
@ -51,7 +51,7 @@ static std::vector<std::string> shellwords(const std::string & s)
auto it = begin;
for (; it != s.cend(); ++it) {
if (st == sBegin) {
std::smatch match;
std::cmatch match;
if (regex_search(it, s.cend(), match, whitespace)) {
cur.append(begin, it);
res.push_back(cur);
@ -173,7 +173,7 @@ static void main_nix_build(int argc, char * * argv)
line = chomp(line);
std::smatch match;
if (std::regex_match(line, match, std::regex("^#!\\s*nix-shell\\s+(.*)$")))
for (const auto & word : shellwords(match[1].str()))
for (const auto & word : shellwords({match[1].first, match[1].second}))
args.push_back(word);
}
}

View file

@ -111,9 +111,7 @@ bool createUserEnv(EvalState & state, PackageInfos & elems,
auto manifestFile = ({
std::ostringstream str;
printAmbiguous(manifest, state.symbols, str, nullptr, std::numeric_limits<int>::max());
// TODO with C++20 we can use str.view() instead and avoid copy.
std::string str2 = str.str();
StringSource source { str2 };
StringSource source { str.view() };
state.store->addToStoreFromDump(
source, "env-manifest.nix", FileSerialisationMethod::Flat, ContentAddressMethod::Raw::Text, HashAlgorithm::SHA256, references);
});