diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx index 45df53c61c5f..72ed59edadc5 100644 --- a/include/tools/json_writer.hxx +++ b/include/tools/json_writer.hxx @@ -17,6 +17,7 @@ #include #include +#include /** Simple JSON encoder designed specifically for LibreOfficeKit purposes. * @@ -73,7 +74,7 @@ public: /** Hands ownership of the underlying storage buffer to the caller, * after this no more document modifications may be written. */ - char* extractData(); + char* extractData() { return extractDataImpl().first; } OString extractAsOString(); std::string extractAsStdString(); @@ -85,17 +86,9 @@ private: void endArray(); void endStruct(); void addCommaBeforeField(); - void reallocBuffer(int noMoreBytesRequired); void writeEscapedOUString(const OUString& rPropVal); - - // this part inline to speed up the fast path - inline void ensureSpace(int noMoreBytesRequired) - { - assert(mpBuffer && "already extracted data"); - int currentUsed = mPos - mpBuffer; - if (currentUsed + noMoreBytesRequired >= mSpaceAllocated) - reallocBuffer(noMoreBytesRequired); - } + std::pair extractDataImpl(); + void ensureSpace(int noMoreBytesRequired); }; /** diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx index 7024b580c7fd..d6e34179f930 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -388,18 +388,22 @@ void JsonWriter::addCommaBeforeField() } } -void JsonWriter::reallocBuffer(int noMoreBytesRequired) +void JsonWriter::ensureSpace(int noMoreBytesRequired) { + assert(mpBuffer && "already extracted data"); int currentUsed = mPos - mpBuffer; - auto newSize = std::max(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2); - mpBuffer = static_cast(realloc(mpBuffer, newSize)); - mPos = mpBuffer + currentUsed; - mSpaceAllocated = newSize; + if (currentUsed + noMoreBytesRequired >= mSpaceAllocated) + { + auto newSize = (currentUsed + noMoreBytesRequired) * 2; + mpBuffer = static_cast(realloc(mpBuffer, newSize)); + mPos = mpBuffer + currentUsed; + mSpaceAllocated = newSize; + } } /** Hands ownership of the underlying storage buffer to the caller, * after this no more document modifications may be written. */ -char* JsonWriter::extractData() +std::pair JsonWriter::extractDataImpl() { assert(mStartNodeCount == 0 && "did not close all nodes"); assert(mpBuffer && "data already extracted"); @@ -409,24 +413,23 @@ char* JsonWriter::extractData() ++mPos; // null-terminate *mPos = 0; + const int sz = mPos - mpBuffer; mPos = nullptr; - char* pRet = nullptr; - std::swap(pRet, mpBuffer); - return pRet; + return { std::exchange(mpBuffer, nullptr), sz }; } OString JsonWriter::extractAsOString() { - char* pChar = extractData(); - OString ret(pChar); + auto[pChar, sz] = extractDataImpl(); + OString ret(pChar, sz); free(pChar); return ret; } std::string JsonWriter::extractAsStdString() { - char* pChar = extractData(); - std::string ret(pChar); + auto[pChar, sz] = extractDataImpl(); + std::string ret(pChar, sz); free(pChar); return ret; }