mirror of
https://github.com/NixOS/nix.git
synced 2025-11-12 13:36:02 +01:00
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.
This commit is contained in:
parent
52ee7ec002
commit
d62a9390fc
33 changed files with 138 additions and 134 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue