summaryrefslogtreecommitdiff
path: root/nix
diff options
context:
space:
mode:
authorCongcong Kuo <congcong.kuo@gmail.com>2025-10-20 15:47:25 +0800
committerLudovic Courtès <ludo@gnu.org>2025-11-03 17:18:37 +0100
commit85a72ed28e91f7f918a439c8678254537a374ac7 (patch)
tree38f71ba140d10370014190699e643a1288e20d1c /nix
parent4951783e30036a650f82034ff9c0603d50c081b0 (diff)
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>
Diffstat (limited to 'nix')
-rw-r--r--nix/libstore/build.cc2
-rw-r--r--nix/libstore/builtins.cc2
-rw-r--r--nix/libstore/builtins.hh32
-rw-r--r--nix/libstore/derivations.cc5
-rw-r--r--nix/libstore/gc.cc6
-rw-r--r--nix/libstore/globals.cc2
-rw-r--r--nix/libstore/local-store.cc2
-rw-r--r--nix/libstore/misc.cc1
-rw-r--r--nix/libstore/store-api.cc2
-rw-r--r--nix/libutil/affinity.cc2
-rw-r--r--nix/libutil/archive.cc2
-rw-r--r--nix/libutil/spawn.cc1
-rw-r--r--nix/libutil/util.cc7
-rw-r--r--nix/libutil/util.hh4
14 files changed, 24 insertions, 46 deletions
diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc
index bf2e9150d6a..b3f9c24983c 100644
--- a/nix/libstore/build.cc
+++ b/nix/libstore/build.cc
@@ -1573,7 +1573,7 @@ HookReply DerivationGoal::tryBuildHook()
1573 string reply; 1573 string reply;
1574 while (true) { 1574 while (true) {
1575 string s = readLine(worker.hook->fromAgent.readSide); 1575 string s = readLine(worker.hook->fromAgent.readSide);
1576 if (string(s, 0, 2) == "# ") { 1576 if (s.starts_with("# ")) {
1577 reply = string(s, 2); 1577 reply = string(s, 2);
1578 break; 1578 break;
1579 } 1579 }
diff --git a/nix/libstore/builtins.cc b/nix/libstore/builtins.cc
index b1a32480b55..dc0f68a171a 100644
--- a/nix/libstore/builtins.cc
+++ b/nix/libstore/builtins.cc
@@ -65,7 +65,7 @@ static const std::map<std::string, derivationBuilder> builtins =
65 65
66derivationBuilder lookupBuiltinBuilder(const std::string & name) 66derivationBuilder lookupBuiltinBuilder(const std::string & name)
67{ 67{
68 if (name.substr(0, 8) == "builtin:") 68 if (name.starts_with("builtin:"))
69 { 69 {
70 auto realName = name.substr(8); 70 auto realName = name.substr(8);
71 auto builder = builtins.find(realName); 71 auto builder = builtins.find(realName);
diff --git a/nix/libstore/builtins.hh b/nix/libstore/builtins.hh
index 602a5a1c58d..d5d09e27bc0 100644
--- a/nix/libstore/builtins.hh
+++ b/nix/libstore/builtins.hh
@@ -25,20 +25,20 @@
25 25
26namespace nix { 26namespace nix {
27 27
28 inline bool isBuiltin(const Derivation & drv) 28inline bool isBuiltin(const Derivation & drv)
29 { 29{
30 return string(drv.builder, 0, 8) == "builtin:"; 30 return drv.builder.starts_with("builtin:");
31 } 31}
32 32
33 /* Build DRV, which lives at DRVPATH. */ 33/* Build DRV, which lives at DRVPATH. */
34 typedef void (*derivationBuilder) (const Derivation &drv, 34typedef void (*derivationBuilder) (const Derivation &drv,
35 const std::string &drvPath, 35 const std::string &drvPath,
36 const std::string &output); 36 const std::string &output);
37 37
38 /* Return the built-in builder called BUILDER, or NULL if none was 38/* Return the built-in builder called BUILDER, or NULL if none was
39 found. */ 39 found. */
40 derivationBuilder lookupBuiltinBuilder(const std::string &builder); 40derivationBuilder lookupBuiltinBuilder(const std::string &builder);
41 41
42 /* Return the list of supported built-in builder names. */ 42/* Return the list of supported built-in builder names. */
43 std::list<std::string> builtinBuilderNames(); 43std::list<std::string> builtinBuilderNames();
44} 44}
diff --git a/nix/libstore/derivations.cc b/nix/libstore/derivations.cc
index c253a2a438a..f05296702b3 100644
--- a/nix/libstore/derivations.cc
+++ b/nix/libstore/derivations.cc
@@ -2,7 +2,6 @@
2#include "store-api.hh" 2#include "store-api.hh"
3#include "globals.hh" 3#include "globals.hh"
4#include "util.hh" 4#include "util.hh"
5#include "misc.hh"
6 5
7#include <format> 6#include <format>
8 7
@@ -16,7 +15,7 @@ void DerivationOutput::parseHashInfo(bool & recursive, HashType & hashType, Hash
16 recursive = false; 15 recursive = false;
17 string algo = hashAlgo; 16 string algo = hashAlgo;
18 17
19 if (string(algo, 0, 2) == "r:") { 18 if (algo.starts_with("r:")) {
20 recursive = true; 19 recursive = true;
21 algo = string(algo, 2); 20 algo = string(algo, 2);
22 } 21 }
@@ -200,7 +199,7 @@ string unparseDerivation(const Derivation & drv)
200 199
201bool isDerivation(const string & fileName) 200bool isDerivation(const string & fileName)
202{ 201{
203 return hasSuffix(fileName, drvExtension); 202 return fileName.ends_with(drvExtension);
204} 203}
205 204
206 205
diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc
index 96440077fb5..efb9fd52cbb 100644
--- a/nix/libstore/gc.cc
+++ b/nix/libstore/gc.cc
@@ -2,8 +2,6 @@
2#include "misc.hh" 2#include "misc.hh"
3#include "local-store.hh" 3#include "local-store.hh"
4 4
5#include <functional>
6#include <queue>
7#include <random> 5#include <random>
8#include <algorithm> 6#include <algorithm>
9#include <format> 7#include <format>
@@ -108,7 +106,7 @@ Path addPermRoot(StoreAPI & store, const Path & _storePath,
108 if (!allowOutsideRootsDir) { 106 if (!allowOutsideRootsDir) {
109 Path rootsDir = canonPath(std::format("{}/{}", settings.nixStateDir, gcRootsDir)); 107 Path rootsDir = canonPath(std::format("{}/{}", settings.nixStateDir, gcRootsDir));
110 108
111 if (string(gcRoot, 0, rootsDir.size() + 1) != rootsDir + "/") 109 if (gcRoot.starts_with(rootsDir + "/"))
112 throw Error(std::format( 110 throw Error(std::format(
113 "path `{}' is not a valid garbage collector root; " 111 "path `{}' is not a valid garbage collector root; "
114 "it's not in the directory `{}'", 112 "it's not in the directory `{}'",
@@ -383,7 +381,7 @@ struct LocalStore::GCState
383bool LocalStore::isActiveTempFile(const GCState & state, 381bool LocalStore::isActiveTempFile(const GCState & state,
384 const Path & path, const string & suffix) 382 const Path & path, const string & suffix)
385{ 383{
386 return hasSuffix(path, suffix) 384 return path.ends_with(suffix)
387 && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end(); 385 && state.tempRoots.find(string(path, 0, path.size() - suffix.size())) != state.tempRoots.end();
388} 386}
389 387
diff --git a/nix/libstore/globals.cc b/nix/libstore/globals.cc
index 16f43f6abcb..8551fea56c0 100644
--- a/nix/libstore/globals.cc
+++ b/nix/libstore/globals.cc
@@ -2,10 +2,8 @@
2 2
3#include "globals.hh" 3#include "globals.hh"
4#include "util.hh" 4#include "util.hh"
5#include "archive.hh"
6 5
7#include <map> 6#include <map>
8#include <algorithm>
9#include <format> 7#include <format>
10 8
11namespace nix { 9namespace nix {
diff --git a/nix/libstore/local-store.cc b/nix/libstore/local-store.cc
index f11f48bcf07..161c8d81bda 100644
--- a/nix/libstore/local-store.cc
+++ b/nix/libstore/local-store.cc
@@ -5,9 +5,7 @@
5#include "pathlocks.hh" 5#include "pathlocks.hh"
6#include "worker-protocol.hh" 6#include "worker-protocol.hh"
7#include "derivations.hh" 7#include "derivations.hh"
8#include "affinity.hh"
9 8
10#include <iostream>
11#include <algorithm> 9#include <algorithm>
12#include <format> 10#include <format>
13#include <cstring> 11#include <cstring>
diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc
index 943fb9c9719..88a24d4b822 100644
--- a/nix/libstore/misc.cc
+++ b/nix/libstore/misc.cc
@@ -2,7 +2,6 @@
2#include <math.h> 2#include <math.h>
3#include "store-api.hh" 3#include "store-api.hh"
4#include "local-store.hh" 4#include "local-store.hh"
5#include "globals.hh"
6 5
7#include <format> 6#include <format>
8 7
diff --git a/nix/libstore/store-api.cc b/nix/libstore/store-api.cc
index 0596678b8b5..1067e44a8e2 100644
--- a/nix/libstore/store-api.cc
+++ b/nix/libstore/store-api.cc
@@ -60,7 +60,7 @@ void checkStoreName(const string & name)
60 string validChars = "+-._?="; 60 string validChars = "+-._?=";
61 /* Disallow names starting with a dot for possible security 61 /* Disallow names starting with a dot for possible security
62 reasons (e.g., "." and ".."). */ 62 reasons (e.g., "." and ".."). */
63 if (string(name, 0, 1) == ".") 63 if (name.starts_with("."))
64 throw Error(std::format("invalid name: `{}' (can't begin with dot)", name)); 64 throw Error(std::format("invalid name: `{}' (can't begin with dot)", name));
65 for (const auto& i : name) 65 for (const auto& i : name)
66 if (!((i >= 'A' && i <= 'Z') || 66 if (!((i >= 'A' && i <= 'Z') ||
diff --git a/nix/libutil/affinity.cc b/nix/libutil/affinity.cc
index d50e9f3e9cd..53500c22ab9 100644
--- a/nix/libutil/affinity.cc
+++ b/nix/libutil/affinity.cc
@@ -3,7 +3,7 @@
3#include "affinity.hh" 3#include "affinity.hh"
4 4
5#include <format> 5#include <format>
6 6
7#if HAVE_SCHED_H 7#if HAVE_SCHED_H
8#include <sched.h> 8#include <sched.h>
9#endif 9#endif
diff --git a/nix/libutil/archive.cc b/nix/libutil/archive.cc
index 0df5285860b..fa9f4398e3c 100644
--- a/nix/libutil/archive.cc
+++ b/nix/libutil/archive.cc
@@ -3,8 +3,6 @@
3#include "config.h" 3#include "config.h"
4 4
5#include <cerrno> 5#include <cerrno>
6#include <algorithm>
7#include <vector>
8#include <map> 6#include <map>
9#include <format> 7#include <format>
10 8
diff --git a/nix/libutil/spawn.cc b/nix/libutil/spawn.cc
index c25c3a681f2..02676d65f97 100644
--- a/nix/libutil/spawn.cc
+++ b/nix/libutil/spawn.cc
@@ -29,7 +29,6 @@
29#include <fcntl.h> 29#include <fcntl.h>
30#include <cstring> 30#include <cstring>
31#include <cstdlib> 31#include <cstdlib>
32#include <cstdint>
33#include <cassert> 32#include <cassert>
34#include <format> 33#include <format>
35 34
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc
index ddce9879cac..22022db51a5 100644
--- a/nix/libutil/util.cc
+++ b/nix/libutil/util.cc
@@ -7,7 +7,6 @@
7#include <cerrno> 7#include <cerrno>
8#include <cstdio> 8#include <cstdio>
9#include <cstdlib> 9#include <cstdlib>
10#include <sstream>
11#include <cstring> 10#include <cstring>
12#include <cassert> 11#include <cassert>
13#include <format> 12#include <format>
@@ -1343,12 +1342,6 @@ bool statusOk(int status)
1343} 1342}
1344 1343
1345 1344
1346bool hasSuffix(const string & s, const string & suffix)
1347{
1348 return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix;
1349}
1350
1351
1352void expect(std::istream & str, std::string_view s) 1345void expect(std::istream & str, std::string_view s)
1353{ 1346{
1354 std::vector<char> s2(s.size()); 1347 std::vector<char> s2(s.size());
diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh
index d30dc7801e2..436c378cb6e 100644
--- a/nix/libutil/util.hh
+++ b/nix/libutil/util.hh
@@ -365,10 +365,6 @@ template<class N> bool string2Int(const string & s, N & n)
365} 365}
366 366
367 367
368/* Return true iff `s' ends in `suffix'. */
369bool hasSuffix(const string & s, const string & suffix);
370
371
372/* Read string `s' from stream `str'. */ 368/* Read string `s' from stream `str'. */
373void expect(std::istream & str, std::string_view s); 369void expect(std::istream & str, std::string_view s);
374 370