about summary refs log tree commit diff
path: root/third_party/nix/src/libutil
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-25T00·19+0100
committerVincent Ambo <tazjin@google.com>2020-05-25T00·19+0100
commit98299da0fda612b42ab933c47f18163cfef5fa71 (patch)
tree78fc0f1127b5f2336d3e370f342f72f38d5a5ee2 /third_party/nix/src/libutil
parentb371821db59d33851d521d66ba1fb126d388c00f (diff)
refactor(3p/nix/libutil): Replace string2Int & trim functions r/843
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.
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r--third_party/nix/src/libutil/args.hh4
-rw-r--r--third_party/nix/src/libutil/config.cc3
-rw-r--r--third_party/nix/src/libutil/meson.build2
-rw-r--r--third_party/nix/src/libutil/util.cc19
-rw-r--r--third_party/nix/src/libutil/util.hh19
5 files changed, 15 insertions, 32 deletions
diff --git a/third_party/nix/src/libutil/args.hh b/third_party/nix/src/libutil/args.hh
index 20a6379fecba..2c352a04368c 100644
--- a/third_party/nix/src/libutil/args.hh
+++ b/third_party/nix/src/libutil/args.hh
@@ -4,6 +4,8 @@
 #include <map>
 #include <memory>
 
+#include <absl/strings/numbers.h>
+
 #include "util.hh"
 
 namespace nix {
@@ -179,7 +181,7 @@ class Args {
         .arity(1)
         .handler([=](std::vector<std::string> ss) {
           I n;
-          if (!string2Int(ss[0], n))
+          if (!absl::SimpleAtoi(ss[0], &n))
             throw UsageError("flag '--%s' requires a integer argument",
                              longName);
           fun(n);
diff --git a/third_party/nix/src/libutil/config.cc b/third_party/nix/src/libutil/config.cc
index c7c952abf16f..bc81ce77ceb5 100644
--- a/third_party/nix/src/libutil/config.cc
+++ b/third_party/nix/src/libutil/config.cc
@@ -3,6 +3,7 @@
 
 #include <utility>
 
+#include <absl/strings/numbers.h>
 #include <glog/logging.h>
 
 #include "args.hh"
@@ -214,7 +215,7 @@ std::string BaseSetting<std::string>::to_string() {
 template <typename T>
 void BaseSetting<T>::set(const std::string& str) {
   static_assert(std::is_integral<T>::value, "Integer required.");
-  if (!string2Int(str, value)) {
+  if (!absl::SimpleAtoi(str, &value)) {
     throw UsageError("setting '%s' has invalid value '%s'", name, str);
   }
 }
diff --git a/third_party/nix/src/libutil/meson.build b/third_party/nix/src/libutil/meson.build
index a6494e6eac7c..50043a8fad57 100644
--- a/third_party/nix/src/libutil/meson.build
+++ b/third_party/nix/src/libutil/meson.build
@@ -47,7 +47,7 @@ libutil_dep_list = [
   openssl_dep,
   pthread_dep,
   libsodium_dep,
-]
+] + absl_deps
 
 libutil_link_list = []
 libutil_link_args = []
diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc
index 696a8ff2365e..8dca97af98a0 100644
--- a/third_party/nix/src/libutil/util.cc
+++ b/third_party/nix/src/libutil/util.cc
@@ -12,6 +12,7 @@
 #include <thread>
 #include <utility>
 
+#include <absl/strings/string_view.h>
 #include <fcntl.h>
 #include <grp.h>
 #include <pwd.h>
@@ -306,16 +307,17 @@ std::string readFile(int fd) {
   return std::string((char*)buf.data(), st.st_size);
 }
 
-std::string readFile(const Path& path, bool drain) {
-  AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
+std::string readFile(absl::string_view path, bool drain) {
+  AutoCloseFD fd = open(std::string(path).c_str(), O_RDONLY | O_CLOEXEC);
   if (!fd) {
     throw SysError(format("opening file '%1%'") % path);
   }
   return drain ? drainFD(fd.get()) : readFile(fd.get());
 }
 
-void readFile(const Path& path, Sink& sink) {
-  AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
+void readFile(absl::string_view path, Sink& sink) {
+  // TODO(tazjin): use stdlib functions for this stuff
+  AutoCloseFD fd = open(std::string(path).c_str(), O_RDONLY | O_CLOEXEC);
   if (!fd) {
     throw SysError("opening file '%s'", path);
   }
@@ -1213,15 +1215,6 @@ std::string concatStringsSep(const std::string& sep, const StringSet& ss) {
   return s;
 }
 
-std::string trim(const std::string& s, const std::string& whitespace) {
-  auto i = s.find_first_not_of(whitespace);
-  if (i == std::string::npos) {
-    return "";
-  }
-  auto j = s.find_last_not_of(whitespace);
-  return std::string(s, i, j == std::string::npos ? j : j - i + 1);
-}
-
 std::string replaceStrings(const std::string& s, const std::string& from,
                            const std::string& to) {
   if (from.empty()) {
diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh
index 7d10df50bf5b..0f3752dfde5a 100644
--- a/third_party/nix/src/libutil/util.hh
+++ b/third_party/nix/src/libutil/util.hh
@@ -8,6 +8,7 @@
 #include <optional>
 #include <sstream>
 
+#include <absl/strings/string_view.h>
 #include <dirent.h>
 #include <signal.h>
 #include <sys/stat.h>
@@ -97,8 +98,8 @@ unsigned char getFileType(const Path& path);
 
 /* Read the contents of a file into a string. */
 std::string readFile(int fd);
-std::string readFile(const Path& path, bool drain = false);
-void readFile(const Path& path, Sink& sink);
+std::string readFile(absl::string_view path, bool drain = false);
+void readFile(absl::string_view path, Sink& sink);
 
 /* Write a string to a file. */
 void writeFile(const Path& path, const std::string& s, mode_t mode = 0666);
@@ -325,10 +326,6 @@ C tokenizeString(const std::string& s,
 std::string concatStringsSep(const std::string& sep, const Strings& ss);
 std::string concatStringsSep(const std::string& sep, const StringSet& ss);
 
-/* Remove whitespace from the start and end of a string. */
-std::string trim(const std::string& s,
-                 const std::string& whitespace = " \n\r\t");
-
 /* Replace all occurrences of a string inside another string. */
 std::string replaceStrings(const std::string& s, const std::string& from,
                            const std::string& to);
@@ -339,16 +336,6 @@ std::string statusToString(int status);
 
 bool statusOk(int status);
 
-/* Parse a string into an integer. */
-template <class N>
-bool string2Int(const std::string& s, N& n) {
-  if (std::string(s, 0, 1) == "-" && !std::numeric_limits<N>::is_signed)
-    return false;
-  std::istringstream str(s);
-  str >> n;
-  return str && str.get() == EOF;
-}
-
 /* Parse a string into a float. */
 template <class N>
 bool string2Float(const std::string& s, N& n) {