aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/rapidjson_helpers.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/json/rapidjson_helpers.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/json/rapidjson_helpers.h')
-rw-r--r--library/cpp/json/rapidjson_helpers.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/library/cpp/json/rapidjson_helpers.h b/library/cpp/json/rapidjson_helpers.h
new file mode 100644
index 0000000000..aeb96ff670
--- /dev/null
+++ b/library/cpp/json/rapidjson_helpers.h
@@ -0,0 +1,104 @@
+#pragma once
+
+#include <util/generic/strbuf.h>
+#include <util/stream/input.h>
+
+namespace NJson {
+ struct TReadOnlyStreamBase {
+ using Ch = char;
+
+ Ch* PutBegin() {
+ Y_ASSERT(false);
+ return nullptr;
+ }
+
+ void Put(Ch) {
+ Y_ASSERT(false);
+ }
+
+ void Flush() {
+ Y_ASSERT(false);
+ }
+
+ size_t PutEnd(Ch*) {
+ Y_ASSERT(false);
+ return 0;
+ }
+ };
+
+ struct TInputStreamWrapper : TReadOnlyStreamBase {
+ Ch Peek() const {
+ if (!Eof) {
+ if (Pos >= Sz) {
+ if (Sz < BUF_SIZE) {
+ Sz += Helper.Read(Buf + Sz, BUF_SIZE - Sz);
+ } else {
+ Sz = Helper.Read(Buf, BUF_SIZE);
+ Pos = 0;
+ }
+ }
+
+ if (Pos < Sz) {
+ return Buf[Pos];
+ }
+ }
+
+ Eof = true;
+ return 0;
+ }
+
+ Ch Take() {
+ auto c = Peek();
+ ++Pos;
+ ++Count;
+ return c;
+ }
+
+ size_t Tell() const {
+ return Count;
+ }
+
+ TInputStreamWrapper(IInputStream& helper)
+ : Helper(helper)
+ , Eof(false)
+ , Sz(0)
+ , Pos(0)
+ , Count(0)
+ {
+ }
+
+ static const size_t BUF_SIZE = 1 << 12;
+
+ IInputStream& Helper;
+ mutable char Buf[BUF_SIZE];
+ mutable bool Eof;
+ mutable size_t Sz;
+ mutable size_t Pos;
+ size_t Count;
+ };
+
+ struct TStringBufStreamWrapper : TReadOnlyStreamBase {
+ Ch Peek() const {
+ return Pos < Data.size() ? Data[Pos] : 0;
+ }
+
+ Ch Take() {
+ auto c = Peek();
+ ++Pos;
+ return c;
+ }
+
+ size_t Tell() const {
+ return Pos;
+ }
+
+ TStringBufStreamWrapper(TStringBuf data)
+ : Data(data)
+ , Pos(0)
+ {
+ }
+
+ TStringBuf Data;
+ size_t Pos;
+ };
+}