View source code
Display the source code in std/conv.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.

std.conv.parse - multiple declarations

Function parse

The parse family of functions works quite like the to family, except that:

  1. It only works with character ranges as input.
  2. It takes the input by reference. (This means that rvalues - such as string literals - are not accepted: use to instead.)
  3. It advances the input to the position following the conversion.
  4. It does not throw if it could not convert the entire input.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source source
)
if (isInputRange!Source && isSomeChar!(ElementType!Source) && is(immutable(Target) == immutable(bool)));

This overload converts a character input range to a bool.

Parameters

NameDescription
Target the type to convert to
source the lvalue of an input range
doCount the flag for deciding to report the number of consumed characters

Returns

  • A bool if doCount is set to No.doCount
  • A tuple containing a bool and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if the range does not represent a bool.

Note

All character input range conversions using to are forwarded to parse and do not require lvalues.

Example

import std.typecons : Flag, Yes, No;
auto s = "true";
bool b = parse!bool(s);
assert(b);
auto s2 = "true";
bool b2 = parse!(bool, string, No.doCount)(s2);
assert(b2);
auto s3 = "true";
auto b3 = parse!(bool, string, Yes.doCount)(s3);
assert(b3.data && b3.count == 4);
auto s4 = "falSE";
auto b4 = parse!(bool, string, Yes.doCount)(s4);
assert(!b4.data && b4.count == 5);

Function parse

Parses a character input range to an integral value.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  scope ref Source s
)
if (isSomeChar!(ElementType!Source) && isIntegral!Target && !is(Target == enum));

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source source,
  uint radix
)
if (isSomeChar!(ElementType!Source) && isIntegral!Target && !is(Target == enum));

Parameters

NameDescription
Target the integral type to convert to
s the lvalue of an input range
doCount the flag for deciding to report the number of consumed characters

Returns

  • 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.

Example

import std.typecons : Flag, Yes, No;
string s = "123";
auto a = parse!int(s);
writeln(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")));

Example

import std.string : tr;
import std.typecons : Flag, Yes, No;
string test = "123 \t  76.14";
auto a = parse!uint(test);
writeln(a); // 123
assert(test == " \t  76.14"); // parse bumps string
test = tr(test, " \t\n\r", "", "d"); // skip ws
writeln(test); // "76.14"
auto b = parse!double(test);
writeln(b); // 76.14
writeln(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
writeln(test2); // "76.14"
auto b2 = parse!(double, string, Yes.doCount)(test2);
assert(b2.data == 76.14 && b2.count == 5);
writeln(test2); // ""

Function parse

Takes a string representing an enum type and returns that type.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s
)
if (isSomeString!Source && !is(Source == enum) && is(Target == enum));

Parameters

NameDescription
Target the enum type to convert to
s the lvalue of the range to parse
doCount the flag for deciding to report the number of consumed characters

Returns

  • An enum of type Target if doCount is set to No.doCount
  • A tuple containing an enum of type Target and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if type Target does not have a member represented by s.

Example

import std.typecons : Flag, Yes, No, tuple;
enum EnumType : bool { a = true, b = false, c = a }

auto str = "a";
writeln(parse!EnumType(str)); // EnumType.a
auto str2 = "a";
writeln(parse!(EnumType, string, No.doCount)(str2)); // EnumType.a
auto str3 = "a";
writeln(parse!(EnumType, string, Yes.doCount)(str3)); // tuple(EnumType.a, 1)

Function parse

Parses a character range to a floating point number.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source source
)
if (isInputRange!Source && isSomeChar!(ElementType!Source) && !is(Source == enum) && isFloatingPoint!Target && !is(Target == enum));

Parameters

NameDescription
Target a floating point type
source the lvalue of the range to parse
doCount the flag for deciding to report the number of consumed characters

Returns

  • A floating point number of type Target if doCount is set to No.doCount
  • A tuple containing a floating point number of·type Target and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if source is empty, if no number could be parsed, or if an overflow occurred.

Example

import std.math.operations : isClose;
import std.math.traits : isNaN, isInfinity;
import std.typecons : Flag, Yes, No;
auto str = "123.456";
assert(parse!double(str).isClose(123.456));
auto str2 = "123.456";
assert(parse!(double, string, No.doCount)(str2).isClose(123.456));
auto str3 = "123.456";
auto r = parse!(double, string, Yes.doCount)(str3);
assert(r.data.isClose(123.456));
writeln(r.count); // 7
auto str4 = "-123.456";
r = parse!(double, string, Yes.doCount)(str4);
assert(r.data.isClose(-123.456));
writeln(r.count); // 8
auto str5 = "+123.456";
r = parse!(double, string, Yes.doCount)(str5);
assert(r.data.isClose(123.456));
writeln(r.count); // 8
auto str6 = "inf0";
r = parse!(double, string, Yes.doCount)(str6);
assert(isInfinity(r.data) && r.count == 3 && str6 == "0");
auto str7 = "-0";
auto r2 = parse!(float, string, Yes.doCount)(str7);
assert(r2.data.isClose(0.0) && r2.count == 2);
auto str8 = "nan";
auto r3 = parse!(real, string, Yes.doCount)(str8);
assert(isNaN(r3.data) && r3.count == 3);

Function parse

Parsing one character off a range returns the first element and calls popFront.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s
)
if (isSomeString!Source && !is(Source == enum) && (staticIndexOf!(immutable(Target), immutable(dchar), immutable(ElementEncodingType!Source)) >= 0));

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s
)
if (!isSomeString!Source && isInputRange!Source && isSomeChar!(ElementType!Source) && isSomeChar!Target && (Target.sizeof >= ElementType!Source.sizeof) && !is(Target == enum));

Parameters

NameDescription
Target the type to convert to
s the lvalue of an input range
doCount the flag for deciding to report the number of consumed characters

Returns

  • A character of type Target if doCount is set to No.doCount
  • A tuple containing a character of type Target and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if the range is empty.

Example

import std.typecons : Flag, Yes, No;
auto s = "Hello, World!";
char first = parse!char(s);
writeln(first); // 'H'
writeln(s); // "ello, World!"
char second = parse!(char, string, No.doCount)(s);
writeln(second); // 'e'
writeln(s); // "llo, World!"
auto third = parse!(char, string, Yes.doCount)(s);
assert(third.data == 'l' && third.count == 1);
writeln(s); // "lo, World!"

Function parse

Parsing a character range to typeof(null) returns null if the range spells "null". This function is case insensitive.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s
)
if (isInputRange!Source && isSomeChar!(ElementType!Source) && is(immutable(Target) == immutable(typeof(null))));

Parameters

NameDescription
Target the type to convert to
s the lvalue of an input range
doCount the flag for deciding to report the number of consumed characters

Returns

  • null if doCount is set to No.doCount
  • A tuple containing null and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if the range doesn't represent null.

Example

import std.exception : assertThrown;
import std.typecons : Flag, Yes, No;

alias NullType = typeof(null);
auto s1 = "null";
assert(parse!NullType(s1) is null);
writeln(s1); // ""

auto s2 = "NUll"d;
assert(parse!NullType(s2) is null);
writeln(s2); // ""

auto s3 = "nuLlNULl";
assert(parse!(NullType, string, No.doCount)(s3) is null);
auto r = parse!(NullType, string, Yes.doCount)(s3);
assert(r.data is null && r.count == 4);

auto m = "maybe";
assertThrown!ConvException(parse!NullType(m));
assertThrown!ConvException(parse!(NullType, string, Yes.doCount)(m));
assert(m == "maybe");  // m shouldn't change on failure

auto s = "NULL";
assert(parse!(const NullType)(s) is null);

Function parse

Parses an array from a string given the left bracket (default '['), right bracket (default ']'), and element separator (by default ','). A trailing separator is allowed.

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s,
  dchar lbracket = '[',
  dchar rbracket = ']',
  dchar comma = ','
)
if (isSomeString!Source && !is(Source == enum) && isDynamicArray!Target && !is(Target == enum));

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s,
  dchar lbracket = '[',
  dchar rbracket = ']',
  dchar comma = ','
)
if (isExactSomeString!Source && isStaticArray!Target && !is(Target == enum));

Parameters

NameDescription
s The string to parse
lbracket the character that starts the array
rbracket the character that ends the array
comma the character that separates the elements of the array
doCount the flag for deciding to report the number of consumed characters

Returns

  • An array of type Target if doCount is set to No.doCount
  • A tuple containing an array of type Target and a size_t if doCount is set to Yes.doCount

Example

import std.typecons : Flag, Yes, No;
auto s1 = `[['h', 'e', 'l', 'l', 'o'], "world"]`;
auto a1 = parse!(string[])(s1);
writeln(a1); // ["hello", "world"]

auto s2 = `["aaa", "bbb", "ccc"]`;
auto a2 = parse!(string[])(s2);
writeln(a2); // ["aaa", "bbb", "ccc"]

auto s3 = `[['h', 'e', 'l', 'l', 'o'], "world"]`;
auto len3 = s3.length;
auto a3 = parse!(string[], string, Yes.doCount)(s3);
writeln(a3.data); // ["hello", "world"]
writeln(a3.count); // len3

Function parse

Parses an associative array from a string given the left bracket (default '['), right bracket (default ']'), key-value separator (default ':'), and element seprator (by default ',').

auto parse(Target, Source, Flag!("doCount") doCount = No.doCount) (
  ref Source s,
  dchar lbracket = '[',
  dchar rbracket = ']',
  dchar keyval = ':',
  dchar comma = ','
)
if (isSomeString!Source && !is(Source == enum) && isAssociativeArray!Target && !is(Target == enum));

Parameters

NameDescription
s the string to parse
lbracket the character that starts the associative array
rbracket the character that ends the associative array
keyval the character that associates the key with the value
comma the character that separates the elements of the associative array
doCount the flag for deciding to report the number of consumed characters

Returns

  • An associative array of type Target if doCount is set to No.doCount
  • A tuple containing an associative array of type Target and a size_t if doCount is set to Yes.doCount

Example

import std.typecons : Flag, Yes, No, tuple;
import std.range.primitives : save;
import std.array : assocArray;
auto s1 = "[1:10, 2:20, 3:30]";
auto copyS1 = s1.save;
auto aa1 = parse!(int[int])(s1);
writeln(aa1); // [1:10, 2:20, 3:30]
// parse!(int[int], string, Yes.doCount)(copyS1)
writeln(tuple([1:10, 2:20, 3:30], copyS1.length));

auto s2 = `["aaa":10, "bbb":20, "ccc":30]`;
auto copyS2 = s2.save;
auto aa2 = parse!(int[string])(s2);
writeln(aa2); // ["aaa":10, "bbb":20, "ccc":30]
assert(tuple(["aaa":10, "bbb":20, "ccc":30], copyS2.length) ==
    parse!(int[string], string, Yes.doCount)(copyS2));

auto s3 = `["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]`;
auto copyS3 = s3.save;
auto aa3 = parse!(int[][string])(s3);
writeln(aa3); // ["aaa":[1], "bbb":[2, 3], "ccc":[4, 5, 6]]
assert(tuple(["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]], copyS3.length) ==
    parse!(int[][string], string, Yes.doCount)(copyS3));

auto s4 = `[]`;
int[int] emptyAA;
writeln(tuple(emptyAA, s4.length)); // parse!(int[int], string, Yes.doCount)(s4)

Authors

Walter Bright, Andrei Alexandrescu, Shin Fujishiro, Adam D. Ruppe, Kenji Hara

License

Boost License 1.0.