use more string_view in tools::JsonWriter

Change-Id: Ia3ac54a1164b99a806298a47115ea3bc570f3b0c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/163810
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2024-02-23 16:50:33 +01:00
parent 822b221b3d
commit 4603aa2888
3 changed files with 14 additions and 13 deletions
+3 -3
View File
@@ -48,7 +48,7 @@ public:
[[nodiscard]] ScopedJsonWriterNode<']'> startArray(std::string_view nodeName);
[[nodiscard]] ScopedJsonWriterNode<'}'> startStruct();
void put(std::u16string_view pPropName, const OUString& rPropValue);
void put(std::u16string_view pPropName, std::u16string_view rPropValue);
void put(std::string_view pPropName, const OUString& rPropValue);
// Assumes utf-8 property value encoding
@@ -69,7 +69,7 @@ public:
}
void put(std::string_view pPropName, bool);
void putSimpleValue(const OUString& rPropValue);
void putSimpleValue(std::u16string_view rPropValue);
/// This assumes that this data belongs at this point in the stream, and is valid, and properly encoded
void putRaw(std::string_view);
@@ -84,7 +84,7 @@ public:
private:
void endNode(char closing);
void addCommaBeforeField();
void writeEscapedOUString(const OUString& rPropVal);
void writeEscapedOUString(std::u16string_view rPropVal);
void closeDocument();
void ensureSpace(int noMoreBytesRequired);
void ensureSpaceAndWriteNameColon(std::string_view name, int valSize);
+2 -2
View File
@@ -82,8 +82,8 @@ void JsonWriterTest::testArray()
tools::JsonWriter aJson;
{
auto aArray = aJson.startArray("items");
aJson.putSimpleValue("foo");
aJson.putSimpleValue("bar");
aJson.putSimpleValue(u"foo");
aJson.putSimpleValue(u"bar");
}
OString aResult(aJson.finishAndGetAsOString());
+9 -8
View File
@@ -8,6 +8,7 @@
*/
#include <tools/json_writer.hxx>
#include <o3tl/string_view.hxx>
#include <stdio.h>
#include <cstring>
#include <rtl/math.hxx>
@@ -130,16 +131,16 @@ static bool writeEscapedSequence(sal_uInt32 ch, char*& pos)
}
}
void JsonWriter::writeEscapedOUString(const OUString& rPropVal)
void JsonWriter::writeEscapedOUString(std::u16string_view rPropVal)
{
*mPos = '"';
++mPos;
// Convert from UTF-16 to UTF-8 and perform escaping
sal_Int32 i = 0;
while (i < rPropVal.getLength())
while (i < static_cast<sal_Int32>(rPropVal.size()))
{
sal_uInt32 ch = rPropVal.iterateCodePoints(&i);
sal_uInt32 ch = o3tl::iterateCodePoints(rPropVal, &i);
if (writeEscapedSequence(ch, mPos))
continue;
if (ch <= 0x7F)
@@ -182,17 +183,17 @@ void JsonWriter::writeEscapedOUString(const OUString& rPropVal)
validate();
}
void JsonWriter::put(std::u16string_view pPropName, const OUString& rPropVal)
void JsonWriter::put(std::u16string_view pPropName, std::u16string_view rPropVal)
{
auto nPropNameLength = pPropName.length();
// But values can be any UTF-8,
// if the string only contains of 0x2028, it will be expanded 6 times (see writeEscapedSequence)
auto nWorstCasePropValLength = rPropVal.getLength() * 6;
auto nWorstCasePropValLength = rPropVal.size() * 6;
ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
addCommaBeforeField();
writeEscapedOUString(OUString(pPropName));
writeEscapedOUString(pPropName);
memcpy(mPos, ": ", 2);
mPos += 2;
@@ -252,9 +253,9 @@ void JsonWriter::put(std::string_view pPropName, bool nPropVal)
putLiteral(pPropName, nPropVal ? std::string_view("true") : std::string_view("false"));
}
void JsonWriter::putSimpleValue(const OUString& rPropVal)
void JsonWriter::putSimpleValue(std::u16string_view rPropVal)
{
auto nWorstCasePropValLength = rPropVal.getLength() * 6;
auto nWorstCasePropValLength = rPropVal.size() * 6;
ensureSpace(nWorstCasePropValLength + 4);
addCommaBeforeField();