2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 02:49:00 +00:00

Add Buffer constructors taking std::string parameters

This commit is contained in:
m-holger 2023-11-17 18:06:57 +00:00
parent 4c71c8680a
commit 9f7f9496ed
4 changed files with 85 additions and 7 deletions

View File

@ -24,6 +24,7 @@
#include <cstddef>
#include <memory>
#include <string>
class Buffer
{
@ -35,11 +36,15 @@ class Buffer
// object is destroyed.
QPDF_DLL
Buffer(size_t size);
QPDF_DLL
Buffer(std::string&& content);
// Create a Buffer object whose memory is owned by the caller and will not be freed when the
// Buffer is destroyed.
QPDF_DLL
Buffer(unsigned char* buf, size_t size);
QPDF_DLL
Buffer(std::string& content);
[[deprecated("Move Buffer or use Buffer::copy instead")]] QPDF_DLL Buffer(Buffer const&);
[[deprecated("Move Buffer or use Buffer::copy instead")]] QPDF_DLL Buffer&
@ -75,8 +80,10 @@ class Buffer
private:
Members(size_t size, unsigned char* buf, bool own_memory);
Members(std::string&& content);
Members(Members const&) = delete;
std::string str;
bool own_memory;
size_t size;
unsigned char* buf;

View File

@ -27,6 +27,14 @@ Buffer::Members::Members(size_t size, unsigned char* buf, bool own_memory) :
}
}
Buffer::Members::Members(std::string&& content) :
str(std::move(content)),
own_memory(false),
size(str.size()),
buf(reinterpret_cast<unsigned char*>(str.data()))
{
}
Buffer::Members::~Members()
{
if (this->own_memory) {
@ -44,11 +52,21 @@ Buffer::Buffer(size_t size) :
{
}
Buffer::Buffer(std::string&& content) :
m(new Members(std::move(content)))
{
}
Buffer::Buffer(unsigned char* buf, size_t size) :
m(new Members(size, buf, false))
{
}
Buffer::Buffer(std::string& content) :
m(new Members(content.size(), reinterpret_cast<unsigned char*>(content.data()), false))
{
}
Buffer::Buffer(Buffer const& rhs)
{
assert(test_mode);

View File

@ -42,13 +42,7 @@ Pl_Buffer::getBuffer()
if (!m->ready) {
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
}
auto size = m->data.size();
auto* b = new Buffer(size);
if (size > 0) {
unsigned char* p = b->getBuffer();
memcpy(p, m->data.data(), size);
}
auto* b = new Buffer(std::move(m->data));
m->data.clear();
return b;
}

View File

@ -35,6 +35,21 @@ main()
assert(bc2p != bc1p);
assert(bc2p[0] == 'R');
assert(bc2p[1] == 'W');
// Test Buffer(std:string&&)
Buffer bc3("QW");
unsigned char* bc3p = bc3.getBuffer();
Buffer bc4(bc3.copy());
bc3p[0] = 'R';
unsigned char* bc4p = bc4.getBuffer();
assert(bc4p != bc3p);
assert(bc4p[0] == 'Q');
assert(bc4p[1] == 'W');
bc4 = bc3.copy();
bc4p = bc4.getBuffer();
assert(bc4p != bc3p);
assert(bc4p[0] == 'R');
assert(bc4p[1] == 'W');
}
#ifdef _MSC_VER
@ -62,6 +77,37 @@ main()
assert(bc2p != bc1p);
assert(bc2p[0] == 'R');
assert(bc2p[1] == 'W');
// Test Buffer(std:string&&)
Buffer bc3("QW");
unsigned char* bc3p = bc3.getBuffer();
Buffer bc4(bc3);
bc3p[0] = 'R';
unsigned char* bc4p = bc4.getBuffer();
assert(bc4p != bc3p);
assert(bc4p[0] == 'Q');
assert(bc4p[1] == 'W');
bc4 = bc3;
bc4p = bc4.getBuffer();
assert(bc4p != bc3p);
assert(bc2p[0] == 'R');
assert(bc2p[1] == 'W');
// Test Buffer(std:string&)
std::string s{"QW"};
Buffer bc5(s);
unsigned char* bc5p = bc5.getBuffer();
Buffer bc6(bc5);
bc5p[0] = 'R';
unsigned char* bc6p = bc6.getBuffer();
assert(bc6p != bc5p);
assert(bc6p[0] == 'Q');
assert(bc6p[1] == 'W');
bc6 = bc5;
bc6p = bc6.getBuffer();
assert(bc6p != bc5p);
assert(bc2p[0] == 'R');
assert(bc2p[1] == 'W');
}
#if (defined(__GNUC__) || defined(__clang__))
# pragma GCC diagnostic pop
@ -82,6 +128,19 @@ main()
Buffer bm3 = std::move(bm2);
unsigned char* bm3p = bm3.getBuffer();
assert(bm3p == bm2p);
// Test Buffer(dtd::string&&)
Buffer bm4("QW");
unsigned char* bm4p = bm4.getBuffer();
Buffer bm5(std::move(bm4));
bm4p[0] = 'R';
unsigned char* bm5p = bm5.getBuffer();
assert(bm5p == bm4p);
assert(bm5p[0] == 'R');
Buffer bm6 = std::move(bm5);
unsigned char* bm6p = bm6.getBuffer();
assert(bm6p == bm5p);
}
try {