View source code
Display the source code in dmd/utils.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function dmd.utils.parseDigits

Convert string to integer.

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

Parameters

NameDescription
T Type of integer to parse
val Variable to store the result in
p slice to start of string digits
max max allowable value (inclusive), defaults to T.max

Returns

false on error, true on success

Example

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));

Authors

Walter Bright

License

Boost License 1.0