From 03c474c640d63f54d520712693e2f47976d8d531 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 13 Oct 2021 11:59:29 +0300 Subject: Unify JsonWriter::put and putRaw a bit In the process, it turned out that there was unnecessary conversion of OStringBuffer to OString and back to OStringBuffer when using putRaw, which is avoided now. Change-Id: I1e3ee685679df0b025bee8f4430624ee5bc9ccb3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123547 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- tools/qa/cppunit/test_json_writer.cxx | 3 +- tools/source/misc/json_writer.cxx | 63 ++++------------------------------- 2 files changed, 8 insertions(+), 58 deletions(-) (limited to 'tools') diff --git a/tools/qa/cppunit/test_json_writer.cxx b/tools/qa/cppunit/test_json_writer.cxx index d5c037801067..fe3019e91b0a 100644 --- a/tools/qa/cppunit/test_json_writer.cxx +++ b/tools/qa/cppunit/test_json_writer.cxx @@ -46,14 +46,13 @@ void JsonWriterTest::test1() { auto testNode = aJson.startNode("node"); aJson.put("oustring", OUString("val1")); - aJson.put("ostring", OString("val2")); aJson.put("charptr", "val3"); aJson.put("int", static_cast(12)); } std::unique_ptr result(aJson.extractData()); - CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"oustring\": \"val1\", \"ostring\": \"val2\", " + CPPUNIT_ASSERT_EQUAL(std::string("{ \"node\": { \"oustring\": \"val1\", " "\"charptr\": \"val3\", \"int\": 12}}"), std::string(result.get())); } diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx index 30ad911f9754..09f34c25c3c7 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -11,7 +11,6 @@ #include #include #include -#include #include namespace tools @@ -221,12 +220,12 @@ void JsonWriter::put(const char* pPropName, const OUString& rPropVal) ++mPos; } -void JsonWriter::put(const char* pPropName, const OString& rPropVal) +void JsonWriter::put(const char* pPropName, std::string_view rPropVal) { // we assume property names are ascii auto nPropNameLength = strlen(pPropName); // escaping can double the length - auto nWorstCasePropValLength = rPropVal.getLength() * 2; + auto nWorstCasePropValLength = rPropVal.size() * 2; ensureSpace(nPropNameLength + nWorstCasePropValLength + 8); addCommaBeforeField(); @@ -239,7 +238,7 @@ void JsonWriter::put(const char* pPropName, const OString& rPropVal) mPos += 4; // copy and perform escaping - for (int i = 0; i < rPropVal.getLength(); ++i) + for (size_t i = 0; i < rPropVal.size(); ++i) { char ch = rPropVal[i]; if (ch == '\\') @@ -267,54 +266,6 @@ void JsonWriter::put(const char* pPropName, const OString& rPropVal) ++mPos; } -void JsonWriter::put(const char* pPropName, const char* pPropVal) -{ - auto nPropNameLength = strlen(pPropName); - auto nPropValLength = strlen(pPropVal); - auto nWorstCasePropValLength = nPropValLength * 2; - ensureSpace(nPropNameLength + nWorstCasePropValLength + 8); - - addCommaBeforeField(); - - *mPos = '"'; - ++mPos; - memcpy(mPos, pPropName, nPropNameLength); - mPos += nPropNameLength; - memcpy(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, sal_Int64 nPropVal) { auto nPropNameLength = strlen(pPropName); @@ -391,14 +342,14 @@ void JsonWriter::putSimpleValue(const OUString& rPropVal) ++mPos; } -void JsonWriter::putRaw(const rtl::OStringBuffer& rRawBuf) +void JsonWriter::putRaw(std::string_view rRawBuf) { - ensureSpace(rRawBuf.getLength() + 2); + ensureSpace(rRawBuf.size() + 2); addCommaBeforeField(); - memcpy(mPos, rRawBuf.getStr(), rRawBuf.getLength()); - mPos += rRawBuf.getLength(); + memcpy(mPos, rRawBuf.data(), rRawBuf.size()); + mPos += rRawBuf.size(); } void JsonWriter::addCommaBeforeField() -- cgit