From 85a72ed28e91f7f918a439c8678254537a374ac7 Mon Sep 17 00:00:00 2001 From: Congcong Kuo Date: Mon, 20 Oct 2025 15:47:25 +0800 Subject: [PATCH] daemon: Use starts_with() and ends_with() instead of string() or hasSuffix() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * nix/libstore/build.cc (DerivationGoal::tryBuildHook): Use starts_with instead of string() * nix/libstore/builtins.cc (lookupBuiltinBuilder): Same. * nix/libstore/builtins.hh (isBuiltin): Same and fix indentation of the file. * nix/libstore/derivations.cc (DerivationOutput::parseHashInfo, isDerivation): Same and clean header files. * nix/libstore/gc.cc (addPermRoot, LocalStore::isActiveTempFile): Same. * nix/libstore/globals.cc: Same. * nix/libstore/local-store.cc: Same. * nix/libstore/misc.cc: Same. * nix/libstore/store-api.cc (checkStoreName): Same. * nix/libutil/affinity.cc: Same. * nix/libutil/archive.cc: Same. * nix/libutil/spawn.cc: Same. * nix/libutil/util.{cc, hh} (hasSuffix): Removed. Signed-off-by: Ludovic Courtès --- nix/libstore/build.cc | 2 +- nix/libstore/builtins.cc | 2 +- nix/libstore/builtins.hh | 32 ++++++++++++++++---------------- nix/libstore/derivations.cc | 5 ++--- nix/libstore/gc.cc | 6 ++---- nix/libstore/globals.cc | 2 -- nix/libstore/local-store.cc | 2 -- nix/libstore/misc.cc | 1 - nix/libstore/store-api.cc | 2 +- nix/libutil/affinity.cc | 2 +- nix/libutil/archive.cc | 2 -- nix/libutil/spawn.cc | 1 - nix/libutil/util.cc | 7 ------- nix/libutil/util.hh | 4 ---- 14 files changed, 24 insertions(+), 46 deletions(-) diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc index bf2e9150d6a25ba6f4015f9a25ae497c632b92f7..b3f9c24983c9cfb902f5d631eff1a70a569377bf 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc @@ -1573,7 +1573,7 @@ HookReply DerivationGoal::tryBuildHook() string reply; while (true) { string s = readLine(worker.hook->fromAgent.readSide); - if (string(s, 0, 2) == "# ") { + if (s.starts_with("# ")) { reply = string(s, 2); break; } diff --git a/nix/libstore/builtins.cc b/nix/libstore/builtins.cc index b1a32480b55141b57d1462d306a413660f470419..dc0f68a171a320f18cf2d1beea8064aba6715686 100644 --- a/nix/libstore/builtins.cc +++ b/nix/libstore/builtins.cc @@ -65,7 +65,7 @@ static const std::map builtins = derivationBuilder lookupBuiltinBuilder(const std::string & name) { - if (name.substr(0, 8) == "builtin:") + if (name.starts_with("builtin:")) { auto realName = name.substr(8); auto builder = builtins.find(realName); diff --git a/nix/libstore/builtins.hh b/nix/libstore/builtins.hh index 602a5a1c58d0aebbc1401e0143647f431d8ac5eb..d5d09e27bc08997d10735bcd116f3959a0340a96 100644 --- a/nix/libstore/builtins.hh +++ b/nix/libstore/builtins.hh @@ -25,20 +25,20 @@ namespace nix { - inline bool isBuiltin(const Derivation & drv) - { - return string(drv.builder, 0, 8) == "builtin:"; - } - - /* Build DRV, which lives at DRVPATH. */ - typedef void (*derivationBuilder) (const Derivation &drv, - const std::string &drvPath, - const std::string &output); - - /* Return the built-in builder called BUILDER, or NULL if none was - found. */ - derivationBuilder lookupBuiltinBuilder(const std::string &builder); - - /* Return the list of supported built-in builder names. */ - std::list builtinBuilderNames(); +inline bool isBuiltin(const Derivation & drv) +{ + return drv.builder.starts_with("builtin:"); +} + +/* Build DRV, which lives at DRVPATH. */ +typedef void (*derivationBuilder) (const Derivation &drv, + const std::string &drvPath, + const std::string &output); + +/* Return the built-in builder called BUILDER, or NULL if none was + found. */ +derivationBuilder lookupBuiltinBuilder(const std::string &builder); + +/* Return the list of supported built-in builder names. */ +std::list builtinBuilderNames(); } diff --git a/nix/libstore/derivations.cc b/nix/libstore/derivations.cc index c253a2a438a4903614c11b6f8f404ba9f27817c2..f05296702b3e0af40e330f770e589d97c8fab6b8 100644 --- a/nix/libstore/derivations.cc +++ b/nix/libstore/derivations.cc @@ -2,7 +2,6 @@ #include "store-api.hh" #include "globals.hh" #include "util.hh" -#include "misc.hh" #include @@ -16,7 +15,7 @@ void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash recursive = false; string algo = hashAlgo; - if (string(algo, 0, 2) == "r:") { + if (algo.starts_with("r:")) { recursive = true; algo = string(algo, 2); } @@ -200,7 +199,7 @@ string unparseDerivation(const Derivation & drv) bool isDerivation(const string & fileName) { - return hasSuffix(fileName, drvExtension); + return fileName.ends_with(drvExtension); } diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc index 96440077fb5be507972095c282604c6ea9c09862..efb9fd52cbbea19ce71e8478759bcbf71f155498 100644 --- a/nix/libstore/gc.cc +++ b/nix/libstore/gc.cc @@ -2,8 +2,6 @@ #include "misc.hh" #include "local-store.hh" -#include -#include #include #include #include @@ -108,7 +106,7 @@ Path addPermRoot(StoreAPI & store, const Path & _storePath, if (!allowOutsideRootsDir) { Path rootsDir = canonPath(std::format("{}/{}", settings.nixStateDir, gcRootsDir)); - if (string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/") + if (gcRoot.starts_with(rootsDir + "/")) throw Error(std::format( "path `{}' is not a valid garbage collector root; " "it's not in the directory `{}'", @@ -383,7 +381,7 @@ struct LocalStore::GCState bool LocalStore::isActiveTempFile(const GCState & state, const Path & path, const string & suffix) { - return hasSuffix(path, suffix) + return path.ends_with(suffix) && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end(); } diff --git a/nix/libstore/globals.cc b/nix/libstore/globals.cc index 16f43f6abcbe6f723c434c7765e2d3c67e146fdb..8551fea56c01417b227adcca89317171d504a53d 100644 --- a/nix/libstore/globals.cc +++ b/nix/libstore/globals.cc @@ -2,10 +2,8 @@ #include "globals.hh" #include "util.hh" -#include "archive.hh" #include -#include #include namespace nix { diff --git a/nix/libstore/local-store.cc b/nix/libstore/local-store.cc index f11f48bcf078d07965593ece2a0d9812d8130131..161c8d81bdae48ff99403b46aa15dea0aad5f293 100644 --- a/nix/libstore/local-store.cc +++ b/nix/libstore/local-store.cc @@ -5,9 +5,7 @@ #include "pathlocks.hh" #include "worker-protocol.hh" #include "derivations.hh" -#include "affinity.hh" -#include #include #include #include diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc index 943fb9c9719e765cf9b3ded8e35f2ae32ebfc806..88a24d4b822473687ef8ea999f36a88f60dce593 100644 --- a/nix/libstore/misc.cc +++ b/nix/libstore/misc.cc @@ -2,7 +2,6 @@ #include #include "store-api.hh" #include "local-store.hh" -#include "globals.hh" #include diff --git a/nix/libstore/store-api.cc b/nix/libstore/store-api.cc index 0596678b8b5fbdb748aa6dc8d62bb503770ead1b..1067e44a8e2b97c736891e1449d4732cc9646c72 100644 --- a/nix/libstore/store-api.cc +++ b/nix/libstore/store-api.cc @@ -60,7 +60,7 @@ void checkStoreName(const string & name) string validChars = "+-._?="; /* Disallow names starting with a dot for possible security reasons (e.g., "." and ".."). */ - if (string(name, 0, 1) == ".") + if (name.starts_with(".")) throw Error(std::format("invalid name: `{}' (can't begin with dot)", name)); for (const auto& i : name) if (!((i >= 'A' && i <= 'Z') || diff --git a/nix/libutil/affinity.cc b/nix/libutil/affinity.cc index d50e9f3e9cdc5c999e1f39bdef9689b6b3b34d1f..53500c22ab90aca8cbf6bb6d8fb75d9801fd36ca 100644 --- a/nix/libutil/affinity.cc +++ b/nix/libutil/affinity.cc @@ -3,7 +3,7 @@ #include "affinity.hh" #include - + #if HAVE_SCHED_H #include #endif diff --git a/nix/libutil/archive.cc b/nix/libutil/archive.cc index 0df5285860be493c55a0f8709b65a23857e4787a..fa9f4398e3c6dfab37720f767d35d35a77f9f504 100644 --- a/nix/libutil/archive.cc +++ b/nix/libutil/archive.cc @@ -3,8 +3,6 @@ #include "config.h" #include -#include -#include #include #include diff --git a/nix/libutil/spawn.cc b/nix/libutil/spawn.cc index c25c3a681f259fd6cb2bbb8f65a5864285989342..02676d65f972ea0fcefb3452659c0874bb5d4d2a 100644 --- a/nix/libutil/spawn.cc +++ b/nix/libutil/spawn.cc @@ -29,7 +29,6 @@ #include #include #include -#include #include #include diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc index ddce9879caccd0feda812a4f2a177d5e52e871e5..22022db51a5c6ff4c106d75d0c7042b98c6b8ad5 100644 --- a/nix/libutil/util.cc +++ b/nix/libutil/util.cc @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -1343,12 +1342,6 @@ bool statusOk(int status) } -bool hasSuffix(const string & s, const string & suffix) -{ - return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix; -} - - void expect(std::istream & str, std::string_view s) { std::vector s2(s.size()); diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh index d30dc7801e271a7b74be8268188b7fe7d5a55e08..436c378cb6ed2adcffc6cb0b7bd3d8ae4bfba51e 100644 --- a/nix/libutil/util.hh +++ b/nix/libutil/util.hh @@ -365,10 +365,6 @@ template bool string2Int(const string & s, N & n) } -/* Return true iff `s' ends in `suffix'. */ -bool hasSuffix(const string & s, const string & suffix); - - /* Read string `s' from stream `str'. */ void expect(std::istream & str, std::string_view s);