aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.hh
diff options
context:
space:
mode:
authorpennae <github@quasiparticle.net>2022-01-12 16:02:29 +0100
committerpennae <github@quasiparticle.net>2022-01-13 13:51:29 +0100
commit44c92a1667ce829518178a77a9de0a53609d284f (patch)
tree9405bda15c74a4341f9a8c79d92a990d57598f9c /src/libutil/util.hh
parente61c4bc25a876ce303022435342748cafeced010 (diff)
use more string_view in utils
there's a couple places that can be easily converted from using strings to using string_views instead. gives a slight (~1%) boost to system eval. # before nix eval --raw --impure --expr 'with import <nixpkgs/nixos> {}; system' Time (mean ± σ): 2.946 s ± 0.026 s [User: 2.655 s, System: 0.209 s] Range (min … max): 2.905 s … 2.995 s 20 runs # after Time (mean ± σ): 2.928 s ± 0.024 s [User: 2.638 s, System: 0.211 s] Range (min … max): 2.893 s … 2.970 s 20 runs
Diffstat (limited to 'src/libutil/util.hh')
-rw-r--r--src/libutil/util.hh18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index c900033f8..369c44f78 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -56,7 +56,7 @@ Path absPath(Path path,
double or trailing slashes. Optionally resolves all symlink
components such that each component of the resulting path is *not*
a symbolic link. */
-Path canonPath(const Path & path, bool resolveSymlinks = false);
+Path canonPath(PathView path, bool resolveSymlinks = false);
/* Return the directory part of the given canonical path, i.e.,
everything before the final `/'. If the path is the root or an
@@ -368,15 +368,19 @@ MakeError(FormatError, Error);
/* String tokenizer. */
-template<class C> C tokenizeString(std::string_view s, const string & separators = " \t\n\r");
+template<class C> C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r");
/* Concatenate the given strings with a separator between the
elements. */
template<class C>
-string concatStringsSep(const string & sep, const C & ss)
+string concatStringsSep(const std::string_view sep, const C & ss)
{
+ size_t size = 0;
+ // need a cast to string_view since this is also called with Symbols
+ for (const auto & s : ss) size += sep.size() + std::string_view(s).size();
string s;
+ s.reserve(size);
for (auto & i : ss) {
if (s.size() != 0) s += sep;
s += i;
@@ -384,6 +388,14 @@ string concatStringsSep(const string & sep, const C & ss)
return s;
}
+template<class ... Parts>
+auto concatStrings(Parts && ... parts)
+ -> std::enable_if_t<(... && std::is_convertible_v<Parts, std::string_view>), string>
+{
+ std::string_view views[sizeof...(parts)] = { parts... };
+ return concatStringsSep({}, views);
+}
+
/* Add quotes around a collection of strings. */
template<class C> Strings quoteStrings(const C & c)