aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorJade Lovelace <lix@jade.fyi>2024-03-17 19:14:18 -0700
committerJade Lovelace <lix@jade.fyi>2024-03-17 20:17:19 -0700
commit61e21b25576f7f3491f6a837bf59d8b44c6897a0 (patch)
tree3f62d83b3bab84afcf1011b5c2353226b84313b3 /src/libutil
parent706cee5c493b39e25bdb0add55d2e1771dc31696 (diff)
Delete hasPrefix and hasSuffix from the codebase
These now have equivalents in the standard lib in C++20. This change was performed with a custom clang-tidy check which I will submit later. Executed like so: ninja -C build && run-clang-tidy -checks='-*,nix-*' -load=build/libnix-clang-tidy.so -p .. -fix ../tests | tee -a clang-tidy-result Change-Id: I62679e315ff9e7ce72a40b91b79c3e9fc01b27e9
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/args.cc6
-rw-r--r--src/libutil/cgroup.cc6
-rw-r--r--src/libutil/config.cc2
-rw-r--r--src/libutil/logging.cc2
-rw-r--r--src/libutil/source-path.cc2
-rw-r--r--src/libutil/split.hh2
-rw-r--r--src/libutil/util.cc13
-rw-r--r--src/libutil/util.hh13
8 files changed, 10 insertions, 36 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index 8db293762..02d559540 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -162,7 +162,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
if (auto prefix = needsCompletion(*pos)) {
for (auto & [name, flag] : longFlags) {
if (!hiddenCategories.count(flag->category)
- && hasPrefix(name, std::string(*prefix, 2)))
+ && name.starts_with(std::string(*prefix, 2)))
{
if (auto & f = flag->experimentalFeature)
flagExperimentalFeatures.insert(*f);
@@ -274,7 +274,7 @@ nlohmann::json Args::toJSON()
static void hashTypeCompleter(size_t index, std::string_view prefix)
{
for (auto & type : hashTypes)
- if (hasPrefix(type, prefix))
+ if (type.starts_with(prefix))
completions->add(type);
}
@@ -370,7 +370,7 @@ MultiCommand::MultiCommand(const Commands & commands_)
}},
.completer = {[&](size_t, std::string_view prefix) {
for (auto & [name, command] : commands)
- if (hasPrefix(name, prefix))
+ if (name.starts_with(prefix))
completions->add(name);
}}
});
diff --git a/src/libutil/cgroup.cc b/src/libutil/cgroup.cc
index a008481ca..9320d2371 100644
--- a/src/libutil/cgroup.cc
+++ b/src/libutil/cgroup.cc
@@ -41,7 +41,7 @@ std::map<std::string, std::string> getCgroups(const Path & cgroupFile)
if (!std::regex_match(line, match, regex))
throw Error("invalid line '%s' in '%s'", line, cgroupFile);
- std::string name = hasPrefix(std::string(match[2]), "name=") ? std::string(match[2], 5) : match[2];
+ std::string name = std::string(match[2]).starts_with("name=") ? std::string(match[2], 5) : match[2];
cgroups.insert_or_assign(name, match[3]);
}
@@ -117,13 +117,13 @@ static CgroupStats destroyCgroup(const Path & cgroup, bool returnStats)
if (pathExists(cpustatPath)) {
for (auto & line : tokenizeString<std::vector<std::string>>(readFile(cpustatPath), "\n")) {
std::string_view userPrefix = "user_usec ";
- if (hasPrefix(line, userPrefix)) {
+ if (line.starts_with(userPrefix)) {
auto n = string2Int<uint64_t>(line.substr(userPrefix.size()));
if (n) stats.cpuUser = std::chrono::microseconds(*n);
}
std::string_view systemPrefix = "system_usec ";
- if (hasPrefix(line, systemPrefix)) {
+ if (line.starts_with(systemPrefix)) {
auto n = string2Int<uint64_t>(line.substr(systemPrefix.size()));
if (n) stats.cpuSystem = std::chrono::microseconds(*n);
}
diff --git a/src/libutil/config.cc b/src/libutil/config.cc
index 2535b410f..8e76d6d66 100644
--- a/src/libutil/config.cc
+++ b/src/libutil/config.cc
@@ -18,7 +18,7 @@ bool Config::set(const std::string & name, const std::string & value)
bool append = false;
auto i = _settings.find(name);
if (i == _settings.end()) {
- if (hasPrefix(name, "extra-")) {
+ if (name.starts_with("extra-")) {
i = _settings.find(std::string(name, 6));
if (i == _settings.end() || !i->second.setting->isAppendable())
return false;
diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc
index 7d61c0611..691b77e95 100644
--- a/src/libutil/logging.cc
+++ b/src/libutil/logging.cc
@@ -266,7 +266,7 @@ static Logger::Fields getFields(nlohmann::json & json)
std::optional<nlohmann::json> parseJSONMessage(const std::string & msg)
{
- if (!hasPrefix(msg, "@nix ")) return std::nullopt;
+ if (!msg.starts_with("@nix ")) return std::nullopt;
try {
return nlohmann::json::parse(std::string(msg, 5));
} catch (std::exception & e) {
diff --git a/src/libutil/source-path.cc b/src/libutil/source-path.cc
index 3ccbca06b..e6721f808 100644
--- a/src/libutil/source-path.cc
+++ b/src/libutil/source-path.cc
@@ -83,7 +83,7 @@ SourcePath SourcePath::resolveSymlinks(SymlinkResolution mode) const
throw Error("infinite symlink recursion in path '%s'", path);
auto target = res.readLink();
res.path.pop();
- if (hasPrefix(target, "/"))
+ if (target.starts_with("/"))
res.path = CanonPath::root;
todo.splice(todo.begin(), tokenizeString<std::list<std::string>>(target, "/"));
}
diff --git a/src/libutil/split.hh b/src/libutil/split.hh
index 3b9b2b83b..4ff940eef 100644
--- a/src/libutil/split.hh
+++ b/src/libutil/split.hh
@@ -27,7 +27,7 @@ static inline std::optional<std::string_view> splitPrefixTo(std::string_view & s
}
static inline bool splitPrefix(std::string_view & string, std::string_view prefix) {
- bool res = hasPrefix(string, prefix);
+ bool res = string.starts_with(prefix);
if (res)
string.remove_prefix(prefix.length());
return res;
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 9bb769fc2..3fe96acd2 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1449,19 +1449,6 @@ bool statusOk(int status)
}
-bool hasPrefix(std::string_view s, std::string_view prefix)
-{
- return s.compare(0, prefix.size(), prefix) == 0;
-}
-
-
-bool hasSuffix(std::string_view s, std::string_view suffix)
-{
- return s.size() >= suffix.size()
- && s.substr(s.size() - suffix.size()) == suffix;
-}
-
-
std::string toLower(const std::string & s)
{
std::string r(s);
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index ed4c8705a..f0f9e922b 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -665,19 +665,6 @@ T readLittleEndian(unsigned char * p)
return x;
}
-
-/**
- * @return true iff `s` starts with `prefix`.
- */
-bool hasPrefix(std::string_view s, std::string_view prefix);
-
-
-/**
- * @return true iff `s` ends in `suffix`.
- */
-bool hasSuffix(std::string_view s, std::string_view suffix);
-
-
/**
* Convert a string to lower case.
*/