aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/writer.cpp
diff options
context:
space:
mode:
authormonster <monster@yandex-team.ru>2022-02-10 16:47:19 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:19 +0300
commitdd76ae1f6213d065375ab296699f764faafbe5bd (patch)
tree075a4553d0011d8f92752c0891794febb15912cf /library/cpp/yson/writer.cpp
parentf02a874a7290593efbe4b3aeae69a04b46c1cc86 (diff)
downloadydb-dd76ae1f6213d065375ab296699f764faafbe5bd.tar.gz
Restoring authorship annotation for <monster@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/yson/writer.cpp')
-rw-r--r--library/cpp/yson/writer.cpp136
1 files changed, 68 insertions, 68 deletions
diff --git a/library/cpp/yson/writer.cpp b/library/cpp/yson/writer.cpp
index 054459f9f5..9f0386e213 100644
--- a/library/cpp/yson/writer.cpp
+++ b/library/cpp/yson/writer.cpp
@@ -1,18 +1,18 @@
-#include "writer.h"
+#include "writer.h"
#include "detail.h"
-#include "format.h"
+#include "format.h"
#include "parser.h"
#include "varint.h"
-#include "zigzag.h"
-
-#include <util/string/cast.h>
-
+#include "zigzag.h"
+
+#include <util/string/cast.h>
+
#include <cmath>
namespace NYson {
////////////////////////////////////////////////////////////////////////////////
-
+
// Copied from <util/string/escape.cpp>
namespace {
inline char HexDigit(char value) {
@@ -22,26 +22,26 @@ namespace NYson {
else
return 'A' + value - 10;
}
-
+
inline char OctDigit(char value) {
Y_ASSERT(value < 8);
return '0' + value;
}
-
+
inline bool IsPrintable(char c) {
return c >= 32 && c <= 126;
}
-
+
inline bool IsHexDigit(char c) {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
-
+
inline bool IsOctDigit(char c) {
return c >= '0' && c <= '7';
}
-
+
const size_t ESCAPE_C_BUFFER_SIZE = 4;
-
+
inline size_t EscapeC(unsigned char c, char next, char r[ESCAPE_C_BUFFER_SIZE]) {
// (1) Printable characters go as-is, except backslash and double quote.
// (2) Characters \r, \n, \t and \0 ... \7 replaced by their simple escape characters (if possible).
@@ -87,33 +87,33 @@ namespace NYson {
return 4;
}
}
-
+
void EscapeC(const char* str, size_t len, IOutputStream& output) {
char buffer[ESCAPE_C_BUFFER_SIZE];
-
+
size_t i, j;
for (i = 0, j = 0; i < len; ++i) {
size_t rlen = EscapeC(str[i], (i + 1 < len ? str[i + 1] : 0), buffer);
-
+
if (rlen > 1) {
output.Write(str + j, i - j);
j = i + 1;
output.Write(buffer, rlen);
}
}
-
+
if (j > 0) {
output.Write(str + j, len - j);
} else {
output.Write(str, len);
}
- }
-
+ }
+
TString FloatToStringWithNanInf(double value) {
if (std::isfinite(value)) {
return ::ToString(value);
}
-
+
static const TStringBuf nanLiteral = "%nan";
static const TStringBuf infLiteral = "%inf";
static const TStringBuf negativeInfLiteral = "%-inf";
@@ -130,7 +130,7 @@ namespace NYson {
}
}
-
+
////////////////////////////////////////////////////////////////////////////////
TYsonWriter::TYsonWriter(
@@ -147,17 +147,17 @@ namespace NYson {
{
Y_ASSERT(stream);
}
-
+
void TYsonWriter::WriteIndent() {
for (int i = 0; i < IndentSize * Depth; ++i) {
Stream->Write(' ');
}
}
-
+
bool TYsonWriter::IsTopLevelFragmentContext() const {
return Depth == 0 && (Type == ::NYson::EYsonType::ListFragment || Type == ::NYson::EYsonType::MapFragment);
- }
-
+ }
+
void TYsonWriter::EndNode() {
if (IsTopLevelFragmentContext()) {
ETokenType separatorToken =
@@ -168,15 +168,15 @@ namespace NYson {
if (Format == EYsonFormat::Text || Format == EYsonFormat::Pretty) {
Stream->Write('\n');
}
- }
- }
-
+ }
+ }
+
void TYsonWriter::BeginCollection(ETokenType beginToken) {
Stream->Write(TokenTypeToChar(beginToken));
++Depth;
BeforeFirstItem = true;
}
-
+
void TYsonWriter::CollectionItem(ETokenType separatorToken) {
if (!IsTopLevelFragmentContext()) {
if (!BeforeFirstItem) {
@@ -187,21 +187,21 @@ namespace NYson {
Stream->Write('\n');
WriteIndent();
}
- }
-
+ }
+
BeforeFirstItem = false;
}
void TYsonWriter::EndCollection(ETokenType endToken) {
--Depth;
if (Format == EYsonFormat::Pretty && !BeforeFirstItem) {
- Stream->Write('\n');
- WriteIndent();
- }
+ Stream->Write('\n');
+ WriteIndent();
+ }
Stream->Write(TokenTypeToChar(endToken));
BeforeFirstItem = false;
- }
-
+ }
+
void TYsonWriter::WriteStringScalar(const TStringBuf& value) {
if (Format == EYsonFormat::Binary) {
Stream->Write(NDetail::StringMarker);
@@ -213,12 +213,12 @@ namespace NYson {
Stream->Write('"');
}
}
-
+
void TYsonWriter::OnStringScalar(TStringBuf value) {
WriteStringScalar(value);
EndNode();
- }
-
+ }
+
void TYsonWriter::OnInt64Scalar(i64 value) {
if (Format == EYsonFormat::Binary) {
Stream->Write(NDetail::Int64Marker);
@@ -227,8 +227,8 @@ namespace NYson {
Stream->Write(::ToString(value));
}
EndNode();
- }
-
+ }
+
void TYsonWriter::OnUint64Scalar(ui64 value) {
if (Format == EYsonFormat::Binary) {
Stream->Write(NDetail::Uint64Marker);
@@ -238,8 +238,8 @@ namespace NYson {
Stream->Write("u");
}
EndNode();
- }
-
+ }
+
void TYsonWriter::OnDoubleScalar(double value) {
if (Format == EYsonFormat::Binary) {
Stream->Write(NDetail::DoubleMarker);
@@ -252,44 +252,44 @@ namespace NYson {
}
}
EndNode();
- }
-
+ }
+
void TYsonWriter::OnBooleanScalar(bool value) {
if (Format == EYsonFormat::Binary) {
Stream->Write(value ? NDetail::TrueMarker : NDetail::FalseMarker);
} else {
Stream->Write(value ? "%true" : "%false");
- }
+ }
EndNode();
- }
-
+ }
+
void TYsonWriter::OnEntity() {
Stream->Write(TokenTypeToChar(EntityToken));
EndNode();
- }
-
+ }
+
void TYsonWriter::OnBeginList() {
BeginCollection(BeginListToken);
}
-
+
void TYsonWriter::OnListItem() {
CollectionItem(ListItemSeparatorToken);
}
-
+
void TYsonWriter::OnEndList() {
EndCollection(EndListToken);
EndNode();
}
-
+
void TYsonWriter::OnBeginMap() {
BeginCollection(BeginMapToken);
}
-
+
void TYsonWriter::OnKeyedItem(TStringBuf key) {
CollectionItem(KeyedItemSeparatorToken);
-
+
WriteStringScalar(key);
-
+
if (Format == NYson::EYsonFormat::Pretty) {
Stream->Write(' ');
}
@@ -297,26 +297,26 @@ namespace NYson {
if (Format == NYson::EYsonFormat::Pretty) {
Stream->Write(' ');
}
-
+
BeforeFirstItem = false;
- }
+ }
void TYsonWriter::OnEndMap() {
EndCollection(EndMapToken);
EndNode();
- }
-
+ }
+
void TYsonWriter::OnBeginAttributes() {
BeginCollection(BeginAttributesToken);
}
-
+
void TYsonWriter::OnEndAttributes() {
EndCollection(EndAttributesToken);
if (Format == NYson::EYsonFormat::Pretty) {
Stream->Write(' ');
}
}
-
+
void TYsonWriter::OnRaw(TStringBuf yson, EYsonType type) {
if (EnableRaw) {
Stream->Write(yson);
@@ -325,19 +325,19 @@ namespace NYson {
TYsonConsumerBase::OnRaw(yson, type);
}
}
-
+
TYsonWriter::TState TYsonWriter::State() const {
TState state;
state.Depth = Depth;
state.BeforeFirstItem = BeforeFirstItem;
return state;
- }
-
+ }
+
void TYsonWriter::Reset(const TState& state) {
Depth = state.Depth;
BeforeFirstItem = state.BeforeFirstItem;
- }
-
+ }
+
////////////////////////////////////////////////////////////////////////////////
void ReformatYsonStream(
@@ -351,5 +351,5 @@ namespace NYson {
}
////////////////////////////////////////////////////////////////////////////////
-
+
} // namespace NYson