summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorNoel Grandin <noelgrandin@gmail.com>2020-06-15 20:32:25 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-06-18 10:54:10 +0200
commitc8bed6445b244a5d9021dbd9a2ff19d80c03917b (patch)
treecdfdf457f2617b4480961009e6b50a645d48c592 /tools
parent41d75ee814d71513922a12fae82f2e7eecbcd5f5 (diff)
new json writer for LOK
we shave about 3 memory copies off in the process, and make the class play nicely with our string types. Change-Id: I1f614fb35b1de499ac99e3b33ac638ad81054bed Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96393 Tested-by: Noel Grandin <noel.grandin@collabora.co.uk> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'tools')
-rw-r--r--tools/CppunitTest_tools_test.mk1
-rw-r--r--tools/Library_tl.mk1
-rw-r--r--tools/qa/cppunit/test_json_writer.cxx57
-rw-r--r--tools/source/misc/json_writer.cxx253
4 files changed, 312 insertions, 0 deletions
diff --git a/tools/CppunitTest_tools_test.mk b/tools/CppunitTest_tools_test.mk
index 5672be53353f..46a6cf5242cd 100644
--- a/tools/CppunitTest_tools_test.mk
+++ b/tools/CppunitTest_tools_test.mk
@@ -19,6 +19,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,tools_test, \
tools/qa/cppunit/test_time \
tools/qa/cppunit/test_fract \
tools/qa/cppunit/test_inetmime \
+ tools/qa/cppunit/test_json_writer \
tools/qa/cppunit/test_pathutils \
tools/qa/cppunit/test_reversemap \
tools/qa/cppunit/test_stream \
diff --git a/tools/Library_tl.mk b/tools/Library_tl.mk
index 233e75db0050..ecad06913ed2 100644
--- a/tools/Library_tl.mk
+++ b/tools/Library_tl.mk
@@ -69,6 +69,7 @@ $(eval $(call gb_Library_add_exception_objects,tl,\
tools/source/memtools/multisel \
tools/source/misc/cpuid \
tools/source/misc/extendapplicationenvironment \
+ tools/source/misc/json_writer \
tools/source/ref/globname \
tools/source/ref/ref \
tools/source/stream/stream \
diff --git a/tools/qa/cppunit/test_json_writer.cxx b/tools/qa/cppunit/test_json_writer.cxx
new file mode 100644
index 000000000000..c4e9331c8d17
--- /dev/null
+++ b/tools/qa/cppunit/test_json_writer.cxx
@@ -0,0 +1,57 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <test/bootstrapfixture.hxx>
+#include <rtl/ustring.hxx>
+#include <tools/stream.hxx>
+#include <tools/json_writer.hxx>
+
+namespace
+{
+class JsonWriterTest : public test::BootstrapFixture
+{
+public:
+ JsonWriterTest()
+ : BootstrapFixture(true, false)
+ {
+ }
+
+ virtual void setUp() override {}
+
+ void test1();
+
+ CPPUNIT_TEST_SUITE(JsonWriterTest);
+ CPPUNIT_TEST(test1);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void JsonWriterTest::test1()
+{
+ tools::JsonWriter aJson;
+
+ {
+ auto testNode = aJson.startNode("node");
+ aJson.put("oustring", OUString("val1"));
+ aJson.put("ostring", OString("val2"));
+ aJson.put("charptr", "val3");
+ aJson.put("int", 12);
+ }
+
+ std::unique_ptr<char[]> result(aJson.extractData());
+
+ CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"oustring\": \"val1\", \"ostring\": \"val2\", "
+ "\"charptr\": \"val3\", \"int\": 12}}"),
+ std::string(result.get()));
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(JsonWriterTest);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
new file mode 100644
index 000000000000..a233381038c4
--- /dev/null
+++ b/tools/source/misc/json_writer.cxx
@@ -0,0 +1,253 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <tools/json_writer.hxx>
+#include <stdio.h>
+#include <cstring>
+
+namespace tools
+{
+/** These buffers are short-lived, so rather waste some space and avoid the cost of
+ * repeated calls into the allocator */
+constexpr int DEFAULT_BUFFER_SIZE = 2048;
+
+JsonWriter::JsonWriter()
+ : mSpaceAllocated(DEFAULT_BUFFER_SIZE)
+ , maBuffer(new char[mSpaceAllocated])
+ , mStartNodeCount(0)
+ , mPos(maBuffer.get())
+{
+ *mPos = '{';
+ ++mPos;
+ *mPos = ' ';
+ ++mPos;
+}
+
+JsonWriter::~JsonWriter() { assert(!maBuffer && "forgot to extract data?"); }
+
+ScopedJsonWriterNode JsonWriter::startNode(const char* pNodeName)
+{
+ auto len = strlen(pNodeName);
+ ensureSpace(len + 4);
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pNodeName, len);
+ mPos += len;
+ strncpy(mPos, "\": { ", 5);
+ mPos += 5;
+ mStartNodeCount++;
+ mbFirstFieldInNode = true;
+ return ScopedJsonWriterNode(*this);
+}
+
+void JsonWriter::endNode()
+{
+ assert(mStartNodeCount && "mismatched StartNode/EndNode somewhere");
+ --mStartNodeCount;
+ ensureSpace(1);
+ *mPos = '}';
+ ++mPos;
+}
+
+void JsonWriter::put(const char* pPropName, const OUString& rPropVal)
+{
+ addCommaBeforeField();
+
+ auto nPropNameLength = strlen(pPropName);
+ auto nWorstCasePropValLength = rPropVal.getLength() * 2;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 6);
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ strncpy(mPos, "\": \"", 4);
+ mPos += 4;
+
+ // Convert from UTF-16 to UTF-8 and perform escaping
+ for (int i = 0; i < rPropVal.getLength(); ++i)
+ {
+ sal_Unicode ch = rPropVal[i];
+ if (ch == '\\')
+ {
+ *mPos = static_cast<char>(ch);
+ ++mPos;
+ *mPos = static_cast<char>(ch);
+ ++mPos;
+ }
+ else if (ch == '"')
+ {
+ *mPos = '\\';
+ ++mPos;
+ *mPos = static_cast<char>(ch);
+ ++mPos;
+ }
+ else if (ch <= 0x7F)
+ {
+ *mPos = static_cast<char>(ch);
+ ++mPos;
+ }
+ else if (ch <= 0x7FF)
+ {
+ *mPos = 0xC0 | (ch >> 6); /* 110xxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
+ else
+ {
+ *mPos = 0xE0 | (ch >> 12); /* 1110xxxx */
+ ++mPos;
+ *mPos = 0x80 | ((ch >> 6) & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ *mPos = 0x80 | (ch & 0x3F); /* 10xxxxxx */
+ ++mPos;
+ }
+ }
+
+ *mPos = '"';
+ ++mPos;
+}
+
+void JsonWriter::put(const char* pPropName, const OString& rPropVal)
+{
+ addCommaBeforeField();
+
+ auto nPropNameLength = strlen(pPropName);
+ auto nWorstCasePropValLength = rPropVal.getLength();
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 6);
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ strncpy(mPos, "\": \"", 4);
+ mPos += 4;
+
+ // copy and perform escaping
+ for (int i = 0; i < rPropVal.getLength(); ++i)
+ {
+ char ch = rPropVal[i];
+ if (ch == '\\')
+ {
+ *mPos = ch;
+ ++mPos;
+ *mPos = ch;
+ ++mPos;
+ }
+ else if (ch == '"')
+ {
+ *mPos = '\\';
+ ++mPos;
+ *mPos = ch;
+ ++mPos;
+ }
+ else
+ {
+ *mPos = ch;
+ ++mPos;
+ }
+ }
+
+ *mPos = '"';
+ ++mPos;
+}
+
+void JsonWriter::put(const char* pPropName, const char* pPropVal)
+{
+ addCommaBeforeField();
+
+ auto nPropNameLength = strlen(pPropName);
+ auto nPropValLength = strlen(pPropVal);
+ auto nWorstCasePropValLength = nPropValLength * 2;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 6);
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ strncpy(mPos, "\": \"", 4);
+ mPos += 4;
+
+ // copy and perform escaping
+ for (;;)
+ {
+ char ch = *pPropVal;
+ if (!ch)
+ break;
+ ++pPropVal;
+ if (ch == '\\')
+ {
+ *mPos = ch;
+ ++mPos;
+ *mPos = ch;
+ ++mPos;
+ }
+ else if (ch == '"')
+ {
+ *mPos = '\\';
+ ++mPos;
+ *mPos = ch;
+ ++mPos;
+ }
+ else
+ {
+ *mPos = ch;
+ ++mPos;
+ }
+ }
+
+ *mPos = '"';
+ ++mPos;
+}
+
+void JsonWriter::put(const char* pPropName, int nPropVal)
+{
+ addCommaBeforeField();
+
+ auto nPropNameLength = strlen(pPropName);
+ auto nWorstCasePropValLength = 32;
+ ensureSpace(nPropNameLength + nWorstCasePropValLength + 6);
+ *mPos = '"';
+ ++mPos;
+ memcpy(mPos, pPropName, nPropNameLength);
+ mPos += nPropNameLength;
+ strncpy(mPos, "\": ", 3);
+ mPos += 3;
+
+ mPos += sprintf(mPos, "%d", nPropVal);
+}
+
+void JsonWriter::addCommaBeforeField()
+{
+ if (mbFirstFieldInNode)
+ mbFirstFieldInNode = false;
+ else
+ {
+ *mPos = ',';
+ ++mPos;
+ *mPos = ' ';
+ ++mPos;
+ }
+}
+
+/** Hands ownership of the the underlying storage buffer to the caller,
+ * after this no more document modifications may be written. */
+char* JsonWriter::extractData()
+{
+ assert(mStartNodeCount == 0 && "did not close all nodes");
+ assert(maBuffer && "data already extracted");
+ // add closing brace
+ *mPos = '}';
+ ++mPos;
+ // null-terminate
+ *mPos = 0;
+ mPos = nullptr;
+ return maBuffer.release();
+}
+
+} // namespace tools
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */