1#include <jsonifier/Parser.hpp>
2#include <jsonifier/Document_Impl.hpp>
3#include <jsonifier/Jsonifier_Impl.hpp>
7 JsonifierResult<Document> Parser::parseJson(std::string_view
string)
noexcept {
8 if (
string.size() == 0) {
9 return ErrorCode::No_String_Error;
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;
18 this->generateJsonIndices();
19 return std::forward<Document>(JsonIterator{
this });
22 JsonifierResult<Document> Parser::parseJson(
const char*
string,
size_t stringLength)
noexcept {
23 if (stringLength == 0) {
24 return ErrorCode::No_String_Error;
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;
33 this->generateJsonIndices();
34 return std::forward<Document>(JsonIterator{
this });
37 JsonifierResult<Document> Parser::parseJson(
const std::string&
string)
noexcept {
38 if (
string.size() == 0) {
39 return ErrorCode::No_String_Error;
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;
48 this->generateJsonIndices();
49 return std::forward<Document>(JsonIterator{
this });
52 char* Parser::getStringView() noexcept {
53 return this->stringView;
56 uint8_t* Parser::getStringBuffer() noexcept {
57 return this->stringBuffer;
60 uint32_t* Parser::getStructuralIndices() noexcept {
61 return this->structuralIndices.operator uint32_t*();
64 size_t Parser::maxDepth() noexcept {
68 size_t Parser::getTapeLength() noexcept {
69 return this->tapeLength;
72 uint64_t Parser::round(int64_t a, int64_t n)
noexcept {
73 return (((a) + (( n )-1)) & ~(( n )-1));
76 ErrorCode Parser::allocate() noexcept {
77 if (this->stringLengthRaw == 0) {
78 return ErrorCode::No_String_Error;
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;
88 return ErrorCode::Success;
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() };
98 size_t currentIndexIntoTape{};
99 while (stringReader.hasFullBlock()) {
100 section.submitDataForProcessing(stringReader.fullBlock());
101 section.generateStructurals();
102 stringReader.advance();
105 stringReader.getRemainder(block);
106 section.submitDataForProcessing(block);
107 section.generateStructurals();
108 currentIndexIntoTape += section.getTapeLength();
109 this->tapeLength = currentIndexIntoTape;