parse

Parses an integer from a character input range.

Parameters

Target

the integral type to convert to

s Source

the lvalue of an input range

doCount

the flag for deciding to report the number of consumed characters

Return Value

Type: auto
  • A number of type Target if doCount is set to No.doCount
  • A tuple containing a number of type Target and a size_t if doCount is set to Yes.doCount

Throws

A ConvException If an overflow occurred during conversion or if no character of the input was meaningfully converted.

Examples

import std.typecons : Flag, Yes, No;
string s = "123";
auto a = parse!int(s);
assert(a == 123);

string s1 = "123";
auto a1 = parse!(int, string, Yes.doCount)(s1);
assert(a1.data == 123 && a1.count == 3);

// parse only accepts lvalues
static assert(!__traits(compiles, parse!int("123")));
import std.string : tr;
import std.typecons : Flag, Yes, No;
string test = "123 \t  76.14";
auto a = parse!uint(test);
assert(a == 123);
assert(test == " \t  76.14"); // parse bumps string
test = tr(test, " \t\n\r", "", "d"); // skip ws
assert(test == "76.14");
auto b = parse!double(test);
assert(b == 76.14);
assert(test == "");

string test2 = "123 \t  76.14";
auto a2 = parse!(uint, string, Yes.doCount)(test2);
assert(a2.data == 123 && a2.count == 3);
assert(test2 == " \t  76.14");// parse bumps string
test2 = tr(test2, " \t\n\r", "", "d"); // skip ws
assert(test2 == "76.14");
auto b2 = parse!(double, string, Yes.doCount)(test2);
assert(b2.data == 76.14 && b2.count == 5);
assert(test2 == "");

Meta