aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.cc
diff options
context:
space:
mode:
authorpennae <github@quasiparticle.net>2022-01-21 17:55:51 +0100
committerpennae <github@quasiparticle.net>2022-01-27 17:15:43 +0100
commit0d7fae6a574ec1b6758a7e6d8e639145c1c465a9 (patch)
treefda5ab069729b9a078ca8e220d42d56515c988ec /src/libutil/util.cc
parent558c4ee3e370c9f9a6ea293df54ed6914a999f1c (diff)
convert a for more utilities to string_view
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r--src/libutil/util.cc20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 1f1f2c861..692bcb180 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -81,7 +81,7 @@ void replaceEnv(std::map<std::string, std::string> newEnv)
}
-Path absPath(Path path, std::optional<Path> dir, bool resolveSymlinks)
+Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
{
if (path[0] != '/') {
if (!dir) {
@@ -95,12 +95,12 @@ Path absPath(Path path, std::optional<Path> dir, bool resolveSymlinks)
if (!getcwd(buf, sizeof(buf)))
#endif
throw SysError("cannot get cwd");
- dir = buf;
+ path = concatStrings(buf, "/", path);
#ifdef __GNU__
free(buf);
#endif
- }
- path = *dir + "/" + path;
+ } else
+ path = concatStrings(*dir, "/", path);
}
return canonPath(path, resolveSymlinks);
}
@@ -172,7 +172,7 @@ Path canonPath(PathView path, bool resolveSymlinks)
}
-Path dirOf(const Path & path)
+Path dirOf(const PathView path)
{
Path::size_type pos = path.rfind('/');
if (pos == string::npos)
@@ -1344,9 +1344,11 @@ std::string toLower(const std::string & s)
}
-std::string shellEscape(const std::string & s)
+std::string shellEscape(const std::string_view s)
{
- std::string r = "'";
+ std::string r;
+ r.reserve(s.size() + 2);
+ r += "'";
for (auto & i : s)
if (i == '\'') r += "'\\''"; else r += i;
r += '\'';
@@ -1751,7 +1753,7 @@ void bind(int fd, const std::string & path)
if (path.size() + 1 >= sizeof(addr.sun_path)) {
Pid pid = startProcess([&]() {
- auto dir = dirOf(path);
+ Path dir = dirOf(path);
if (chdir(dir.c_str()) == -1)
throw SysError("chdir to '%s' failed", dir);
std::string base(baseNameOf(path));
@@ -1780,7 +1782,7 @@ void connect(int fd, const std::string & path)
if (path.size() + 1 >= sizeof(addr.sun_path)) {
Pid pid = startProcess([&]() {
- auto dir = dirOf(path);
+ Path dir = dirOf(path);
if (chdir(dir.c_str()) == -1)
throw SysError("chdir to '%s' failed", dir);
std::string base(baseNameOf(path));