desktop/qa/desktop_lib/test_desktop_lib.cxx |    2 
 include/tools/json_writer.hxx               |   20 ++++----
 sc/source/ui/view/tabview.cxx               |    4 -
 tools/qa/cppunit/test_json_writer.cxx       |    3 -
 tools/source/misc/json_writer.cxx           |   63 +++-------------------------
 5 files changed, 22 insertions(+), 70 deletions(-)

New commits:
commit 03c474c640d63f54d520712693e2f47976d8d531
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Oct 13 11:59:29 2021 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Oct 13 17:39:32 2021 +0200

    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 <mike.kagan...@collabora.com>

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index d827124a4e52..389ae7af74af 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2356,7 +2356,7 @@ void DesktopLOKTest::testCommentsCallbacksWriter()
 namespace
 {
 
-void addParameter(tools::JsonWriter& rJson, const char* sName, OString const & 
type, OString const & value)
+void addParameter(tools::JsonWriter& rJson, const char* sName, 
std::string_view type, std::string_view value)
 {
     auto testNode = rJson.startNode(sName);
     rJson.put("type", type);
diff --git a/include/tools/json_writer.hxx b/include/tools/json_writer.hxx
index fb40e1920314..45df53c61c5f 100644
--- a/include/tools/json_writer.hxx
+++ b/include/tools/json_writer.hxx
@@ -8,13 +8,15 @@
  */
 #pragma once
 
+#include <sal/config.h>
+
 #include <tools/toolsdllapi.h>
+#include <rtl/string.hxx>
 #include <rtl/ustring.hxx>
+#include <sal/types.h>
 
-namespace rtl
-{
-class OStringBuffer;
-}
+#include <string>
+#include <string_view>
 
 /** Simple JSON encoder designed specifically for LibreOfficeKit purposes.
  *
@@ -49,11 +51,11 @@ public:
     [[nodiscard]] ScopedJsonWriterStruct startStruct();
 
     void put(const char* pPropName, const OUString& rPropValue);
-    void put(const char* pPropName, const OString& rPropValue);
-    void put(const char* pPropName, const char* pPropVal);
-    void put(const char* pPropName, const std::string& rPropValue)
+    // Assumes utf-8 property value encoding
+    void put(const char* pPropName, std::string_view rPropValue);
+    void put(const char* pPropName, const char* pPropVal)
     {
-        put(pPropName, rPropValue.data());
+        put(pPropName, std::string_view(pPropVal));
     }
 
     void put(const char* pPropName, sal_uInt16 nPropVal) { put(pPropName, 
sal_Int64(nPropVal)); }
@@ -67,7 +69,7 @@ public:
     void putSimpleValue(const OUString& rPropValue);
 
     /// This assumes that this data belongs at this point in the stream, and 
is valid, and properly encoded
-    void putRaw(const rtl::OStringBuffer&);
+    void putRaw(std::string_view);
 
     /** Hands ownership of the underlying storage buffer to the caller,
      * after this no more document modifications may be written. */
diff --git a/sc/source/ui/view/tabview.cxx b/sc/source/ui/view/tabview.cxx
index 1315b2d88214..8d8412d3673e 100644
--- a/sc/source/ui/view/tabview.cxx
+++ b/sc/source/ui/view/tabview.cxx
@@ -2720,7 +2720,7 @@ void ScTabView::getRowColumnHeaders(const 
tools::Rectangle& rRectangle, tools::J
     if (nRowGroupDepth > 0)
     {
         aRowGroupsBuffer.append(",\n");
-        rJsonWriter.putRaw(aRowGroupsBuffer.getStr());
+        rJsonWriter.putRaw(aRowGroupsBuffer);
     }
     ///  end collecting ROWS
 
@@ -2815,7 +2815,7 @@ void ScTabView::getRowColumnHeaders(const 
tools::Rectangle& rRectangle, tools::J
     if (nColGroupDepth > 0)
     {
         aColGroupsBuffer.append(",\n");
-        rJsonWriter.putRaw(aColGroupsBuffer.getStr());
+        rJsonWriter.putRaw(aColGroupsBuffer);
     }
     ///  end collecting COLs
 
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<sal_Int32>(12));
     }
 
     std::unique_ptr<char, o3tl::free_delete> 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 <stdio.h>
 #include <algorithm>
 #include <cstring>
-#include <rtl/strbuf.hxx>
 #include <rtl/math.hxx>
 
 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()

Reply via email to