From 98299da0fda612b42ab933c47f18163cfef5fa71 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 25 May 2020 01:19:02 +0100 Subject: refactor(3p/nix/libutil): Replace string2Int & trim functions Replaces these functions with corresponding functions from Abseil, namely absl::StripAsciiWhitespace and absl::SimpleAtoi. In the course of doing this some minor things I encountered along the way were also refactored. This also changes the signatures of the various custom readFile functions to use absl::string_view types. --- third_party/nix/src/libstore/profiles.cc | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'third_party/nix/src/libstore/profiles.cc') diff --git a/third_party/nix/src/libstore/profiles.cc b/third_party/nix/src/libstore/profiles.cc index 7c802d754935..3516954b50b7 100644 --- a/third_party/nix/src/libstore/profiles.cc +++ b/third_party/nix/src/libstore/profiles.cc @@ -3,6 +3,9 @@ #include #include +#include +#include +#include #include #include #include @@ -17,22 +20,22 @@ static bool cmpGensByNumber(const Generation& a, const Generation& b) { return a.number < b.number; } -/* Parse a generation name of the format - `--link'. */ -static int parseName(const std::string& profileName, const std::string& name) { - if (std::string(name, 0, profileName.size() + 1) != profileName + "-") { - return -1; - } - std::string s = std::string(name, profileName.size() + 1); - std::string::size_type p = s.find("-link"); - if (p == std::string::npos) { +// Parse a generation out of the format +// `--link'. +static int parseName(absl::string_view profileName, absl::string_view name) { + // Consume the `-' prefix and and `-link' suffix. + if (!(absl::ConsumePrefix(&name, profileName) && + absl::ConsumePrefix(&name, "-") && + absl::ConsumeSuffix(&name, "-link"))) { return -1; } + int n; - if (string2Int(std::string(s, 0, p), n) && n >= 0) { - return n; + if (!absl::SimpleAtoi(name, &n) && n < 0) { + return -1; } - return -1; + + return n; } Generations findGenerations(const Path& profile, int& curGen) { @@ -218,7 +221,7 @@ void deleteGenerationsOlderThan(const Path& profile, std::string strDays = std::string(timeSpec, 0, timeSpec.size() - 1); int days; - if (!string2Int(strDays, days) || days < 1) { + if (!absl::SimpleAtoi(strDays, &days) || days < 1) { throw Error(format("invalid number of days specifier '%1%'") % timeSpec); } -- cgit 1.4.1