summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-10-14 00:15:11 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2021-10-14 07:40:52 +0200
commit02d225f2a46b3d531e187976d8f2c39ed390899c (patch)
treedb31918ce20b15d592e4b0b6dca94a3d623b95e7 /tools
parent8a017d25a62e878fdd32f189f0663b05d2ffb9cf (diff)
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>
Diffstat (limited to 'tools')
-rw-r--r--tools/source/misc/json_writer.cxx29
1 files changed, 16 insertions, 13 deletions
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<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;
}