aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-01-17 22:20:05 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-01-18 11:12:30 +0100
commitd62a9390fcdc0f9e4971e5fab2f667567237b252 (patch)
treecdbf6aef8b15b2ba5f02f7426ec2e5cd1f595e39 /src/libutil
parent52ee7ec0028263f04e15c05256ca90fa62a0da66 (diff)
Get rid of std::shared_ptr<std::string> and ref<std::string>
These were needed back in the pre-C++11 era because we didn't have move semantics. But now we do.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/compression.cc8
-rw-r--r--src/libutil/compression.hh4
-rw-r--r--src/libutil/serialise.cc6
-rw-r--r--src/libutil/serialise.hh11
-rw-r--r--src/libutil/tests/compression.cc28
-rw-r--r--src/libutil/util.cc4
6 files changed, 31 insertions, 30 deletions
diff --git a/src/libutil/compression.cc b/src/libutil/compression.cc
index f80ca664c..89180e7a7 100644
--- a/src/libutil/compression.cc
+++ b/src/libutil/compression.cc
@@ -190,13 +190,13 @@ struct BrotliDecompressionSink : ChunkedCompressionSink
}
};
-ref<std::string> decompress(const std::string & method, const std::string & in)
+std::string decompress(const std::string & method, std::string_view in)
{
StringSink ssink;
auto sink = makeDecompressionSink(method, ssink);
(*sink)(in);
sink->finish();
- return ssink.s;
+ return std::move(ssink.s);
}
std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink)
@@ -281,13 +281,13 @@ ref<CompressionSink> makeCompressionSink(const std::string & method, Sink & next
throw UnknownCompressionMethod("unknown compression method '%s'", method);
}
-ref<std::string> compress(const std::string & method, const std::string & in, const bool parallel, int level)
+std::string compress(const std::string & method, std::string_view in, const bool parallel, int level)
{
StringSink ssink;
auto sink = makeCompressionSink(method, ssink, parallel, level);
(*sink)(in);
sink->finish();
- return ssink.s;
+ return std::move(ssink.s);
}
}
diff --git a/src/libutil/compression.hh b/src/libutil/compression.hh
index 9b1e4a9d4..c470b82a5 100644
--- a/src/libutil/compression.hh
+++ b/src/libutil/compression.hh
@@ -15,11 +15,11 @@ struct CompressionSink : BufferedSink, FinishSink
using FinishSink::finish;
};
-ref<std::string> decompress(const std::string & method, const std::string & in);
+std::string decompress(const std::string & method, std::string_view in);
std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink);
-ref<std::string> compress(const std::string & method, const std::string & in, const bool parallel = false, int level = -1);
+std::string compress(const std::string & method, std::string_view in, const bool parallel = false, int level = -1);
ref<CompressionSink> makeCompressionSink(const std::string & method, Sink & nextSink, const bool parallel = false, int level = -1);
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index 704359b4a..fe703de06 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -110,7 +110,7 @@ std::string Source::drain()
{
StringSink s;
drainInto(s);
- return *s.s;
+ return std::move(s.s);
}
@@ -450,11 +450,11 @@ Error readError(Source & source)
void StringSink::operator () (std::string_view data)
{
static bool warned = false;
- if (!warned && s->size() > threshold) {
+ if (!warned && s.size() > threshold) {
warnLargeDump();
warned = true;
}
- s->append(data);
+ s.append(data);
}
size_t ChainSource::read(char * data, size_t len)
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index e80fb9ecf..fdd35aa00 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -154,12 +154,13 @@ private:
/* A sink that writes data to a string. */
struct StringSink : Sink
{
- ref<std::string> s;
- StringSink() : s(make_ref<std::string>()) { };
- explicit StringSink(const size_t reservedSize) : s(make_ref<std::string>()) {
- s->reserve(reservedSize);
+ std::string s;
+ StringSink() { }
+ explicit StringSink(const size_t reservedSize)
+ {
+ s.reserve(reservedSize);
};
- StringSink(ref<std::string> s) : s(s) { };
+ StringSink(std::string && s) : s(std::move(s)) { };
void operator () (std::string_view data) override;
};
diff --git a/src/libutil/tests/compression.cc b/src/libutil/tests/compression.cc
index 2efa3266b..bbbf3500f 100644
--- a/src/libutil/tests/compression.cc
+++ b/src/libutil/tests/compression.cc
@@ -12,17 +12,17 @@ namespace nix {
}
TEST(compress, noneMethodDoesNothingToTheInput) {
- ref<std::string> o = compress("none", "this-is-a-test");
+ auto o = compress("none", "this-is-a-test");
- ASSERT_EQ(*o, "this-is-a-test");
+ ASSERT_EQ(o, "this-is-a-test");
}
TEST(decompress, decompressNoneCompressed) {
auto method = "none";
auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
- ref<std::string> o = decompress(method, str);
+ auto o = decompress(method, str);
- ASSERT_EQ(*o, str);
+ ASSERT_EQ(o, str);
}
TEST(decompress, decompressEmptyCompressed) {
@@ -30,33 +30,33 @@ namespace nix {
// (Content-Encoding == "").
auto method = "";
auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
- ref<std::string> o = decompress(method, str);
+ auto o = decompress(method, str);
- ASSERT_EQ(*o, str);
+ ASSERT_EQ(o, str);
}
TEST(decompress, decompressXzCompressed) {
auto method = "xz";
auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
- ref<std::string> o = decompress(method, *compress(method, str));
+ auto o = decompress(method, compress(method, str));
- ASSERT_EQ(*o, str);
+ ASSERT_EQ(o, str);
}
TEST(decompress, decompressBzip2Compressed) {
auto method = "bzip2";
auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
- ref<std::string> o = decompress(method, *compress(method, str));
+ auto o = decompress(method, compress(method, str));
- ASSERT_EQ(*o, str);
+ ASSERT_EQ(o, str);
}
TEST(decompress, decompressBrCompressed) {
auto method = "br";
auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf";
- ref<std::string> o = decompress(method, *compress(method, str));
+ auto o = decompress(method, compress(method, str));
- ASSERT_EQ(*o, str);
+ ASSERT_EQ(o, str);
}
TEST(decompress, decompressInvalidInputThrowsCompressionError) {
@@ -77,7 +77,7 @@ namespace nix {
(*sink)(inputString);
sink->finish();
- ASSERT_STREQ((*strSink.s).c_str(), inputString);
+ ASSERT_STREQ(strSink.s.c_str(), inputString);
}
TEST(makeCompressionSink, compressAndDecompress) {
@@ -90,7 +90,7 @@ namespace nix {
sink->finish();
decompressionSink->finish();
- ASSERT_STREQ((*strSink.s).c_str(), inputString);
+ ASSERT_STREQ(strSink.s.c_str(), inputString);
}
}
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 9edd69c64..f43161e33 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -670,7 +670,7 @@ string drainFD(int fd, bool block, const size_t reserveSize)
{
StringSink sink(reserveSize);
drainFD(fd, sink, block);
- return std::move(*sink.s);
+ return std::move(sink.s);
}
@@ -1055,7 +1055,7 @@ std::pair<int, std::string> runProgram(RunOptions && options)
status = e.status;
}
- return {status, std::move(*sink.s)};
+ return {status, std::move(sink.s)};
}
void runProgram2(const RunOptions & options)