Skip to content

Commit b43286a

Browse files
committed
Update: Minor improvement on hipjson
1 parent 113784a commit b43286a

File tree

1 file changed

+30
-86
lines changed

1 file changed

+30
-86
lines changed

hipjson/source/hipjson.d

+30-86
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,23 @@ struct JSONArray
3939
}
4040
private void append(T[] values)
4141
{
42+
import core.stdc.string;
4243
if(actualLength + values.length <= N)
43-
staticData.ptr[actualLength..actualLength+values.length] = values[];
44+
memcpy(staticData.ptr + actualLength, values.ptr, values.length * T.sizeof);
4445
else
4546
{
4647
import std.array:uninitializedArray;
4748
if(dynData is null)
4849
{
4950
dynData = uninitializedArray!(T[])(actualLength+values.length);
50-
dynData.ptr[0..actualLength] = staticData.ptr[0..actualLength];
51+
memcpy(dynData.ptr, staticData.ptr, actualLength * T.sizeof);
5152
}
5253
else if (dynData.length < actualLength + values.length)
5354
{
5455
size_t newSize = actualLength+values.length > actualLength*2 ? actualLength+values.length : actualLength*2;
5556
dynData.length = newSize;
5657
}
57-
dynData.ptr[actualLength..actualLength+values.length] = values[];
58+
memcpy(dynData.ptr + actualLength, values.ptr, values.length * T.sizeof);
5859
}
5960
actualLength+= values.length;
6061
}
@@ -145,8 +146,8 @@ bool isWhitespace(char ch)
145146
}
146147
}
147148

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 == '.';}
150151

151152
struct JSONValue
152153
{
@@ -374,52 +375,6 @@ struct JSONValue
374375
ptrdiff_t index = 0;
375376
StringPool pool = StringPool(cast(size_t)(data.length*0.75));
376377

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-
423378
bool getNextString(string data, ptrdiff_t currentIndex, out ptrdiff_t newIndex, out string theString)
424379
{
425380
assert(data[currentIndex] == '"', "getNextString must start with a quotation mark");
@@ -433,41 +388,22 @@ struct JSONValue
433388
ch = data[i];
434389
switch(ch)
435390
{
436-
case '"':
437-
{
391+
case '"':
438392
ret = pool.resizeString(ret, returnLength);
439393
newIndex = i;
440394
theString = cast(string)ret;
441395
return true;
442-
}
443396
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)
464398
return false;
399+
ch = escapedCharacter(data[++i]);
465400
break;
466401
default: break;
402+
467403
}
468404
if(returnLength >= ret.length)
469405
ret = pool.resizeString(ret, ret.length*2);
470-
406+
471407
ret[returnLength++] = ch;
472408
i++;
473409
}
@@ -482,23 +418,17 @@ struct JSONValue
482418
newIndex = currentIndex;
483419
if(data[currentIndex] == '-')
484420
newIndex++;
485-
if(data[newIndex] == '.')
486-
{
487-
hasDecimal = true;
488-
newIndex++;
489-
}
490421

491422
while(newIndex < data.length)
492423
{
493424
if(!hasDecimal && data[newIndex] == '.')
494425
{
495-
if(!hasDecimal) hasDecimal = true;
426+
hasDecimal = true;
496427
if(newIndex+1 < data.length) newIndex++;
497428
}
498-
if(isNumber(data[newIndex]))
499-
newIndex++;
500-
else
429+
if(!isNumber(data[newIndex]))
501430
break;
431+
newIndex++;
502432
}
503433
if(hasDecimal)
504434
{
@@ -917,7 +847,7 @@ private struct StringPool
917847
else
918848
{
919849
ptrdiff_t offset = str.ptr - pool.ptr;
920-
assert(offset > 0, " Out of bounds?");
850+
assert(offset >= 0, " Out of bounds?");
921851
used+= newSize - str.length;
922852
return pool[cast(size_t)offset..offset+newSize];
923853
}
@@ -1066,4 +996,18 @@ unittest
1066996
]
1067997
}`;
1068998
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

Comments
 (0)