-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcon-decode.hpp
1934 lines (1650 loc) · 48.3 KB
/
qcon-decode.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
///
/// QCON 0.1.4
/// https://github.com/daskie/qcon
/// This header provides a SAX QCON decoder
/// See the README for more info
///
#include <array>
#include <charconv>
#include <chrono>
#include <format>
#include <limits>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <qcon-common.hpp>
namespace qcon
{
///
/// Represents the current state of the decoder
///
enum class DecodeState
{
error, /// An error has occurred
ready, /// The QSON was just loaded
object, /// An object was started
array, /// An array was started
end, /// An object or array was ended
key, /// A key was just decoded
string, /// A string value was decoded
integer, /// An integer value was decoded
floater, /// A floater value was decoded
boolean, /// A boolean value was decoded
date, /// A date value was decoded
time, /// A time value was decoded
datetime, /// A datetime value was decoded
null /// A null value was decoded
};
///
/// Class to facilitate QCON SAX decoding
/// A QCON string is loaded and decoded values may be extracted in sequence
///
class Decoder
{
public:
std::string key{}; /// If an object element was just decoded, holds its key; unspecified otherwise; may be moved from
std::string string{}; /// If a string was just decoded, holds its value; unspecified otherwise; may be moved from
s64 integer{}; /// If an integer was just decoded, holds its value; unspecified otherwise
f64 floater{}; /// If a floater was just decoded, holds its value; unspecified otherwise
bool positive{}; /// If a number was just decoded, indicates whether it was positive; unspecified otherwise
bool boolean{}; /// If a boolean was just decoded, holds its value; unspecified otherwise
Datetime datetime{}; /// If a datetime was just decoded, holds its value; unspecified otherwise
Date & date{datetime.date}; /// If a date was just decoded, holds its value; unspecified otherwise; alias for `datetime.date`
Time & time{datetime.time}; /// If a time was just decoded, holds its value; unspecified otherwise; alias for `datetime.time`
std::string errorMessage{}; /// Holds a brief description of the most recent error; may be moved from
Decoder();
Decoder(const Decoder &) = delete;
Decoder(Decoder && other);
Decoder & operator=(const Decoder &) = delete;
Decoder & operator=(Decoder && other);
///
/// Constructs a decoder and loads the given QSON string
/// The QSON string *must* be null terminated (optimization allowing most range checks to be eliminated)
/// Equivalent to `Decoder d{}; d.load(qcon)`
/// @param qson encoded QSON to load
///
Decoder(const char * qson);
Decoder(const std::string & qson) : Decoder{qson.c_str()} {}
Decoder(std::string &&) = delete; // Prevent binding to temporary
Decoder(std::string_view) = delete; /// QCON string must be null terminated; pass c-string instead
///
/// @return whether the decoding has been thusfar successful
///
explicit operator bool() const { return _state != DecodeState::error; }
///
/// Loads the given QSON string, overriding any existing state
/// The QSON string *must* be null terminated (optimization allowing most range checks to be eliminated)
/// @param qson encoded QSON to load
///
void load(const char * qson);
void load(const std::string & qson) { load(qson.c_str()); }
void load(std::string &&) = delete; /// Prevent binding to temporary
void load(std::string_view) = delete; /// QCON string must be null terminated; pass c-string instead
///
/// Decode the next QCON unit, which could be a value, key-value pair, container start, or container end
/// Calling this after reaching the end of the QCON will yield an error
/// Once in the `error` state, will stay in the `error` state until a new QCON string is loaded
/// @return current state; `error` if there was an error
///
DecodeState step();
///
/// If at root, returns whether the value has yet to be consumed
/// If at the end of a container, consumes the end brace/bracket and returns false
/// If not at the end of a container, returns true
/// If state is `error`, returns false
/// @return whether there are more elements in the current container
///
[[nodiscard]] bool more();
///
/// @return whether the full QCON was successfully decoded
///
[[nodiscard]] bool finished() const;
///
/// Decode a value of the given type into the provided destination variable
/// If the current state is `error`, does nothing
/// If the decode is successful, progresses the internal state like `step()`
/// If the decode is unsuccessful, sets the state to `error`
/// May be used instead of, or in combination with, `step()`
/// @return this
///
Decoder & operator>>(Container container);
Decoder & operator>>(std::string & v);
Decoder & operator>>(std::string_view &) = delete;
Decoder & operator>>(char *) = delete;
Decoder & operator>>(const char *) = delete;
Decoder & operator>>(char &); /// Expects a single character string
Decoder & operator>>(s64 & v);
Decoder & operator>>(u64 & v);
Decoder & operator>>(s32 & v);
Decoder & operator>>(u32 & v);
Decoder & operator>>(s16 & v);
Decoder & operator>>(u16 & v);
Decoder & operator>>(s8 & v);
Decoder & operator>>(u8 & v);
Decoder & operator>>(f64 & v);
Decoder & operator>>(f32 & v);
Decoder & operator>>(bool & v);
Decoder & operator>>(Date & v);
Decoder & operator>>(Time & v);
Decoder & operator>>(Datetime & v);
Decoder & operator>>(Timepoint & v);
Decoder & operator>>(nullptr_t);
///
/// @return current state
///
[[nodiscard]] DecodeState state() const { return _state; }
///
/// @return current position within the QCON string
///
const char * position() const { return _pos; }
private:
DecodeState _state;
const char * _qcon;
const char * _pos;
u64 _stack;
u64 _depth;
const char * _cachedEnd; // Only used for floating point `from_chars`
bool _hadComma;
void _reset();
void _skipSpace();
void _skipSpaceAndComments();
[[nodiscard]] bool _preValueStreamCheck();
[[nodiscard]] bool _preKeyStreamCheck();
void _postValue(DecodeState newState);
void _postValue(bool condition, DecodeState newState);
s32 _tryConsumeSign();
bool _tryConsumeChar(char c);
bool _tryConsumeChars(std::string_view str);
template <bool checkOverflow> bool _tryConsumeBinaryDigit(u64 & dst);
template <bool checkOverflow> bool _tryConsumeOctalDigit(u64 & dst);
template <bool checkOverflow> bool _tryConsumeDecimalDigit(u64 & dst);
template <bool checkOverflow> bool _tryConsumeHexDigit(u64 & dst);
bool _tryConsumeDecimalDigits(u64 digits, u64 & dst);
bool _tryConsumeHexDigits(u64 digits, u64 & dst);
[[nodiscard]] bool _consumeChar(char c);
[[nodiscard]] bool _consumeChars(std::string_view str);
[[nodiscard]] bool _consumeDecimalDigits(u64 digits, u64 & dst);
[[nodiscard]] bool _consumeHexDigits(u64 digits, u64 & dst);
[[nodiscard]] bool _consumeCodePoint(u64 digits, std::string & dst);
[[nodiscard]] bool _consumeEscaped(std::string & dst);
[[nodiscard]] bool _consumeString(std::string & dst);
[[nodiscard]] bool _consumeKey(std::string & dst);
[[nodiscard]] bool _consumeBinaryInteger(u64 & dst);
[[nodiscard]] bool _consumeOctalInteger(u64 & dst);
[[nodiscard]] bool _consumeDecimalInteger(u64 & dst);
[[nodiscard]] bool _consumeHexInteger(u64 & dst);
[[nodiscard]] bool _consumeInteger(s64 & dst);
[[nodiscard]] bool _consumeFloater(f64 & dst);
[[nodiscard]] bool _consumeDate(Date & dst);
[[nodiscard]] bool _consumeTime(Time & dst);
[[nodiscard]] bool _consumeTimezone(Timezone & dst);
void _ingestStart(Container container);
void _ingestEnd();
void _ingestNumber();
void _ingestValue();
template <typename T> void _streamSmallerSignedInteger(T & v);
template <typename T> void _streamSmallerUnsignedInteger(T & v);
};
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace qcon
{
inline consteval std::array<u8, 256u> _createHexTable()
{
std::array<u8, 256u> table;
// Fill table with 255
for (u8 & v : table)
{
v = 255u;
}
// Set '0' - '9'
for (u32 v{0u}; v < 10u; ++v)
{
table[u32('0') + v] = u8(v);
}
// Set 'A' - 'F' and 'a' - 'f'
for (u32 v{0u}; v < 6u; ++v)
{
table[u32('A') + v] = u8(10u + v);
table[u32('a') + v] = u8(10u + v);
}
return table;
}
[[nodiscard]] inline bool _isSpace(const char c)
{
switch (c)
{
case '\t': [[fallthrough]];
case '\n': [[fallthrough]];
case '\v': [[fallthrough]];
case '\f': [[fallthrough]];
case '\r': [[fallthrough]];
case ' ': return true;
default: return false;
}
}
[[nodiscard]] inline bool _isFloater(const char * str)
{
while (_private::isDigit(*str)) ++str;
if (*str == '.')
{
return _private::isDigit(str[1]);
}
else if (*str == 'e' || *str == 'E')
{
return true;
}
else
{
return false;
}
}
inline Decoder::Decoder()
{
_reset();
}
inline Decoder::Decoder(Decoder && other) :
key{std::move(other.key)},
string{std::move(other.string)},
errorMessage{std::move(other.errorMessage)},
_state{other._state},
_qcon{other._qcon},
_pos{other._pos},
_stack{other._stack},
_depth{other._depth},
_cachedEnd{other._cachedEnd},
_hadComma{other._hadComma}
{
other._reset();
}
inline Decoder & Decoder::operator=(Decoder && other)
{
if (&other == this)
{
return *this;
}
key = std::move(other.key);
string = std::move(other.string);
errorMessage = std::move(other.errorMessage);
_state = other._state;
_qcon = other._qcon;
_pos = other._pos;
_stack = other._stack;
_depth = other._depth;
_cachedEnd = other._cachedEnd;
_hadComma = other._hadComma;
other._reset();
return *this;
}
inline Decoder::Decoder(const char * const qcon)
{
load(qcon);
}
inline void Decoder::load(const char * const qcon)
{
_reset();
_state = DecodeState::ready;
_qcon = qcon;
_pos = _qcon;
_skipSpaceAndComments();
// QCON missing value
if (!*_pos)
{
errorMessage = "Expected value"sv;
_state = DecodeState::error;
}
}
inline DecodeState Decoder::step()
{
// Preserve error state
if (_state == DecodeState::error)
{
return _state;
}
// In container
if (_depth)
{
const bool inObject{bool(_stack & 1u)};
if (inObject)
{
if (_state != DecodeState::key)
{
// Check for end
if (*_pos == '}')
{
_ingestEnd();
return _state;
}
// Otherwise ingest key
else
{
// Ensure there was a comma between elements
if (_state != DecodeState::object && !_hadComma)
{
errorMessage = "Missing comma between object elements"sv;
return _state = DecodeState::error;
}
if (_consumeKey(key))
{
_skipSpaceAndComments();
return _state = DecodeState::key;
}
else
{
return _state = DecodeState::error;
}
}
}
}
else
{
// Check for end
if (*_pos == ']')
{
_ingestEnd();
return _state;
}
// Ensure there was a comma between elements
if (_state != DecodeState::array && !_hadComma)
{
errorMessage = "Missing comma between array elements"sv;
return _state = DecodeState::error;
}
}
}
_ingestValue();
return _state;
}
inline bool Decoder::more()
{
// Preserve error state
if (_state == DecodeState::error)
{
return false;
}
if (_depth)
{
if (_state == DecodeState::key)
{
return true;
}
// Check if we have end brace/bracket
const bool inObject{bool(_stack & 1u)};
if (*_pos == (inObject ? '}' : ']'))
{
_ingestEnd();
return false;
}
else
{
return true;
}
}
else
{
return _state == DecodeState::ready;
}
}
inline bool Decoder::finished() const
{
return _state != DecodeState::error && _state != DecodeState::ready && !_depth && !*_pos;
}
inline Decoder & Decoder::operator>>(const Container container)
{
// Stream container end
if (container == end)
{
if (more())
{
errorMessage = "There are more elements in the container"sv;
_state = DecodeState::error;
}
return *this;
}
// Stream container start
else
{
if (!_preValueStreamCheck())
{
return *this;
}
if (_consumeChar(container == Container::object ? '{' : '['))
{
_ingestStart(container);
}
else
{
_state = DecodeState::error;
}
return *this;
}
}
inline Decoder & Decoder::operator>>(std::string & v)
{
// Stream key
const bool inObject{bool(_stack & 1u)};
if (inObject && _state != DecodeState::key)
{
if (!_preKeyStreamCheck())
{
return *this;
}
if (_consumeKey(v))
{
_state = DecodeState::key;
_skipSpaceAndComments();
}
else
{
_state = DecodeState::error;
}
return *this;
}
// Stream value
else
{
if (!_preValueStreamCheck())
{
return *this;
}
if (_consumeChar('"'))
{
_postValue(_consumeString(v), DecodeState::string);
}
else
{
_state = DecodeState::error;
}
return *this;
}
}
inline Decoder & Decoder::operator>>(char & v)
{
const bool inObject{bool(_stack & 1u)};
const bool isKey{inObject && _state != DecodeState::key};
std::string & dst{isKey ? key : string};
if (*this >> dst)
{
if (dst.size() == 1u)
{
v = dst.front();
}
else
{
errorMessage = "Expected single character string"sv;
_state = DecodeState::error;
}
}
return *this;
}
inline Decoder & Decoder::operator>>(s64 & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
positive = _tryConsumeSign() >= 0;
if (_private::isDigit(*_pos) && !_isFloater(_pos + 1))
{
_postValue(_consumeInteger(v), DecodeState::integer);
}
else
{
errorMessage = "Expected integer"sv;
_state = DecodeState::error;
}
return *this;
}
inline Decoder & Decoder::operator>>(u64 & v)
{
s64 tmp;
if (!(*this >> tmp))
{
return *this;
}
// Ensure value is positive
if (positive)
{
v = u64(tmp);
}
else
{
errorMessage = "Cannot decode negative value into unsigned integer"sv;
_state = DecodeState::error;
}
return *this;
}
template <typename T>
inline void Decoder::_streamSmallerSignedInteger(T & v)
{
s64 v64;
if (!(*this >> v64))
{
return;
}
// Verify range
if (v64 >= std::numeric_limits<T>::min() && v64 <= std::numeric_limits<T>::max())
{
v = T(v64);
}
else
{
errorMessage = "Signed integer too large";
_state = DecodeState::error;
}
}
template <typename T>
inline void Decoder::_streamSmallerUnsignedInteger(T & v)
{
u64 v64;
*this >> v64;
// Verify range
if (v64 <= std::numeric_limits<T>::max())
{
v = T(v64);
}
else
{
errorMessage = "Unsigned integer too large";
_state = DecodeState::error;
}
}
inline Decoder & Decoder::operator>>(s32 & v)
{
_streamSmallerSignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(u32 & v)
{
_streamSmallerUnsignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(s16 & v)
{
_streamSmallerSignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(u16 & v)
{
_streamSmallerUnsignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(s8 & v)
{
_streamSmallerSignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(u8 & v)
{
_streamSmallerUnsignedInteger(v);
return *this;
}
inline Decoder & Decoder::operator>>(f64 & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
positive = _tryConsumeSign() >= 0;
if (_private::isDigit(*_pos) && _isFloater(_pos + 1))
{
_postValue(_consumeFloater(v), DecodeState::floater);
}
else if (_tryConsumeChars("inf"sv))
{
v = positive ? std::numeric_limits<f64>::infinity() : -std::numeric_limits<f64>::infinity();
_postValue(DecodeState::floater);
}
else if (_tryConsumeChars("nan"sv))
{
v = std::numeric_limits<f64>::quiet_NaN();
_postValue(DecodeState::floater);
}
else
{
errorMessage = "Expected floater"sv;
_state = DecodeState::error;
}
return *this;
}
inline Decoder & Decoder::operator>>(f32 & v)
{
f64 tmp;
if (*this >> tmp)
{
v = f32(tmp);
}
return *this;
}
inline Decoder & Decoder::operator>>(bool & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
if (_tryConsumeChars("true"))
{
v = true;
_postValue(DecodeState::boolean);
}
else if (_tryConsumeChars("false"))
{
v = false;
_postValue(DecodeState::boolean);
}
else
{
errorMessage = "Expected boolean"sv;
_state = DecodeState::error;
}
return *this;
}
inline Decoder & Decoder::operator>>(Date & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
_postValue(_consumeChar('D') && _consumeDate(v), DecodeState::date);
return *this;
}
inline Decoder & Decoder::operator>>(Time & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
_postValue(_consumeChar('T') && _consumeTime(v), DecodeState::time);
return *this;
}
inline Decoder & Decoder::operator>>(Datetime & v)
{
if (!_preValueStreamCheck())
{
return *this;
}
_postValue(_consumeChar('D') && _consumeDate(v.date) && _consumeChar('T') && _consumeTime(v.time) && _consumeTimezone(v.zone), DecodeState::datetime);
return *this;
}
inline Decoder & Decoder::operator>>(Timepoint & v)
{
if (*this >> datetime)
{
v = datetime.toTimepoint();
}
return *this;
}
inline Decoder & Decoder::operator>>(nullptr_t)
{
if (!_preValueStreamCheck())
{
return *this;
}
_postValue(_consumeChars("null"sv), DecodeState::null);
return *this;
}
inline void Decoder::_reset()
{
_state = DecodeState::error;
_qcon = nullptr;
_pos = nullptr;
_stack = 0u;
_depth = 0u;
_cachedEnd = nullptr;
_hadComma = false;
}
inline void Decoder::_skipSpace()
{
while (_isSpace(*_pos)) ++_pos;
}
inline void Decoder::_skipSpaceAndComments()
{
_skipSpace();
// Skip comments and space
while (*_pos == '#')
{
// Skip `#`
++_pos;
// Skip rest of line
while (*_pos && *_pos != '\n') ++_pos;
_skipSpace();
}
}
inline bool Decoder::_preValueStreamCheck()
{
// Preserve error state
if (_state == DecodeState::error)
{
return false;
}
if (_depth)
{
const bool inObject{bool(_stack & 1u)};
if (inObject)
{
// Ensure we have key if in object
if (_state != DecodeState::key)
{
errorMessage = "Expected key"sv;
_state = DecodeState::error;
return false;
}
}
else
{
// Ensure there is a comma between array elements
if (_state != DecodeState::array && !_hadComma)
{
errorMessage = "Expected comma"sv;
_state = DecodeState::error;
return false;
}
}
}
else
{
// Ensure only one value at root level
if (_state != DecodeState::ready)
{
errorMessage = "Root may only have a single value"sv;
_state = DecodeState::error;
return false;
}
}
return true;
}
inline bool Decoder::_preKeyStreamCheck()
{
// Already know we're in object and don't already have key
// Preserve error state
if (_state == DecodeState::error)
{
return false;
}
// Ensure there is a comma between elements
if (_state != DecodeState::object && !_hadComma)
{
errorMessage = "Expected comma"sv;
_state = DecodeState::error;
return false;
}
return true;
}
inline void Decoder::_postValue(const DecodeState newState)
{
_state = newState;
_skipSpaceAndComments();
if (_depth)
{
// Ingest comma
_hadComma = _tryConsumeChar(',');
if (_hadComma)
{
_skipSpaceAndComments();
}
}
else
{
// Ensure there is nothing else at root level
if (*_pos)
{
errorMessage = "Extraneous root content"sv;
_state = DecodeState::error;
}
}
}
inline void Decoder::_postValue(const bool condition, const DecodeState newState)
{
if (condition)
{
_postValue(newState);
}
else
{
_state = DecodeState::error;
}
}
inline s32 Decoder::_tryConsumeSign()
{
const s32 sign{(*_pos == '+') - (*_pos == '-')};
if (sign)
{
++_pos;
}
return sign;
}
inline bool Decoder::_tryConsumeChar(const char c)
{
if (*_pos == c)
{
++_pos;
return true;
}
else
{
return false;
}
}
inline bool Decoder::_tryConsumeChars(const std::string_view str)
{
const char * const start{_pos};
for (const char c : str)
{
if (*_pos != c)
{
_pos = start;