Skip to content

Commit 2935ae0

Browse files
authored
Merge pull request #5067 from JackStouffer/patch-21
Small clean-up in std.conv.parse
2 parents 140aa0d + c6fb78e commit 2935ae0

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

std/conv.d

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,15 +2342,17 @@ in
23422342
body
23432343
{
23442344
import core.checkedint : mulu, addu;
2345+
import std.exception : enforce;
2346+
23452347
if (radix == 10)
23462348
return parse!Target(s);
23472349

2348-
immutable uint beyond = (radix < 10 ? '0' : 'a'-10) + radix;
2350+
enforce!ConvException(!s.empty, "s must not be empty in integral parse");
23492351

2352+
immutable uint beyond = (radix < 10 ? '0' : 'a'-10) + radix;
23502353
Target v = 0;
2351-
bool atStart = true;
23522354

2353-
for (; !s.empty; s.popFront())
2355+
do
23542356
{
23552357
uint c = s.front;
23562358
if (c < '0')
@@ -2373,20 +2375,12 @@ body
23732375

23742376
bool overflow = false;
23752377
auto nextv = v.mulu(radix, overflow).addu(c - '0', overflow);
2376-
if (overflow || nextv > Target.max)
2377-
goto Loverflow;
2378+
enforce!ConvOverflowException(!overflow && nextv < Target.max, "Overflow in integral conversion");
23782379
v = cast(Target) nextv;
2380+
s.popFront();
2381+
} while (!s.empty);
23792382

2380-
atStart = false;
2381-
}
2382-
if (atStart)
2383-
goto Lerr;
23842383
return v;
2385-
2386-
Loverflow:
2387-
throw new ConvOverflowException("Overflow in integral conversion");
2388-
Lerr:
2389-
throw convError!(Source, Target)(s, radix);
23902384
}
23912385

23922386
@safe pure unittest

0 commit comments

Comments
 (0)