From bf452cbc2ae2b209ec262ce858deca470d086f24 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 25 May 2020 15:54:14 +0100 Subject: [PATCH] refactor(3p/nix): Replace tokenizeStrings with absl::StrSplit This function was a custom (and inefficient in the case of single-character delimiters) string splitter which was used all over the codebase. Abseil provides an appropriate replacement function. --- third_party/nix/src/libexpr/primops.cc | 3 +- .../nix/src/libexpr/primops/fetchGit.cc | 6 ++-- .../nix/src/libexpr/primops/fetchMercurial.cc | 14 +++++---- .../nix/src/libstore/binary-cache-store.cc | 3 +- third_party/nix/src/libstore/build.cc | 7 +++-- .../nix/src/libstore/builtins/buildenv.cc | 14 +++++---- third_party/nix/src/libstore/derivations.cc | 18 +++++++----- third_party/nix/src/libstore/derivations.hh | 2 +- third_party/nix/src/libstore/download.cc | 7 +++-- third_party/nix/src/libstore/gc.cc | 27 ++--------------- third_party/nix/src/libstore/globals.cc | 18 +++++------- third_party/nix/src/libstore/local-store.cc | 10 ++++--- third_party/nix/src/libstore/local-store.hh | 6 +++- third_party/nix/src/libstore/machines.cc | 20 ++++++++----- .../nix/src/libstore/nar-info-disk-cache.cc | 11 ++++--- third_party/nix/src/libstore/nar-info.cc | 3 +- .../nix/src/libstore/parsed-derivations.cc | 4 ++- third_party/nix/src/libstore/ssh.cc | 6 ++-- third_party/nix/src/libstore/store-api.cc | 4 ++- third_party/nix/src/libutil/config.cc | 14 +++++---- third_party/nix/src/libutil/util.cc | 29 +++---------------- third_party/nix/src/libutil/util.hh | 7 +---- third_party/nix/src/nix-build/nix-build.cc | 6 ++-- .../nix/src/nix-channel/nix-channel.cc | 11 ++++--- third_party/nix/src/nix-daemon/nix-daemon.cc | 2 +- third_party/nix/src/nix/doctor.cc | 12 ++++---- third_party/nix/src/nix/edit.cc | 3 +- third_party/nix/src/nix/run.cc | 18 ++++++------ third_party/nix/src/nix/upgrade-nix.cc | 6 ++-- 29 files changed, 146 insertions(+), 145 deletions(-) diff --git a/third_party/nix/src/libexpr/primops.cc b/third_party/nix/src/libexpr/primops.cc index fa0057041..8ade52bd9 100644 --- a/third_party/nix/src/libexpr/primops.cc +++ b/third_party/nix/src/libexpr/primops.cc @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -712,7 +713,7 @@ static void prim_derivationStrict(EvalState& state, const Pos& pos, } else if (i->name == state.sOutputHashMode) { handleHashMode(s); } else if (i->name == state.sOutputs) { - handleOutputs(tokenizeString(s)); + handleOutputs(absl::StrSplit(s, absl::ByAnyChar(" \t\n\r"))); } } } diff --git a/third_party/nix/src/libexpr/primops/fetchGit.cc b/third_party/nix/src/libexpr/primops/fetchGit.cc index 99f1c3c4b..2cfdefe7a 100644 --- a/third_party/nix/src/libexpr/primops/fetchGit.cc +++ b/third_party/nix/src/libexpr/primops/fetchGit.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -54,8 +55,9 @@ GitInfo exportGit(ref store, const std::string& uri, gitInfo.rev = "0000000000000000000000000000000000000000"; gitInfo.shortRev = std::string(gitInfo.rev, 0, 7); - auto files = tokenizeString>( - runProgram("git", true, {"-C", uri, "ls-files", "-z"}), "\0"s); + std::set files = + absl::StrSplit(runProgram("git", true, {"-C", uri, "ls-files", "-z"}), + absl::ByChar('\0')); PathFilter filter = [&](const Path& p) -> bool { assert(absl::StartsWith(p, uri)); diff --git a/third_party/nix/src/libexpr/primops/fetchMercurial.cc b/third_party/nix/src/libexpr/primops/fetchMercurial.cc index b6d4a5e1c..71722faed 100644 --- a/third_party/nix/src/libexpr/primops/fetchMercurial.cc +++ b/third_party/nix/src/libexpr/primops/fetchMercurial.cc @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -48,11 +49,11 @@ HgInfo exportMercurial(ref store, const std::string& uri, hgInfo.branch = absl::StripTrailingAsciiWhitespace( runProgram("hg", true, {"branch", "-R", uri})); - auto files = tokenizeString>( + std::set files = absl::StrSplit( runProgram("hg", true, {"status", "-R", uri, "--clean", "--modified", "--added", "--no-status", "--print0"}), - "\0"s); + absl::ByChar('\0')); PathFilter filter = [&](const Path& p) -> bool { assert(absl::StartsWith(p, uri)); @@ -124,10 +125,11 @@ HgInfo exportMercurial(ref store, const std::string& uri, writeFile(stampFile, ""); } - auto tokens = tokenizeString>( - runProgram("hg", true, - {"log", "-R", cacheDir, "-r", rev, "--template", - "{node} {rev} {branch}"})); + std::vector tokens = + absl::StrSplit(runProgram("hg", true, + {"log", "-R", cacheDir, "-r", rev, "--template", + "{node} {rev} {branch}"}), + absl::ByAnyChar(" \t\n\r")); assert(tokens.size() == 3); HgInfo hgInfo; diff --git a/third_party/nix/src/libstore/binary-cache-store.cc b/third_party/nix/src/libstore/binary-cache-store.cc index ea3607843..f631c9dee 100644 --- a/third_party/nix/src/libstore/binary-cache-store.cc +++ b/third_party/nix/src/libstore/binary-cache-store.cc @@ -6,6 +6,7 @@ #include #include +#include #include "archive.hh" #include "compression.hh" @@ -41,7 +42,7 @@ void BinaryCacheStore::init() { upsertFile(cacheInfoFile, "StoreDir: " + storeDir + "\n", "text/x-nix-cache-info"); } else { - for (auto& line : tokenizeString(*cacheInfo, "\n")) { + for (auto& line : absl::StrSplit(*cacheInfo, absl::ByChar('\n'))) { size_t colon = line.find(':'); if (colon == std::string::npos) { continue; diff --git a/third_party/nix/src/libstore/build.cc b/third_party/nix/src/libstore/build.cc index c0fa0074d..a60f5ef17 100644 --- a/third_party/nix/src/libstore/build.cc +++ b/third_party/nix/src/libstore/build.cc @@ -10,10 +10,12 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -1968,7 +1970,7 @@ void DerivationGoal::startBuilder() { by `nix-store --register-validity'. However, the deriver fields are left empty. */ std::string s = get(drv->env, "exportReferencesGraph"); - auto ss = tokenizeString(s); + std::vector ss = absl::StrSplit(s, absl::ByAnyChar(" \t\n\r")); if (ss.size() % 2 != 0) { throw BuildError( format("odd number of tokens in 'exportReferencesGraph': '%1%'") % s); @@ -2481,7 +2483,8 @@ void DerivationGoal::initTmpDir() { needed (attributes are not passed through the environment, so there is no size constraint). */ if (!parsedDrv->getStructuredAttrs()) { - auto passAsFile = tokenizeString(get(drv->env, "passAsFile")); + std::set passAsFile = + absl::StrSplit(get(drv->env, "passAsFile"), absl::ByAnyChar(" \t\n\r")); int fileNr = 0; for (auto& i : drv->env) { if (passAsFile.find(i.first) == passAsFile.end()) { diff --git a/third_party/nix/src/libstore/builtins/buildenv.cc b/third_party/nix/src/libstore/builtins/buildenv.cc index 92cf2f8c1..db093663b 100644 --- a/third_party/nix/src/libstore/builtins/buildenv.cc +++ b/third_party/nix/src/libstore/builtins/buildenv.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -134,12 +135,14 @@ static void addPkg(const Path& pkgDir, int priority) { createLinks(pkgDir, out, priority); try { - for (const auto& p : tokenizeString>( + for (auto p : absl::StrSplit( readFile(pkgDir + "/nix-support/propagated-user-env-packages"), - " \n")) - if (!done.count(p)) { - postponed.insert(p); + absl::ByAnyChar(" \n"))) { + auto pkg = std::string(p); + if (!done.count(pkg)) { + postponed.insert(pkg); } + } } catch (SysError& e) { if (e.errNo != ENOENT && e.errNo != ENOTDIR) { throw; @@ -172,7 +175,8 @@ void builtinBuildenv(const BasicDerivation& drv) { /* Convert the stuff we get from the environment back into a * coherent data type. */ Packages pkgs; - auto derivations = tokenizeString(getAttr("derivations")); + Strings derivations = + absl::StrSplit(getAttr("derivations"), absl::ByAnyChar(" \t\n\r")); while (!derivations.empty()) { /* !!! We're trusting the caller to structure derivations env var correctly */ diff --git a/third_party/nix/src/libstore/derivations.cc b/third_party/nix/src/libstore/derivations.cc index 9f2aa5ea9..b2590f5e2 100644 --- a/third_party/nix/src/libstore/derivations.cc +++ b/third_party/nix/src/libstore/derivations.cc @@ -1,6 +1,8 @@ #include "derivations.hh" #include +#include +#include #include "fs-accessor.hh" #include "globals.hh" @@ -357,13 +359,15 @@ Hash hashDerivationModulo(Store& store, Derivation drv) { return hashString(htSHA256, drv.unparse()); } -DrvPathWithOutputs parseDrvPathWithOutputs(const std::string& s) { - size_t n = s.find('!'); - return n == std::string::npos - ? DrvPathWithOutputs(s, std::set()) - : DrvPathWithOutputs(std::string(s, 0, n), - tokenizeString >( - std::string(s, n + 1), ",")); +// TODO(tazjin): doc comment? +DrvPathWithOutputs parseDrvPathWithOutputs(absl::string_view path) { + auto pos = path.find('!'); + if (pos == absl::string_view::npos) { + return DrvPathWithOutputs(path, std::set()); + } + + return DrvPathWithOutputs(path.substr(pos + 1), + absl::StrSplit(path, absl::ByChar(','))); } Path makeDrvPathWithOutputs(const Path& drvPath, diff --git a/third_party/nix/src/libstore/derivations.hh b/third_party/nix/src/libstore/derivations.hh index ae365e68b..170f09804 100644 --- a/third_party/nix/src/libstore/derivations.hh +++ b/third_party/nix/src/libstore/derivations.hh @@ -88,7 +88,7 @@ extern DrvHashes drvHashes; // FIXME: global, not thread-safe (/nix/store/hash-foo!out1,out2,...) into the derivation path and the outputs. */ typedef std::pair > DrvPathWithOutputs; -DrvPathWithOutputs parseDrvPathWithOutputs(const std::string& s); +DrvPathWithOutputs parseDrvPathWithOutputs(absl::string_view path); Path makeDrvPathWithOutputs(const Path& drvPath, const std::set& outputs); diff --git a/third_party/nix/src/libstore/download.cc b/third_party/nix/src/libstore/download.cc index 8b36063da..92a630872 100644 --- a/third_party/nix/src/libstore/download.cc +++ b/third_party/nix/src/libstore/download.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include "archive.hh" #include "compression.hh" @@ -180,7 +181,7 @@ struct CurlDownloader : public Downloader { << "': " << absl::StripAsciiWhitespace(line); if (line.compare(0, 5, "HTTP/") == 0) { // new response starts result.etag = ""; - auto ss = tokenizeString>(line, " "); + std::vector ss = absl::StrSplit(line, absl::ByChar(' ')); status = ss.size() >= 2 ? ss[1] : ""; result.data = std::make_shared(); result.bodySize = 0; @@ -894,8 +895,8 @@ CachedDownloadResult Downloader::downloadCached( storePath = readLink(fileLink); store->addTempRoot(storePath); if (store->isValidPath(storePath)) { - auto ss = - tokenizeString>(readFile(dataFile), "\n"); + std::vector ss = + absl::StrSplit(readFile(dataFile), absl::ByChar('\n')); if (ss.size() >= 3 && ss[0] == url) { time_t lastChecked; if (absl::SimpleAtoi(ss[2], &lastChecked) && diff --git a/third_party/nix/src/libstore/gc.cc b/third_party/nix/src/libstore/gc.cc index 262f17df0..5c1ea3d58 100644 --- a/third_party/nix/src/libstore/gc.cc +++ b/third_party/nix/src/libstore/gc.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -422,8 +423,8 @@ void LocalStore::findRuntimeRoots(Roots& roots, bool censor) { try { auto mapFile = fmt("/proc/%s/maps", ent->d_name); - auto mapLines = tokenizeString>( - readFile(mapFile, true), "\n"); + std::vector mapLines = + absl::StrSplit(readFile(mapFile, true), absl::ByChar('\n')); for (const auto& line : mapLines) { auto match = std::smatch{}; if (std::regex_match(line, match, mapRegex)) { @@ -452,31 +453,9 @@ void LocalStore::findRuntimeRoots(Roots& roots, bool censor) { } } -#if !defined(__linux__) - // lsof is really slow on OS X. This actually causes the gc-concurrent.sh test - // to fail. See: https://github.com/NixOS/nix/issues/3011 Because of this we - // disable lsof when running the tests. - if (getEnv("_NIX_TEST_NO_LSOF") == "") { - try { - std::regex lsofRegex(R"(^n(/.*)$)"); - auto lsofLines = tokenizeString>( - runProgram(LSOF, true, {"-n", "-w", "-F", "n"}), "\n"); - for (const auto& line : lsofLines) { - std::smatch match; - if (std::regex_match(line, match, lsofRegex)) - unchecked[match[1]].emplace("{lsof}"); - } - } catch (ExecError& e) { - /* lsof not installed, lsof failed */ - } - } -#endif - -#if defined(__linux__) readFileRoots("/proc/sys/kernel/modprobe", unchecked); readFileRoots("/proc/sys/kernel/fbsplash", unchecked); readFileRoots("/proc/sys/kernel/poweroff_cmd", unchecked); -#endif for (auto& [target, links] : unchecked) { if (isInStore(target)) { diff --git a/third_party/nix/src/libstore/globals.cc b/third_party/nix/src/libstore/globals.cc index 7307f0352..f218534cc 100644 --- a/third_party/nix/src/libstore/globals.cc +++ b/third_party/nix/src/libstore/globals.cc @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include "archive.hh" @@ -20,9 +22,6 @@ namespace nix { must be deleted and recreated on startup.) */ #define DEFAULT_SOCKET_PATH "/daemon-socket/socket" -// TODO(tazjin): this was __APPLE__ specific, still needed? -#define DEFAULT_ALLOWED_IMPURE_PREFIXES "" - Settings settings; static GlobalConfig::Register r1(&settings); @@ -55,21 +54,18 @@ Settings::Settings() } /* Backwards compatibility. */ + // TODO(tazjin): still? auto s = getEnv("NIX_REMOTE_SYSTEMS"); if (!s.empty()) { Strings ss; - for (auto& p : tokenizeString(s, ":")) { - ss.push_back("@" + p); + for (auto p : absl::StrSplit(s, absl::ByChar(':'))) { + ss.push_back(absl::StrCat("@", p)); } builders = concatStringsSep(" ", ss); } -#if defined(__linux__) && defined(SANDBOX_SHELL) - sandboxPaths = tokenizeString("/bin/sh=" SANDBOX_SHELL); -#endif - - allowedImpureHostPrefixes = - tokenizeString(DEFAULT_ALLOWED_IMPURE_PREFIXES); + sandboxPaths = + absl::StrSplit("/bin/sh=" SANDBOX_SHELL, absl::ByAnyChar(" \t\n\r")); } void loadConfFile() { diff --git a/third_party/nix/src/libstore/local-store.cc b/third_party/nix/src/libstore/local-store.cc index faea0fbce..ebc912969 100644 --- a/third_party/nix/src/libstore/local-store.cc +++ b/third_party/nix/src/libstore/local-store.cc @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -477,14 +478,15 @@ static void canonicalisePathMetaData_(const Path& path, uid_t fromUid, throw SysError("querying extended attributes of '%s'", path); } - for (auto& eaName : tokenizeString( - std::string(eaBuf.data(), eaSize), std::string("\000", 1))) { + for (auto& eaName : + absl::StrSplit(std::string(eaBuf.data(), eaSize), + absl::ByString(std::string("\000", 1)))) { /* Ignore SELinux security labels since these cannot be removed even by root. */ if (eaName == "security.selinux") { continue; } - if (lremovexattr(path.c_str(), eaName.c_str()) == -1) { + if (lremovexattr(path.c_str(), std::string(eaName).c_str()) == -1) { throw SysError("removing extended attribute '%s' from '%s'", eaName, path); } @@ -702,7 +704,7 @@ void LocalStore::queryPathInfoUncached( s = (const char*)sqlite3_column_text(state->stmtQueryPathInfo, 6); if (s != nullptr) { - info->sigs = tokenizeString(s, " "); + info->sigs = absl::StrSplit(s, absl::ByChar(' ')); } s = (const char*)sqlite3_column_text(state->stmtQueryPathInfo, 7); diff --git a/third_party/nix/src/libstore/local-store.hh b/third_party/nix/src/libstore/local-store.hh index c1bfc0876..178cadf92 100644 --- a/third_party/nix/src/libstore/local-store.hh +++ b/third_party/nix/src/libstore/local-store.hh @@ -5,6 +5,8 @@ #include #include +#include + #include "pathlocks.hh" #include "sqlite.hh" #include "store-api.hh" @@ -94,7 +96,9 @@ class LocalStore : public LocalFSStore { public: // Hack for build-remote.cc. - PathSet locksHeld = tokenizeString(getEnv("NIX_HELD_LOCKS")); + // TODO(tazjin): remove this when we've got gRPC + PathSet locksHeld = + absl::StrSplit(getEnv("NIX_HELD_LOCKS"), absl::ByAnyChar(" \t\n\r")); /* Initialise the local store, upgrading the schema if necessary. */ diff --git a/third_party/nix/src/libstore/machines.cc b/third_party/nix/src/libstore/machines.cc index bbb84784c..9ad20a25f 100644 --- a/third_party/nix/src/libstore/machines.cc +++ b/third_party/nix/src/libstore/machines.cc @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -51,14 +52,15 @@ bool Machine::mandatoryMet(const std::set& features) const { } void parseMachines(const std::string& s, Machines& machines) { - for (auto line : tokenizeString>(s, "\n;")) { - line.erase(std::find(line.begin(), line.end(), '#'), line.end()); - if (line.empty()) { + for (auto line : absl::StrSplit(s, absl::ByAnyChar("\n;"))) { + // Skip empty lines & comments + line = absl::StripAsciiWhitespace(line); + if (line.empty() || line[line.find_first_not_of(" \t")] == '#') { continue; } if (line[0] == '@') { - auto file = absl::StripAsciiWhitespace(std::string(line, 1)); + auto file = absl::StripAsciiWhitespace(line.substr(1)); try { parseMachines(readFile(file), machines); } catch (const SysError& e) { @@ -70,7 +72,8 @@ void parseMachines(const std::string& s, Machines& machines) { continue; } - auto tokens = tokenizeString>(line); + std::vector tokens = + absl::StrSplit(line, absl::ByAnyChar(" \t\n\r")); auto sz = tokens.size(); if (sz < 1) { throw FormatError("bad machine specification '%s'", line); @@ -80,15 +83,16 @@ void parseMachines(const std::string& s, Machines& machines) { return tokens.size() > n && !tokens[n].empty() && tokens[n] != "-"; }; + // TODO(tazjin): what??? machines.emplace_back( tokens[0], - isSet(1) ? tokenizeString>(tokens[1], ",") + isSet(1) ? absl::StrSplit(tokens[1], absl::ByChar(',')) : std::vector{settings.thisSystem}, isSet(2) ? tokens[2] : "", isSet(3) ? std::stoull(tokens[3]) : 1LL, isSet(4) ? std::stoull(tokens[4]) : 1LL, - isSet(5) ? tokenizeString>(tokens[5], ",") + isSet(5) ? absl::StrSplit(tokens[5], absl::ByChar(',')) : std::set{}, - isSet(6) ? tokenizeString>(tokens[6], ",") + isSet(6) ? absl::StrSplit(tokens[6], absl::ByChar(',')) : std::set{}, isSet(7) ? tokens[7] : ""); } diff --git a/third_party/nix/src/libstore/nar-info-disk-cache.cc b/third_party/nix/src/libstore/nar-info-disk-cache.cc index 19ef2c9d7..0a7a0dc22 100644 --- a/third_party/nix/src/libstore/nar-info-disk-cache.cc +++ b/third_party/nix/src/libstore/nar-info-disk-cache.cc @@ -1,5 +1,7 @@ #include "nar-info-disk-cache.hh" +#include +#include #include #include @@ -227,14 +229,15 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache { narInfo->fileSize = queryNAR.getInt(5); narInfo->narHash = Hash(queryNAR.getStr(6)); narInfo->narSize = queryNAR.getInt(7); - for (auto& r : tokenizeString(queryNAR.getStr(8), " ")) { - narInfo->references.insert(cache.storeDir + "/" + r); + for (auto r : absl::StrSplit(queryNAR.getStr(8), absl::ByChar(' '))) { + narInfo->references.insert(absl::StrCat(cache.storeDir, "/", r)); } if (!queryNAR.isNull(9)) { narInfo->deriver = cache.storeDir + "/" + queryNAR.getStr(9); } - for (auto& sig : tokenizeString(queryNAR.getStr(10), " ")) { - narInfo->sigs.insert(sig); + for (auto& sig : + absl::StrSplit(queryNAR.getStr(10), absl::ByChar(' '))) { + narInfo->sigs.insert(std::string(sig)); } narInfo->ca = queryNAR.getStr(11); diff --git a/third_party/nix/src/libstore/nar-info.cc b/third_party/nix/src/libstore/nar-info.cc index 9f4c53e19..1dc0b54cf 100644 --- a/third_party/nix/src/libstore/nar-info.cc +++ b/third_party/nix/src/libstore/nar-info.cc @@ -1,6 +1,7 @@ #include "nar-info.hh" #include +#include #include "globals.hh" @@ -59,7 +60,7 @@ NarInfo::NarInfo(const Store& store, const std::string& s, corrupt(); } } else if (name == "References") { - auto refs = tokenizeString(value, " "); + std::vector refs = absl::StrSplit(value, absl::ByChar(' ')); if (!references.empty()) { corrupt(); } diff --git a/third_party/nix/src/libstore/parsed-derivations.cc b/third_party/nix/src/libstore/parsed-derivations.cc index 118620324..571f49ad5 100644 --- a/third_party/nix/src/libstore/parsed-derivations.cc +++ b/third_party/nix/src/libstore/parsed-derivations.cc @@ -1,5 +1,7 @@ #include "parsed-derivations.hh" +#include + namespace nix { ParsedDerivation::ParsedDerivation(const Path& drvPath, BasicDerivation& drv) @@ -86,7 +88,7 @@ std::optional ParsedDerivation::getStringsAttr( if (i == drv.env.end()) { return {}; } - return tokenizeString(i->second); + return absl::StrSplit(i->second, absl::ByAnyChar(" \t\n\r")); } } diff --git a/third_party/nix/src/libstore/ssh.cc b/third_party/nix/src/libstore/ssh.cc index 76c78b2fc..1b09eb42c 100644 --- a/third_party/nix/src/libstore/ssh.cc +++ b/third_party/nix/src/libstore/ssh.cc @@ -3,6 +3,7 @@ #include #include +#include namespace nix { @@ -20,8 +21,9 @@ SSHMaster::SSHMaster(const std::string& host, std::string keyFile, } void SSHMaster::addCommonSSHOpts(Strings& args) { - for (auto& i : tokenizeString(getEnv("NIX_SSHOPTS"))) { - args.push_back(i); + for (auto& i : + absl::StrSplit(getEnv("NIX_SSHOPTS"), absl::ByAnyChar(" \t\n\r"))) { + args.push_back(std::string(i)); } if (!keyFile.empty()) { args.insert(args.end(), {"-i", keyFile}); diff --git a/third_party/nix/src/libstore/store-api.cc b/third_party/nix/src/libstore/store-api.cc index 9440d78d5..afdbc14e2 100644 --- a/third_party/nix/src/libstore/store-api.cc +++ b/third_party/nix/src/libstore/store-api.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include "crypto.hh" @@ -858,7 +859,8 @@ std::pair splitUriAndParams( Store::Params params; auto q = uri.find('?'); if (q != std::string::npos) { - for (const auto& s : tokenizeString(uri.substr(q + 1), "&")) { + Strings parts = absl::StrSplit(uri.substr(q + 1), absl::ByChar('&')); + for (const auto& s : parts) { auto e = s.find('='); if (e != std::string::npos) { auto value = s.substr(e + 1); diff --git a/third_party/nix/src/libutil/config.cc b/third_party/nix/src/libutil/config.cc index bc81ce77c..9e3f6f85c 100644 --- a/third_party/nix/src/libutil/config.cc +++ b/third_party/nix/src/libutil/config.cc @@ -1,14 +1,16 @@ -#define GOOGLE_STRIP_LOG 0 #include "config.hh" +#include #include +#include #include +#include +#include #include #include "args.hh" #include "json.hh" -// #include namespace nix { @@ -99,7 +101,9 @@ void AbstractConfig::applyConfigFile(const Path& path) { line = std::string(line, 0, hash); } - auto tokens = tokenizeString >(line); + // TODO(tazjin): absl::string_view after path functions are fixed. + std::vector tokens = + absl::StrSplit(line, absl::ByAnyChar(" \t\n\r")); if (tokens.empty()) { continue; } @@ -258,7 +262,7 @@ void BaseSetting::convertToArg(Args& args, const std::string& category) { template <> void BaseSetting::set(const std::string& str) { - value = tokenizeString(str); + value = absl::StrSplit(str, absl::ByAnyChar(" \t\n\r")); } template <> @@ -276,7 +280,7 @@ void BaseSetting::toJSON(JSONPlaceholder& out) { template <> void BaseSetting::set(const std::string& str) { - value = tokenizeString(str); + value = absl::StrSplit(str, absl::ByAnyChar(" \t\n\r")); } template <> diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc index fb9c6bfd5..3f541134b 100644 --- a/third_party/nix/src/libutil/util.cc +++ b/third_party/nix/src/libutil/util.cc @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -165,7 +166,7 @@ Path canonPath(const Path& path, bool resolveSymlinks) { return s.empty() ? "/" : s; } -Path dirOf(const Path& path) { +Path dirOf(absl::string_view path) { Path::size_type pos = path.rfind('/'); if (pos == std::string::npos) { return "."; @@ -542,7 +543,8 @@ Path getConfigDir() { std::vector getConfigDirs() { Path configHome = getConfigDir(); std::string configDirs = getEnv("XDG_CONFIG_DIRS"); - auto result = tokenizeString>(configDirs, ":"); + std::vector result = + absl::StrSplit(configDirs, absl::ByChar(':')); result.insert(result.begin(), configHome); return result; } @@ -1170,29 +1172,6 @@ void _interrupted() { ////////////////////////////////////////////////////////////////////// -template -C tokenizeString(const std::string& s, const std::string& separators) { - C result; - std::string::size_type pos = s.find_first_not_of(separators, 0); - while (pos != std::string::npos) { - std::string::size_type end = s.find_first_of(separators, pos + 1); - if (end == std::string::npos) { - end = s.size(); - } - std::string token(s, pos, end - pos); - result.insert(result.end(), token); - pos = s.find_first_not_of(separators, end); - } - return result; -} - -template Strings tokenizeString(const std::string& s, - const std::string& separators); -template StringSet tokenizeString(const std::string& s, - const std::string& separators); -template std::vector tokenizeString(const std::string& s, - const std::string& separators); - std::string concatStringsSep(const std::string& sep, const Strings& ss) { std::string s; for (auto& i : ss) { diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh index 51d9979e1..27e6b47fb 100644 --- a/third_party/nix/src/libutil/util.hh +++ b/third_party/nix/src/libutil/util.hh @@ -56,7 +56,7 @@ Path canonPath(const Path& path, bool resolveSymlinks = false); everything before the final `/'. If the path is the root or an immediate child thereof (e.g., `/foo'), this means an empty string is returned. */ -Path dirOf(const Path& path); +Path dirOf(absl::string_view path); /* Return the base name of the given canonical path, i.e., everything following the final `/'. */ @@ -316,11 +316,6 @@ MakeError(Interrupted, BaseError); MakeError(FormatError, Error); -/* String tokenizer. */ -template -C tokenizeString(const std::string& s, - const std::string& separators = " \t\n\r"); - /* Concatenate the given strings with a separator between the elements. */ std::string concatStringsSep(const std::string& sep, const Strings& ss); diff --git a/third_party/nix/src/nix-build/nix-build.cc b/third_party/nix/src/nix-build/nix-build.cc index 6906a8058..938452f71 100644 --- a/third_party/nix/src/nix-build/nix-build.cc +++ b/third_party/nix/src/nix-build/nix-build.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include "affinity.hh" @@ -113,7 +114,7 @@ static void _main(int argc, char** argv) { !std::regex_search(argv[1], std::regex("nix-shell"))) { script = argv[1]; try { - auto lines = tokenizeString(readFile(script), "\n"); + Strings lines = absl::StrSplit(readFile(script), absl::ByChar('\n')); if (std::regex_search(lines.front(), std::regex("^#!"))) { lines.pop_front(); inShebang = true; @@ -444,7 +445,8 @@ static void _main(int argc, char** argv) { env["NIX_STORE"] = store->storeDir; env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores); - auto passAsFile = tokenizeString(get(drv.env, "passAsFile", "")); + StringSet passAsFile = absl::StrSplit(get(drv.env, "passAsFile", ""), + absl::ByAnyChar(" \t\n\r")); bool keepTmp = false; int fileNr = 0; diff --git a/third_party/nix/src/nix-channel/nix-channel.cc b/third_party/nix/src/nix-channel/nix-channel.cc index 04a492d80..01fc788b9 100644 --- a/third_party/nix/src/nix-channel/nix-channel.cc +++ b/third_party/nix/src/nix-channel/nix-channel.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -24,13 +25,15 @@ static void readChannels() { } auto channelsFile = readFile(channelsList); - for (const auto& line : - tokenizeString>(channelsFile, "\n")) { - absl::StripTrailingAsciiWhitespace(line); + std::vector lines = + absl::StrSplit(channelsFile, absl::ByChar('\n')); + + for (auto& line : lines) { + line = absl::StripTrailingAsciiWhitespace(line); if (std::regex_search(line, std::regex("^\\s*\\#"))) { continue; } - auto split = tokenizeString>(line, " "); + std::vector split = absl::StrSplit(line, absl::ByChar(' ')); auto url = std::regex_replace(split[0], std::regex("/*$"), ""); auto name = split.size() > 1 ? split[1] : baseNameOf(url); channels[name] = url; diff --git a/third_party/nix/src/nix-daemon/nix-daemon.cc b/third_party/nix/src/nix-daemon/nix-daemon.cc index 5b62d67db..4373c33e8 100644 --- a/third_party/nix/src/nix-daemon/nix-daemon.cc +++ b/third_party/nix/src/nix-daemon/nix-daemon.cc @@ -526,7 +526,7 @@ static void performOp(TunnelLogger* logger, const ref& store, trusted.insert(s); } Strings subs; - auto ss = tokenizeString(value); + Strings ss = absl::StrSplit(value, absl::ByAnyChar(" \t\n\r")); for (auto& s : ss) { if (trusted.count(s) != 0u) { subs.push_back(s); diff --git a/third_party/nix/src/nix/doctor.cc b/third_party/nix/src/nix/doctor.cc index 79e9eb539..c7d133fda 100644 --- a/third_party/nix/src/nix/doctor.cc +++ b/third_party/nix/src/nix/doctor.cc @@ -1,4 +1,6 @@ #include +#include +#include #include "command.hh" #include "serve-protocol.hh" @@ -46,9 +48,9 @@ struct CmdDoctor : StoreCommand { static bool checkNixInPath() { PathSet dirs; - for (auto& dir : tokenizeString(getEnv("PATH"), ":")) { - if (pathExists(dir + "/nix-env")) { - dirs.insert(dirOf(canonPath(dir + "/nix-env", true))); + for (auto& dir : absl::StrSplit(getEnv("PATH"), absl::ByChar(':'))) { + if (pathExists(absl::StrCat(dir, "/nix-env"))) { + dirs.insert(dirOf(canonPath(absl::StrCat(dir, "/nix-env"), true))); } } @@ -69,7 +71,7 @@ struct CmdDoctor : StoreCommand { static bool checkProfileRoots(const ref& store) { PathSet dirs; - for (auto& dir : tokenizeString(getEnv("PATH"), ":")) { + for (auto dir : absl::StrSplit(getEnv("PATH"), absl::ByChar(':'))) { Path profileDir = dirOf(dir); try { Path userEnv = canonPath(profileDir, true); @@ -82,7 +84,7 @@ struct CmdDoctor : StoreCommand { } if (profileDir.find("/profiles/") == std::string::npos) { - dirs.insert(dir); + dirs.insert(std::string(dir)); } } } catch (SysError&) { diff --git a/third_party/nix/src/nix/edit.cc b/third_party/nix/src/nix/edit.cc index 330a845dc..4d5cb382b 100644 --- a/third_party/nix/src/nix/edit.cc +++ b/third_party/nix/src/nix/edit.cc @@ -1,3 +1,4 @@ +#include #include #include @@ -54,7 +55,7 @@ struct CmdEdit : InstallableCommand { auto editor = getEnv("EDITOR", "cat"); - auto args = tokenizeString(editor); + Strings args = absl::StrSplit(editor, absl::ByAnyChar(" \t\n\r")); if (editor.find("emacs") != std::string::npos || editor.find("nano") != std::string::npos || diff --git a/third_party/nix/src/nix/run.cc b/third_party/nix/src/nix/run.cc index fa471bd54..47103e8db 100644 --- a/third_party/nix/src/nix/run.cc +++ b/third_party/nix/src/nix/run.cc @@ -1,3 +1,8 @@ +#include + +#include +#include + #include "affinity.hh" #include "command.hh" #include "common-args.hh" @@ -8,12 +13,6 @@ #include "shared.hh" #include "store-api.hh" -#if __linux__ -#include -#endif - -#include - using namespace nix; std::string chrootHelperName = "__run_in_chroot"; @@ -124,7 +123,7 @@ struct CmdRun : InstallablesCommand { todo.push(path); } - auto unixPath = tokenizeString(getEnv("PATH"), ":"); + Strings unixPath = absl::StrSplit(getEnv("PATH"), absl::ByChar(':')); while (!todo.empty()) { Path path = todo.front(); @@ -137,8 +136,9 @@ struct CmdRun : InstallablesCommand { auto propPath = path + "/nix-support/propagated-user-env-packages"; if (accessor->stat(propPath).type == FSAccessor::tRegular) { - for (auto& p : tokenizeString(readFile(propPath))) { - todo.push(p); + for (auto p : + absl::StrSplit(readFile(propPath), absl::ByAnyChar(" \t\n\r"))) { + todo.push(std::string(p)); } } } diff --git a/third_party/nix/src/nix/upgrade-nix.cc b/third_party/nix/src/nix/upgrade-nix.cc index 7098e0eb9..174cc1f1f 100644 --- a/third_party/nix/src/nix/upgrade-nix.cc +++ b/third_party/nix/src/nix/upgrade-nix.cc @@ -1,4 +1,6 @@ #include +#include +#include #include #include "attr-path.hh" @@ -101,8 +103,8 @@ struct CmdUpgradeNix : MixDryRun, StoreCommand { static Path getProfileDir(const ref& store) { Path where; - for (auto& dir : tokenizeString(getEnv("PATH"), ":")) { - if (pathExists(dir + "/nix-env")) { + for (auto& dir : absl::StrSplit(getEnv("PATH"), absl::ByChar(':'))) { + if (pathExists(absl::StrCat(dir, "/nix-env"))) { where = dir; break; }