-
Notifications
You must be signed in to change notification settings - Fork 8
/
jsonparser.d
1326 lines (1232 loc) · 39.5 KB
/
jsonparser.d
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
/++
JSON Parsing API
Copyright: Tamedia Digital, 2016-2017
Authors: Ilia Ki
License: MIT
Macros:
SUBMODULE = $(LINK2 asdf_$1.html, asdf.$1)
SUBREF = $(LINK2 asdf_$1.html#.$2, $(TT $2))$(NBSP)
T2=$(TR $(TDNW $(LREF $1)) $(TD $+))
T4=$(TR $(TDNW $(LREF $1)) $(TD $2) $(TD $3) $(TD $4))
+/
module asdf.jsonparser;
import asdf.asdf;
import asdf.outputarray;
import std.experimental.allocator.gc_allocator;
import std.meta;
import std.range.primitives;
import std.traits;
import std.typecons;
import mir.serde: SerdeException;
version(LDC)
{
import ldc.attributes: optStrategy;
enum minsize = optStrategy("minsize");
static if (__traits(targetHasFeature, "sse4.2"))
{
import core.simd;
import ldc.simd;
import ldc.gccbuiltins_x86;
version = SSE42;
}
}
else
{
enum minsize;
}
version(X86_64)
version = X86_Any;
else
version(X86)
version = X86_Any;
private alias ASDFGCAllocator = typeof(GCAllocator.instance);
/++
Parses json value
Params:
chunks = input range composed of elements type of `const(ubyte)[]`.
`chunks` can use the same buffer for each chunk.
initLength = initial output buffer length. Minimum value is 32.
Returns:
ASDF value
+/
Asdf parseJson(
Flag!"includingNewLine" includingNewLine = Yes.includingNewLine,
Flag!"spaces" spaces = Yes.spaces,
Chunks)
(Chunks chunks, size_t initLength = 32)
if(is(ElementType!Chunks : const(ubyte)[]))
{
enum assumeValid = false;
auto parser = jsonParser!(includingNewLine, spaces, assumeValid)(ASDFGCAllocator.instance, chunks);
return parseJson(parser);
}
///
unittest
{
import std.range: chunks;
auto text = cast(const ubyte[])`true `;
auto ch = text.chunks(3);
assert(ch.parseJson(32).data == [1]);
}
/++
Parses json value
Params:
str = input string
allocator = (optional) memory allocator
Returns:
ASDF value
+/
Asdf parseJson(
Flag!"includingNewLine" includingNewLine = Yes.includingNewLine,
Flag!"spaces" spaces = Yes.spaces,
Flag!"assumeValid" assumeValid = No.assumeValid,
Allocator,
)
(in char[] str, auto ref Allocator allocator)
{
auto parser = jsonParser!(includingNewLine, spaces, assumeValid)(allocator, str);
return parseJson(parser);
}
///
@system unittest {
import std.experimental.allocator.mallocator: Mallocator;
import std.experimental.allocator.showcase: StackFront;
StackFront!(1024, Mallocator) allocator;
auto json = parseJson(`{"ak": {"sub": "subval"} }`, allocator);
assert(json["ak", "sub"] == "subval");
}
/// Faulty location
pure unittest
{
import asdf;
try
{
auto data = `[1, 2, ]`.parseJson;
}
catch(AsdfSerdeException e)
{
import std.conv;
/// zero based index
assert(e.location == 7);
return;
}
assert(0);
}
/// ditto
Asdf parseJson(
Flag!"includingNewLine" includingNewLine = Yes.includingNewLine,
Flag!"spaces" spaces = Yes.spaces,
Flag!"assumeValid" assumeValid = No.assumeValid,
)
(in char[] str)
{
auto parser = jsonParser!(includingNewLine, spaces, assumeValid)(&ASDFGCAllocator.instance, str);
return parseJson(parser);
}
///
unittest
{
assert(`{"ak": {"sub": "subval"} }`.parseJson["ak", "sub"] == "subval");
}
private Asdf parseJson(Parser)(ref Parser parser) {
size_t location;
if (parser.parse(location))
throw new AsdfSerdeException(parser.lastError, location);
return Asdf(parser.result);
}
deprecated("please remove the initBufferLength argument (latest)")
auto parseJsonByLine(
Flag!"spaces" spaces = Yes.spaces,
Input)
(Input input, sizediff_t initBufferLength)
{
return .parseJsonByLine!(spaces, No.throwOnInvalidLines, Input)(input);
}
/++
Parses JSON value in each line from a Range of buffers.
Params:
spaces = adds support for spaces beetwen json tokens. Default value is Yes.
throwOnInvalidLines = throws an $(LREF SerdeException) on invalid lines if Yes and ignore invalid lines if No. Default value is No.
input = input range composed of elements type of `const(ubyte)[]` or string / const(char)[].
`chunks` can use the same buffer for each chunk.
Returns:
Input range composed of ASDF values. Each value uses the same internal buffer.
+/
auto parseJsonByLine(
Flag!"spaces" spaces = Yes.spaces,
Flag!"throwOnInvalidLines" throwOnInvalidLines = No.throwOnInvalidLines,
Input)
(Input input)
{
alias Parser = JsonParser!(false, cast(bool)spaces, false, ASDFGCAllocator, Input);
struct ByLineValue
{
Parser parser;
private bool _empty, _nextEmpty;
void popFront()
{
for(;;)
{
assert(!empty);
if(_nextEmpty)
{
_empty = true;
return;
}
// parser.oa.shift = 0;
parser.dataLength = 0;
auto error = parser.parse;
if(!error)
{
auto t = parser.skipSpaces_;
if(t != '\n' && t != 0)
{
error = AsdfErrorCode.unexpectedValue;
parser._lastError = "expected new line or end of input";
}
else
if(t == 0)
{
_nextEmpty = true;
return;
}
else
{
parser.skipNewLine;
_nextEmpty = !parser.skipSpaces_;
return;
}
}
static if (throwOnInvalidLines)
throw new SerdeException(parser.lastError);
else
parser.skipLine();
}
}
auto front() @property
{
assert(!empty);
return Asdf(parser.result);
}
bool empty()
{
return _empty;
}
}
ByLineValue ret;
if(input.empty)
{
ret._empty = ret._nextEmpty = true;
}
else
{
ret = ByLineValue(Parser(ASDFGCAllocator.instance, input));
ret.popFront;
}
return ret;
}
version(LDC)
{
public import ldc.intrinsics: _expect = llvm_expect;
}
else
{
T _expect(T)(T val, T expected_val) if (__traits(isIntegral, T))
{
return val;
}
}
enum AsdfErrorCode
{
success,
unexpectedEnd,
unexpectedValue,
}
private __gshared immutable ubyte[256] parseFlags = [
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0,0,0,0,0,0,0,0, 0,6,2,0,0,6,0,0, // 0
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, // 1
7,1,0,1,1,1,1,1, 1,1,1,9,1,9,9,1, // 2
9,9,9,9,9,9,9,9, 9,9,1,1,1,1,1,1, // 3
1,1,1,1,1,9,1,1, 1,1,1,1,1,1,1,1, // 4
1,1,1,1,1,1,1,1, 1,1,1,1,0,1,1,1, // 5
1,1,1,1,1,9,1,1, 1,1,1,1,1,1,1,1, // 6
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, // 7
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
];
private __gshared immutable byte[256] uniFlags = [
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 0
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 1
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 2
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, // 3
-1,10,11,12,13,14,15,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 4
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 5
-1,10,11,12,13,14,15,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 6
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, // 7
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
];
pragma(inline, true)
bool isPlainJsonCharacter()(size_t c)
{
return (parseFlags[c] & 1) != 0;
}
pragma(inline, true)
bool isJsonWhitespace()(size_t c)
{
return (parseFlags[c] & 2) != 0;
}
pragma(inline, true)
bool isJsonLineWhitespace()(size_t c)
{
return (parseFlags[c] & 4) != 0;
}
pragma(inline, true)
bool isJsonNumber()(size_t c)
{
return (parseFlags[c] & 8) != 0;
}
package auto assumePure(T)(T t)
if (isFunctionPointer!T || isDelegate!T)
{
enum attrs = functionAttributes!T | FunctionAttribute.pure_;
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}
package auto callPure(alias fn,T...)(T args)
{
auto fp = assumePure(&fn);
return (*fp)(args);
}
/+
Fast picewise stack
+/
private struct Stack
{
import core.stdc.stdlib: cmalloc = malloc, cfree = free;
@disable this(this);
struct Node
{
enum length = 32; // 2 power
Node* prev;
size_t* buff;
}
size_t[Node.length] buffer = void;
size_t length = 0;
Node node;
pure:
void push()(size_t value)
{
version(LDC)
pragma(inline, true);
immutable local = length++ & (Node.length - 1);
if (local)
{
node.buff[local] = value;
}
else
if (length == 1)
{
node = Node(null, buffer.ptr);
buffer[0] = value;
}
else
{
auto prevNode = cast(Node*) callPure!cmalloc(Node.sizeof);
*prevNode = node;
node.prev = prevNode;
node.buff = cast(size_t*) callPure!cmalloc(Node.length * size_t.sizeof);
node.buff[0] = value;
}
}
size_t top()()
{
version(LDC)
pragma(inline, true);
assert(length);
immutable local = (length - 1) & (Node.length - 1);
return node.buff[local];
}
size_t pop()()
{
version(LDC)
pragma(inline, true);
assert(length);
immutable local = --length & (Node.length - 1);
immutable ret = node.buff[local];
if (local == 0)
{
if (node.buff != buffer.ptr)
{
callPure!cfree(node.buff);
node = *node.prev;
}
}
return ret;
}
pragma(inline, false)
void free()()
{
version(LDC)
pragma(inline, true);
if (node.buff is null)
return;
while(node.buff !is buffer.ptr)
{
callPure!cfree(node.buff);
node = *node.prev;
}
}
}
unittest
{
Stack stack;
assert(stack.length == 0);
foreach(i; 1 .. 100)
{
stack.push(i);
assert(stack.length == i);
assert(stack.top() == i);
}
foreach_reverse(i; 1 .. 100)
{
assert(stack.length == i);
assert(stack.pop() == i);
}
assert(stack.length == 0);
}
///
auto jsonParser(bool includingNewLine, bool hasSpaces, bool assumeValid, Allocator, Input = const(ubyte)[])(auto ref Allocator allocator, Input input)
if (!isPointer!Allocator)
{
return JsonParser!(includingNewLine, hasSpaces, assumeValid, Allocator, Input)(allocator, input);
}
///
auto jsonParser(bool includingNewLine, bool hasSpaces, bool assumeValid, Allocator, Input = const(ubyte)[])(Allocator* allocator, Input input) {
return JsonParser!(includingNewLine, hasSpaces, assumeValid, Allocator, Input)(allocator, input);
}
///
struct JsonParser(bool includingNewLine, bool hasSpaces, bool assumeValid, Allocator, Input = const(ubyte)[])
{
ubyte[] data;
Allocator* allocator;
Input input;
static if (chunked)
ubyte[] front;
else
alias front = input;
size_t dataLength;
string _lastError;
enum bool chunked = !is(Input : const(char)[]);
this(ref Allocator allocator, Input input)
{
this.input = input;
this.allocator = &allocator;
}
this(Allocator* allocator, Input input)
{
this.input = input;
this.allocator = allocator;
}
bool prepareInput_()()
{
static if (chunked)
{
if (front.length == 0)
{
assert(!input.empty);
input.popFront;
if (input.empty)
return false;
front = cast(typeof(front)) input.front;
}
}
return front.length != 0;
}
void skipNewLine()()
{
assert(front.length);
assert(front[0] == '\n');
front = front[1 .. $];
}
char skipSpaces_()()
{
static if (hasSpaces)
for(;;)
{
if (prepareInput_ == false)
return 0;
static if (includingNewLine)
alias isWhite = isJsonWhitespace;
else
alias isWhite = isJsonLineWhitespace;
if (isWhite(front[0]))
{
front = front[1 .. $];
continue;
}
return front[0];
}
else
{
if (prepareInput_ == false)
return 0;
return front[0];
}
}
bool skipLine()()
{
for(;;)
{
if (_expect(!prepareInput_, false))
return false;
auto c = front[0];
front = front[1 .. $];
if (c == '\n')
return true;
}
}
auto result()()
{
return data[0 .. dataLength];
}
string lastError()() @property
{
return _lastError;
}
AsdfErrorCode parse()
{
size_t location;
return parse(location);
}
static if(is(Allocator == ASDFGCAllocator))
{
auto parse(out size_t location) @trusted
{
return parseImpl(location);
}
}
else
{
auto parse(out size_t location)
{
return parseImpl(location);
}
}
pragma(inline, false)
private AsdfErrorCode parseImpl(out size_t location)
{
version(SSE42)
{
enum byte16 str2E = [
'\u0001', '\u001F',
'\"', '\"',
'\\', '\\',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'];
enum byte16 num2E = ['+', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '\0'];
byte16 str2 = str2E;
byte16 num2 = num2E;
}
const(ubyte)* strPtr;
const(ubyte)* strEnd;
ubyte* dataPtr;
ubyte* stringAndNumberShift = void;
static if (chunked)
{
bool prepareInput()()
{
pragma(inline, false);
if(strPtr)
{
location += front.length;
input.popFront;
if (input.empty)
{
return false;
}
}
front = cast(typeof(front)) input.front;
if (front.length == 0)
return false;
strPtr = front.ptr;
strEnd = front.ptr + front.length;
const dataAddLength = front.length * 6;
const dataLength = dataPtr - data.ptr;
const dataRequiredLength = dataLength + dataAddLength;
if (data.length < dataRequiredLength)
{
const valueLength = stringAndNumberShift - dataPtr;
import std.algorithm.comparison: max;
const len = max(data.length * 2, dataRequiredLength);
allocator.reallocate(*cast(void[]*)&data, len);
dataPtr = data.ptr + dataLength;
stringAndNumberShift = dataPtr + valueLength;
}
return true;
}
strPtr = front.ptr;
strEnd = front.ptr + front.length;
}
else
{
strPtr = cast(const(ubyte)*) input.ptr;
strEnd = cast(const(ubyte)*) input.ptr + input.length;
enum bool prepareInput = false;
}
auto rl = (strEnd - strPtr) * 6;
if (data.ptr !is null && data.length < rl)
{
allocator.deallocate(data);
data = null;
}
if (data.ptr is null)
{
data = cast(ubyte[])allocator.allocate(rl);
}
dataPtr = data.ptr;
bool skipSpaces()()
{
version(LDC)
pragma(inline, true);
static if (includingNewLine)
alias isWhite = isJsonWhitespace;
else
alias isWhite = isJsonLineWhitespace;
F:
{
if (_expect(strEnd != strPtr, true))
{
L:
static if (hasSpaces)
{
if (isWhite(strPtr[0]))
{
strPtr++;
goto F;
}
}
return true;
}
else
{
if (prepareInput)
goto L;
return false;
}
}
}
@minsize
int readUnicode()(ref dchar d)
{
version(LDC)
pragma(inline, true);
uint e = 0;
size_t i = 4;
do
{
if (strEnd == strPtr && !prepareInput)
return 1;
int c = uniFlags[*strPtr++];
assert(c < 16);
if (c == -1)
return -1;
assert(c >= 0);
e <<= 4;
e ^= c;
}
while(--i);
d = e;
return 0;
}
Stack stack;
typeof(return) retCode;
bool currIsKey = void;
size_t stackValue = void;
goto value;
/////////// RETURN
ret:
front = front[cast(typeof(front.ptr)) strPtr - front.ptr .. $];
dataLength = dataPtr - data.ptr;
assert(stack.length == 0);
ret_final:
return retCode;
///////////
key:
if (!skipSpaces)
goto object_key_unexpectedEnd;
key_start:
if (*strPtr != '"')
goto object_key_start_unexpectedValue;
currIsKey = true;
stringAndNumberShift = dataPtr;
// reserve 1 byte for the length
dataPtr += 1;
goto string;
next:
if (stack.length == 0)
goto ret;
{
if (!skipSpaces)
goto next_unexpectedEnd;
stackValue = stack.top;
const isObject = stackValue & 1;
auto v = *strPtr++;
if (isObject)
{
if (v == ',')
goto key;
if (v != '}')
goto next_unexpectedValue;
}
else
{
if (v == ',')
goto value;
if (v != ']')
goto next_unexpectedValue;
}
}
structure_end: {
stackValue = stack.pop();
const structureShift = stackValue >> 1;
const structureLengthPtr = data.ptr + structureShift;
const size_t structureLength = dataPtr - structureLengthPtr - 4;
if (structureLength > uint.max)
goto object_or_array_is_to_large;
version(X86_Any)
*cast(uint*) structureLengthPtr = cast(uint) structureLength;
else
*cast(ubyte[4]*) structureLengthPtr = cast(ubyte[4]) cast(uint[1]) [cast(uint) structureLength];
goto next;
}
value:
if (!skipSpaces)
goto value_unexpectedEnd;
value_start:
switch(*strPtr)
{
stringValue:
case '"':
currIsKey = false;
*dataPtr++ = Asdf.Kind.string;
stringAndNumberShift = dataPtr;
// reserve 4 byte for the length
dataPtr += 4;
goto string;
case '-':
case '0':
..
case '9': {
*dataPtr++ = Asdf.Kind.number;
stringAndNumberShift = dataPtr;
// reserve 1 byte for the length
dataPtr++; // write the first character
*dataPtr++ = *strPtr++;
for(;;)
{
if (strEnd == strPtr && !prepareInput)
goto number_found;
version(SSE42)
{
while (strEnd >= strPtr + 16)
{
byte16 str1 = loadUnaligned!byte16(cast(byte*)strPtr);
size_t ecx = __builtin_ia32_pcmpistri128(num2, str1, 0x10);
storeUnaligned!byte16(str1, cast(byte*)dataPtr);
strPtr += ecx;
dataPtr += ecx;
if(ecx != 16)
goto number_found;
}
}
else
{
while(strEnd >= strPtr + 4)
{
char c0 = strPtr[0]; dataPtr += 4; if (!isJsonNumber(c0)) goto number_found0;
char c1 = strPtr[1]; dataPtr[-4] = c0; if (!isJsonNumber(c1)) goto number_found1;
char c2 = strPtr[2]; dataPtr[-3] = c1; if (!isJsonNumber(c2)) goto number_found2;
char c3 = strPtr[3]; dataPtr[-2] = c2; if (!isJsonNumber(c3)) goto number_found3;
strPtr += 4; dataPtr[-1] = c3;
}
}
while(strEnd > strPtr)
{
char c0 = strPtr[0]; if (!isJsonNumber(c0)) goto number_found; dataPtr[0] = c0;
strPtr += 1;
dataPtr += 1;
}
}
version(SSE42){} else
{
number_found3: dataPtr++; strPtr++;
number_found2: dataPtr++; strPtr++;
number_found1: dataPtr++; strPtr++;
number_found0: dataPtr -= 4;
}
number_found:
auto numberLength = dataPtr - stringAndNumberShift - 1;
if (numberLength > ubyte.max)
goto number_length_unexpectedValue;
*stringAndNumberShift = cast(ubyte) numberLength;
goto next;
}
case '{':
strPtr++;
*dataPtr++ = Asdf.Kind.object;
stack.push(((dataPtr - data.ptr) << 1) ^ 1);
dataPtr += 4;
if (!skipSpaces)
goto object_first_value_start_unexpectedEnd;
if (*strPtr != '}')
goto key_start;
strPtr++;
goto structure_end;
case '[':
strPtr++;
*dataPtr++ = Asdf.Kind.array;
stack.push(((dataPtr - data.ptr) << 1) ^ 0);
dataPtr += 4;
if (!skipSpaces)
goto array_first_value_start_unexpectedEnd;
if (*strPtr != ']')
goto value_start;
strPtr++;
goto structure_end;
foreach (name; AliasSeq!("false", "null", "true"))
{
case name[0]:
if (_expect(strEnd - strPtr >= name.length, true))
{
static if (!assumeValid)
{
version(X86_Any)
{
enum uint referenceValue =
(uint(name[$ - 4]) << 0x00) ^
(uint(name[$ - 3]) << 0x08) ^
(uint(name[$ - 2]) << 0x10) ^
(uint(name[$ - 1]) << 0x18);
if (*cast(uint*)(strPtr + bool(name.length == 5)) != referenceValue)
{
static if (name == "true")
goto true_unexpectedValue;
else
static if (name == "false")
goto false_unexpectedValue;
else
goto null_unexpectedValue;
}
}
else
{
char[name.length - 1] c = void;
import std.range: iota;
foreach (i; aliasSeqOf!(iota(1, name.length)))
c[i - 1] = strPtr[i];
foreach (i; aliasSeqOf!(iota(1, name.length)))
{
if (c[i - 1] != name[i])
{
static if (name == "true")
goto true_unexpectedValue;
else
static if (name == "false")
goto false_unexpectedValue;
else
goto null_unexpectedValue;
}
}
}
}
static if (name == "null")
*dataPtr++ = Asdf.Kind.null_;
else
static if (name == "false")
*dataPtr++ = Asdf.Kind.false_;
else
*dataPtr++ = Asdf.Kind.true_;
strPtr += name.length;
goto next;
}
else
{
strPtr += 1;
foreach (i; 1 .. name.length)
{
if (strEnd == strPtr && !prepareInput)
{
static if (name == "true")
goto true_unexpectedEnd;
else
static if (name == "false")
goto false_unexpectedEnd;
else
goto null_unexpectedEnd;
}
static if (!assumeValid)
{
if (_expect(strPtr[0] != name[i], false))
{
static if (name == "true")
goto true_unexpectedValue;
else
static if (name == "false")
goto false_unexpectedValue;
else
goto null_unexpectedValue;
}
}
strPtr++;
}
static if (name == "null")
*dataPtr++ = Asdf.Kind.null_;
else
static if (name == "false")
*dataPtr++ = Asdf.Kind.false_;
else
*dataPtr++ = Asdf.Kind.true_;
goto next;
}
}
default: goto value_unexpectedStart;
}
string:
debug assert(*strPtr == '"', "Internal ASDF logic error. Please report an issue.");
strPtr += 1;
StringLoop: {
for(;;)
{
if (strEnd == strPtr && !prepareInput)
goto string_unexpectedEnd;
version(SSE42)
{