Skip to content

Commit 69868ac

Browse files
committed
optimise bigint parsing
1 parent fba267f commit 69868ac

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

source/mir/bignum/internal/parse.d

+35-8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ SmallDecimalParsingResult parseJsonNumberImpl()(scope const(char)[] str)
8989
return result;
9090
}
9191

92+
version(mir_test)
9293
unittest
9394
{
9495
import mir.test;
@@ -99,6 +100,19 @@ unittest
99100
res.coefficient.should == 1234567890;
100101
}
101102

103+
version(mir_test)
104+
unittest
105+
{
106+
import mir.test;
107+
auto res = "-12345.67890e-10".parseJsonNumberImpl;
108+
res.key.should == DecimalExponentKey.e;
109+
res.sign.should == true;
110+
res.exponent.should == -15;
111+
res.coefficient.should == 1234567890;
112+
}
113+
114+
115+
version(mir_test)
102116
unittest
103117
{
104118
import mir.test;
@@ -156,6 +170,7 @@ template decimalFromStringImpl(alias mullAdd, W = size_t)
156170
}
157171
}
158172

173+
W multiplier = 10;
159174
W d = str[0] - C('0');
160175
str = str[1 .. $];
161176

@@ -205,8 +220,26 @@ template decimalFromStringImpl(alias mullAdd, W = size_t)
205220

206221
if (d < 10)
207222
{
223+
multiplier = 10;
224+
static if (is(C == char) && is(W == ulong))
225+
if (!__ctfe)
226+
if (str.length >= 8 && isMadeOfEightDigits(str[0 .. 8]))
227+
{
228+
multiplier = 1000000000UL;
229+
d *= 100000000;
230+
d += parseEightDigits(str[0 .. 8]);
231+
str = str[8 .. $];
232+
if (str.length >= 8 && isMadeOfEightDigits(str[0 .. 8]))
233+
{
234+
multiplier = 1000000000UL * 100000000;
235+
d *= 100000000;
236+
d += parseEightDigits(str[0 .. 8]);
237+
str = str[8 .. $];
238+
}
239+
}
240+
208241
F0:
209-
if (_expect(mullAdd(10u, cast(uint)d), false))
242+
if (_expect(mullAdd(multiplier, d), false))
210243
return false;
211244
goto S;
212245
}
@@ -239,7 +272,7 @@ template decimalFromStringImpl(alias mullAdd, W = size_t)
239272
}
240273

241274
IF:
242-
W multiplier = 10;
275+
multiplier = 10;
243276
static if (is(C == char) && is(W == ulong))
244277
if (!__ctfe)
245278
{
@@ -305,8 +338,6 @@ template decimalFromStringImpl(alias mullAdd, W = size_t)
305338
FIL:
306339
if (_expect(mullAdd(multiplier, d), false))
307340
return false;
308-
import mir.stdio;
309-
// debug dump("str = ", str);
310341
if (str.length == 0)
311342
return true;
312343
d = str[0] - C('0');
@@ -348,13 +379,9 @@ template decimalFromStringImpl(alias mullAdd, W = size_t)
348379
long exponentPlus;
349380
if (parse(str, exponentPlus) && str.length == 0)
350381
{
351-
import mir.stdio;
352-
// debug dump("exponentPlus ", exponentPlus);
353-
// debug dump("exponent ", exponent);
354382
import mir.checkedint: adds;
355383
bool overflow;
356384
exponent = exponent.adds(exponentPlus, overflow);
357-
// debug dump("overflow ", overflow);
358385
return !overflow;
359386
}
360387
}

0 commit comments

Comments
 (0)