DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
Parser.cpp
1#include <jsonifier/Parser.hpp>
2#include <jsonifier/Document_Impl.hpp>
3#include <jsonifier/Jsonifier_Impl.hpp>
4
5namespace Jsonifier {
6
7 JsonifierResult<Document> Parser::parseJson(std::string_view string) noexcept {
8 if (string.size() == 0) {
9 return ErrorCode::No_String_Error;
10 }
11 this->stringView = ( char* )string.data();
12 this->stringLengthRaw = string.size();
13 if (this->allocatedSpace < round(5 * this->stringLengthRaw / 3 + 256, 256)) {
14 if (this->allocate() != ErrorCode::Success) {
15 return ErrorCode::Mem_Alloc_Error;
16 }
17 }
18 this->generateJsonIndices();
19 return std::forward<Document>(JsonIterator{ this });
20 }
21
22 JsonifierResult<Document> Parser::parseJson(const char* string, size_t stringLength) noexcept {
23 if (stringLength == 0) {
24 return ErrorCode::No_String_Error;
25 }
26 this->stringView = ( char* )string;
27 this->stringLengthRaw = stringLength;
28 if (this->allocatedSpace < round(5 * this->stringLengthRaw / 3 + 256, 256)) {
29 if (this->allocate() != ErrorCode::Success) {
30 return ErrorCode::Mem_Alloc_Error;
31 }
32 }
33 this->generateJsonIndices();
34 return std::forward<Document>(JsonIterator{ this });
35 }
36
37 JsonifierResult<Document> Parser::parseJson(const std::string& string) noexcept {
38 if (string.size() == 0) {
39 return ErrorCode::No_String_Error;
40 }
41 this->stringView = ( char* )string.data();
42 this->stringLengthRaw = string.size();
43 if (this->allocatedSpace < round(5 * this->stringLengthRaw / 3 + 256, 256)) {
44 if (this->allocate() != ErrorCode::Success) {
45 return ErrorCode::Mem_Alloc_Error;
46 }
47 }
48 this->generateJsonIndices();
49 return std::forward<Document>(JsonIterator{ this });
50 }
51
52 char* Parser::getStringView() noexcept {
53 return this->stringView;
54 }
55
56 uint8_t* Parser::getStringBuffer() noexcept {
57 return this->stringBuffer;
58 }
59
60 uint32_t* Parser::getStructuralIndices() noexcept {
61 return this->structuralIndices.operator uint32_t*();
62 }
63
64 size_t Parser::maxDepth() noexcept {
65 return 512;
66 }
67
68 size_t Parser::getTapeLength() noexcept {
69 return this->tapeLength;
70 }
71
72 uint64_t Parser::round(int64_t a, int64_t n) noexcept {
73 return (((a) + (( n )-1)) & ~(( n )-1));
74 }
75
76 ErrorCode Parser::allocate() noexcept {
77 if (this->stringLengthRaw == 0) {
78 return ErrorCode::No_String_Error;
79 }
80 this->stringBuffer.reset(round(5 * this->stringLengthRaw / 3 + 256, 256));
81 this->structuralIndices.reset(round(this->stringLengthRaw + 3, 256));
82 this->allocatedSpace = round(5 * this->stringLengthRaw / 3 + 256, 256);
83 if (!(this->stringBuffer && this->structuralIndices)) {
84 this->stringBuffer.reset(0);
85 return ErrorCode::Mem_Alloc_Error;
86 }
87
88 return ErrorCode::Success;
89 }
90
91 ErrorCode Parser::generateJsonIndices() {
92 StopWatch stopWatch{ std::chrono::nanoseconds{ 1 } };
93 if (this->stringView) {
94 StringBlockReader<256> stringReader{};
95 stringReader.addNewString(( uint8_t* )this->stringView, this->stringLengthRaw);
96 SimdStringSection section{ this->getStructuralIndices() };
97 this->tapeLength = 0;
98 size_t currentIndexIntoTape{};
99 while (stringReader.hasFullBlock()) {
100 section.submitDataForProcessing(stringReader.fullBlock());
101 section.generateStructurals();
102 stringReader.advance();
103 }
104 uint8_t block[256];
105 stringReader.getRemainder(block);
106 section.submitDataForProcessing(block);
107 section.generateStructurals();
108 currentIndexIntoTape += section.getTapeLength();
109 this->tapeLength = currentIndexIntoTape;
110 }
111 return Success;
112 }
113
114}