/* -*- 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/.
*/
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;
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.size() * 6;
ensureSpace(nPropNameLength + nWorstCasePropValLength + 8);
addCommaBeforeField();
writeEscapedOUString(pPropName);
memcpy(mPos, ": ", 2);
mPos += 2;
writeEscapedOUString(rPropVal);
validate();
}
void JsonWriter::put(std::string_view pPropName, const OUString& rPropVal)
{ // 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 + 2;
ensureSpaceAndWriteNameColon(pPropName, nWorstCasePropValLength);
writeEscapedOUString(rPropVal);
}
void JsonWriter::put(std::string_view pPropName, std::string_view rPropVal)
{ // escaping can double the length, plus quotes auto nWorstCasePropValLength = rPropVal.size() * 2 + 2;
ensureSpaceAndWriteNameColon(pPropName, nWorstCasePropValLength);
*mPos = '"';
++mPos;
// copy and perform escaping for (size_t i = 0; i < rPropVal.size(); ++i)
{ char ch = rPropVal[i]; if (ch == 0) break; // Special processing of U+2028 and U+2029 if (ch == '\xE2' && i + 2 < rPropVal.size() && rPropVal[i + 1] == '\x80'
&& (rPropVal[i + 2] == '\xA8' || rPropVal[i + 2] == '\xA9'))
{
writeEscapedSequence(rPropVal[i + 2] == '\xA8' ? 0x2028 : 0x2029, mPos);
i += 2;
} elseif (!writeEscapedSequence(static_cast<sal_uInt32>(ch), mPos))
{
*mPos = ch;
++mPos;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.