parseDigits

Convert string to integer.

@safe pure @nogc nothrow
bool
parseDigits
(
T
)
(
ref T val
,
const(char)[] p
,
const T max = T.max
)

Parameters

T

Type of integer to parse

val T

Variable to store the result in

p const(char)[]

slice to start of string digits

max T

max allowable value (inclusive), defaults to T.max

Return Value

Type: bool

false on error, true on success

Examples

byte b;
ubyte ub;
short s;
ushort us;
int i;
uint ui;
long l;
ulong ul;

assert(b.parseDigits("42") && b  == 42);
assert(ub.parseDigits("42") && ub == 42);

assert(s.parseDigits("420") && s  == 420);
assert(us.parseDigits("42000") && us == 42_000);

assert(i.parseDigits("420000") && i  == 420_000);
assert(ui.parseDigits("420000") && ui == 420_000);

assert(l.parseDigits("42000000000") && l  == 42_000_000_000);
assert(ul.parseDigits("82000000000") && ul == 82_000_000_000);

assert(!b.parseDigits(ubyte.max.stringof));
assert(!b.parseDigits("WYSIWYG"));
assert(!b.parseDigits("-42"));
assert(!b.parseDigits("200"));
assert(ub.parseDigits("200") && ub == 200);
assert(i.parseDigits(int.max.stringof) && i == int.max);
assert(i.parseDigits("420", 500) && i == 420);
assert(!i.parseDigits("420", 400));
Suggestion Box / Bug Report