@@ -39,22 +39,23 @@ struct JSONArray
39
39
}
40
40
private void append (T[] values )
41
41
{
42
+ import core.stdc.string ;
42
43
if (actualLength + values .length <= N)
43
- staticData.ptr[ actualLength.. actualLength + values .length] = values [] ;
44
+ memcpy( staticData.ptr + actualLength, values .ptr, values .length * T.sizeof) ;
44
45
else
45
46
{
46
47
import std.array :uninitializedArray;
47
48
if (dynData is null )
48
49
{
49
50
dynData = uninitializedArray! (T[])(actualLength+ values .length);
50
- dynData.ptr[ 0 .. actualLength] = staticData.ptr[ 0 .. actualLength] ;
51
+ memcpy( dynData.ptr, staticData.ptr, actualLength * T.sizeof) ;
51
52
}
52
53
else if (dynData.length < actualLength + values .length)
53
54
{
54
55
size_t newSize = actualLength+ values .length > actualLength* 2 ? actualLength+ values .length : actualLength* 2 ;
55
56
dynData.length = newSize;
56
57
}
57
- dynData.ptr[ actualLength.. actualLength + values .length] = values [] ;
58
+ memcpy( dynData.ptr + actualLength, values .ptr, values .length * T.sizeof) ;
58
59
}
59
60
actualLength+= values .length;
60
61
}
@@ -145,8 +146,8 @@ bool isWhitespace(char ch)
145
146
}
146
147
}
147
148
148
- pragma (inline, true ) bool isNumber(char ch){return ch >= ' 0' && ch <= ' 9' ;}
149
- pragma (inline, true ) bool isNumeric(char ch){return (ch >= ' 0' && ch <= ' 9' ) || ch == ' -' || ch == ' .' ;}
149
+ pragma (inline, true ) bool isNumber(char ch){return ' 0' <= ch && ch <= ' 9' ;}
150
+ pragma (inline, true ) bool isNumeric(char ch){return (' 0' <= ch && ch <= ' 9' ) || ch == ' -' || ch == ' .' ;}
150
151
151
152
struct JSONValue
152
153
{
@@ -374,52 +375,6 @@ struct JSONValue
374
375
ptrdiff_t index = 0 ;
375
376
StringPool pool = StringPool(cast (size_t )(data.length* 0.75 ));
376
377
377
- // bool getNextString(string data, ptrdiff_t currentIndex, out ptrdiff_t newIndex, out string theString)
378
- // {
379
- // import std.array;
380
- // assert(data[currentIndex] == '"');
381
- // ptrdiff_t i = currentIndex + 1;
382
- // size_t returnLength = 0;
383
- // // char[] ret = uninitializedArray!(char[])(128);
384
- // char[] ret = pool.getNewString(64);
385
- // char ch;
386
-
387
- // ubyte[4] chars;
388
- // bool escaped;
389
- // LOOP: while(i < data.length)
390
- // {
391
- // ubyte count = nextByte32(chars, data, i);
392
- // for(size_t byteIndex = 0; byteIndex < count; byteIndex++)
393
- // {
394
- // ch = cast(char)chars[byteIndex];
395
- // if(!escaped)
396
- // {
397
- // if(ch == '"')
398
- // {
399
- // i+= byteIndex;
400
- // break LOOP;
401
- // }
402
- // else if(ch == '\\')
403
- // {
404
- // escaped = true;
405
- // continue;
406
- // }
407
- // }
408
- // if(returnLength >= ret.length)
409
- // ret = pool.resizeString(ret, ret.length*2);
410
- // ret[returnLength++] = cast(char)chars[byteIndex];
411
- // escaped = false;
412
- // }
413
- // i+= count;
414
- // }
415
- // ret = pool.resizeString(ret, returnLength);
416
- // newIndex = i;
417
- // if(newIndex == data.length) //Not found
418
- // return false;
419
- // theString = cast(string)ret;
420
- // return true;
421
- // }
422
-
423
378
bool getNextString (string data, ptrdiff_t currentIndex, out ptrdiff_t newIndex, out string theString)
424
379
{
425
380
assert (data[currentIndex] == ' "' , " getNextString must start with a quotation mark" );
@@ -433,41 +388,22 @@ struct JSONValue
433
388
ch = data[i];
434
389
switch (ch)
435
390
{
436
- case ' "' :
437
- {
391
+ case ' "' :
438
392
ret = pool.resizeString(ret, returnLength);
439
393
newIndex = i;
440
394
theString = cast (string )ret;
441
395
return true ;
442
- }
443
396
case ' \\ ' :
444
- if (i + 1 < data.length)
445
- {
446
- i++ ;
447
- switch (data[i])
448
- {
449
- case ' n' :
450
- ch = ' \n ' ;
451
- break ;
452
- case ' t' :
453
- ch = ' \t ' ;
454
- break ;
455
- case ' r' :
456
- ch = ' \r ' ;
457
- break ;
458
- default :
459
- ch = data[i];
460
- break ;
461
- }
462
- }
463
- else
397
+ if (i + 1 >= data.length)
464
398
return false ;
399
+ ch = escapedCharacter(data[++ i]);
465
400
break ;
466
401
default : break ;
402
+
467
403
}
468
404
if (returnLength >= ret.length)
469
405
ret = pool.resizeString(ret, ret.length* 2 );
470
-
406
+
471
407
ret[returnLength++ ] = ch;
472
408
i++ ;
473
409
}
@@ -482,23 +418,17 @@ struct JSONValue
482
418
newIndex = currentIndex;
483
419
if (data[currentIndex] == ' -' )
484
420
newIndex++ ;
485
- if (data[newIndex] == ' .' )
486
- {
487
- hasDecimal = true ;
488
- newIndex++ ;
489
- }
490
421
491
422
while (newIndex < data.length)
492
423
{
493
424
if (! hasDecimal && data[newIndex] == ' .' )
494
425
{
495
- if ( ! hasDecimal) hasDecimal = true ;
426
+ hasDecimal = true ;
496
427
if (newIndex+ 1 < data.length) newIndex++ ;
497
428
}
498
- if (isNumber(data[newIndex]))
499
- newIndex++ ;
500
- else
429
+ if (! isNumber(data[newIndex]))
501
430
break ;
431
+ newIndex++ ;
502
432
}
503
433
if (hasDecimal)
504
434
{
@@ -917,7 +847,7 @@ private struct StringPool
917
847
else
918
848
{
919
849
ptrdiff_t offset = str.ptr - pool.ptr;
920
- assert (offset > 0 , " Out of bounds?" );
850
+ assert (offset >= 0 , " Out of bounds?" );
921
851
used+= newSize - str.length;
922
852
return pool[cast (size_t )offset.. offset+ newSize];
923
853
}
@@ -1066,4 +996,18 @@ unittest
1066
996
]
1067
997
}` ;
1068
998
assert (parseJSON(json)[" D5F04185E96CC720" ].array[1 ].array[0 ].toString == ` "Second Value"` );
1069
- }
999
+ }
1000
+
1001
+
1002
+ pragma (inline, true )
1003
+ private char escapedCharacter (char a)
1004
+ {
1005
+ switch (a)
1006
+ {
1007
+ case ' n' : return ' \n ' ;
1008
+ case ' t' : return ' \t ' ;
1009
+ case ' b' : return ' \b ' ;
1010
+ case ' r' : return ' \r ' ;
1011
+ default : return a;
1012
+ }
1013
+ }
0 commit comments