1
0
mirror of https://git.savannah.gnu.org/git/guix.git synced 2026-05-28 12:01:49 +02:00

daemon: Use starts_with() and ends_with() instead of string() or hasSuffix()

* 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 <ludo@gnu.org>
This commit is contained in:
Congcong Kuo
2025-10-20 15:47:25 +08:00
committed by Ludovic Courtès
parent 4951783e30
commit 85a72ed28e
14 changed files with 24 additions and 46 deletions
+1 -1
View File
@@ -1573,7 +1573,7 @@ HookReply DerivationGoal::tryBuildHook()
string reply; string reply;
while (true) { while (true) {
string s = readLine(worker.hook->fromAgent.readSide); string s = readLine(worker.hook->fromAgent.readSide);
if (string(s, 0, 2) == "# ") { if (s.starts_with("# ")) {
reply = string(s, 2); reply = string(s, 2);
break; break;
} }
+1 -1
View File
@@ -65,7 +65,7 @@ static const std::map<std::string, derivationBuilder> builtins =
derivationBuilder lookupBuiltinBuilder(const std::string & name) derivationBuilder lookupBuiltinBuilder(const std::string & name)
{ {
if (name.substr(0, 8) == "builtin:") if (name.starts_with("builtin:"))
{ {
auto realName = name.substr(8); auto realName = name.substr(8);
auto builder = builtins.find(realName); auto builder = builtins.find(realName);
+16 -16
View File
@@ -25,20 +25,20 @@
namespace nix { namespace nix {
inline bool isBuiltin(const Derivation & drv) inline bool isBuiltin(const Derivation & drv)
{ {
return string(drv.builder, 0, 8) == "builtin:"; return drv.builder.starts_with("builtin:");
} }
/* Build DRV, which lives at DRVPATH. */ /* Build DRV, which lives at DRVPATH. */
typedef void (*derivationBuilder) (const Derivation &drv, typedef void (*derivationBuilder) (const Derivation &drv,
const std::string &drvPath, const std::string &drvPath,
const std::string &output); const std::string &output);
/* Return the built-in builder called BUILDER, or NULL if none was /* Return the built-in builder called BUILDER, or NULL if none was
found. */ found. */
derivationBuilder lookupBuiltinBuilder(const std::string &builder); derivationBuilder lookupBuiltinBuilder(const std::string &builder);
/* Return the list of supported built-in builder names. */ /* Return the list of supported built-in builder names. */
std::list<std::string> builtinBuilderNames(); std::list<std::string> builtinBuilderNames();
} }
+2 -3
View File
@@ -2,7 +2,6 @@
#include "store-api.hh" #include "store-api.hh"
#include "globals.hh" #include "globals.hh"
#include "util.hh" #include "util.hh"
#include "misc.hh"
#include <format> #include <format>
@@ -16,7 +15,7 @@ void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash
recursive = false; recursive = false;
string algo = hashAlgo; string algo = hashAlgo;
if (string(algo, 0, 2) == "r:") { if (algo.starts_with("r:")) {
recursive = true; recursive = true;
algo = string(algo, 2); algo = string(algo, 2);
} }
@@ -200,7 +199,7 @@ string unparseDerivation(const Derivation & drv)
bool isDerivation(const string & fileName) bool isDerivation(const string & fileName)
{ {
return hasSuffix(fileName, drvExtension); return fileName.ends_with(drvExtension);
} }
+2 -4
View File
@@ -2,8 +2,6 @@
#include "misc.hh" #include "misc.hh"
#include "local-store.hh" #include "local-store.hh"
#include <functional>
#include <queue>
#include <random> #include <random>
#include <algorithm> #include <algorithm>
#include <format> #include <format>
@@ -108,7 +106,7 @@ Path addPermRoot(StoreAPI & store, const Path & _storePath,
if (!allowOutsideRootsDir) { if (!allowOutsideRootsDir) {
Path rootsDir = canonPath(std::format("{}/{}", settings.nixStateDir, gcRootsDir)); 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( throw Error(std::format(
"path `{}' is not a valid garbage collector root; " "path `{}' is not a valid garbage collector root; "
"it's not in the directory `{}'", "it's not in the directory `{}'",
@@ -383,7 +381,7 @@ struct LocalStore::GCState
bool LocalStore::isActiveTempFile(const GCState & state, bool LocalStore::isActiveTempFile(const GCState & state,
const Path & path, const string & suffix) 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(); && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end();
} }
-2
View File
@@ -2,10 +2,8 @@
#include "globals.hh" #include "globals.hh"
#include "util.hh" #include "util.hh"
#include "archive.hh"
#include <map> #include <map>
#include <algorithm>
#include <format> #include <format>
namespace nix { namespace nix {
-2
View File
@@ -5,9 +5,7 @@
#include "pathlocks.hh" #include "pathlocks.hh"
#include "worker-protocol.hh" #include "worker-protocol.hh"
#include "derivations.hh" #include "derivations.hh"
#include "affinity.hh"
#include <iostream>
#include <algorithm> #include <algorithm>
#include <format> #include <format>
#include <cstring> #include <cstring>
-1
View File
@@ -2,7 +2,6 @@
#include <math.h> #include <math.h>
#include "store-api.hh" #include "store-api.hh"
#include "local-store.hh" #include "local-store.hh"
#include "globals.hh"
#include <format> #include <format>
+1 -1
View File
@@ -60,7 +60,7 @@ void checkStoreName(const string & name)
string validChars = "+-._?="; string validChars = "+-._?=";
/* Disallow names starting with a dot for possible security /* Disallow names starting with a dot for possible security
reasons (e.g., "." and ".."). */ 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)); throw Error(std::format("invalid name: `{}' (can't begin with dot)", name));
for (const auto& i : name) for (const auto& i : name)
if (!((i >= 'A' && i <= 'Z') || if (!((i >= 'A' && i <= 'Z') ||
+1 -1
View File
@@ -3,7 +3,7 @@
#include "affinity.hh" #include "affinity.hh"
#include <format> #include <format>
#if HAVE_SCHED_H #if HAVE_SCHED_H
#include <sched.h> #include <sched.h>
#endif #endif
-2
View File
@@ -3,8 +3,6 @@
#include "config.h" #include "config.h"
#include <cerrno> #include <cerrno>
#include <algorithm>
#include <vector>
#include <map> #include <map>
#include <format> #include <format>
-1
View File
@@ -29,7 +29,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <cstdint>
#include <cassert> #include <cassert>
#include <format> #include <format>
-7
View File
@@ -7,7 +7,6 @@
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <sstream>
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <format> #include <format>
@@ -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) void expect(std::istream & str, std::string_view s)
{ {
std::vector<char> s2(s.size()); std::vector<char> s2(s.size());
-4
View File
@@ -365,10 +365,6 @@ template<class N> 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'. */ /* Read string `s' from stream `str'. */
void expect(std::istream & str, std::string_view s); void expect(std::istream & str, std::string_view s);