Simplify JsonWriter a bit
Move ensureSpace code to json_writer.cxx, and merge with reallocBuffer. The methods are private, and are only used in methods in the same .CXX, so the code will get inlined as compiler decides anyway, but becomes simpler. Make extractDataAs* to consider the known size of the data, to avoid calculating null-terminated size. To do that, the code is moved from extractData to private extractDataImpl, which returns both pointer and size. Change-Id: I7c0e425b5c584089c6e866c31d4cfdb5e242d66b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123568 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
/** 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<char*, int> extractDataImpl();
|
||||
void ensureSpace(int noMoreBytesRequired);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@@ -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<int>(mSpaceAllocated * 2, (currentUsed + noMoreBytesRequired) * 2);
|
||||
mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize));
|
||||
mPos = mpBuffer + currentUsed;
|
||||
mSpaceAllocated = newSize;
|
||||
if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
|
||||
{
|
||||
auto newSize = (currentUsed + noMoreBytesRequired) * 2;
|
||||
mpBuffer = static_cast<char*>(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<char*, int> 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user