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.

Target parse(Target, Source) (
  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

Returns

A bool

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

auto s = "true";
bool b = parse!bool(s);
assert(b);

Function parse

Parses a character input range to an integral value.

Target parse(Target, Source) (
  ref Source s
)
if (isSomeChar!(ElementType!Source) && isIntegral!Target && !is(Target == enum));

Target parse(Target, Source) (
  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

Returns

A number of type Target

Throws

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

Example

string s = "123";
auto a = parse!int(s);
writeln(a); // 123

// parse only accepts lvalues
static assert(!__traits(compiles, parse!int("123")));

Example

import std.string : tr;
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); // ""

Function parse

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

Target parse(Target, Source) (
  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

Returns

An enum of type Target

Throws

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

Example

enum EnumType : bool { a = true, b = false, c = a }

auto str = "a";
writeln(parse!EnumType(str)); // EnumType.a

Function parse

Parses a character range to a floating point number.

Target parse(Target, Source) (
  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

Returns

A floating point number of type Target

Throws

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

Example

import std.math : approxEqual;
auto str = "123.456";

assert(parse!double(str).approxEqual(123.456));

Function parse

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

Target parse(Target, Source) (
  ref Source s
)
if (isSomeString!Source && !is(Source == enum) && (staticIndexOf!(immutable(Target), immutable(dchar), immutable(ElementEncodingType!Source)) >= 0));

Target parse(Target, Source) (
  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

Returns

A character of type Target

Throws

A ConvException if the range is empty.

Example

auto s = "Hello, World!";
char first = parse!char(s);
writeln(first); // 'H'
writeln(s); // "ello, World!"

Function parse

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

Target parse(Target, Source) (
  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

Returns

null

Throws

A ConvException if the range doesn't represent null.

Example

import std.exception : assertThrown;

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 m = "maybe";
assertThrown!ConvException(parse!NullType(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.

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

Target parse(Target, Source) (
  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

Returns

An array of type Target

Example

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"]

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 ',').

Target parse(Target, Source) (
  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

Returns

An associative array of type Target

Example

auto s1 = "[1:10, 2:20, 3:30]";
auto aa1 = parse!(int[int])(s1);
writeln(aa1); // [1:10, 2:20, 3:30]

auto s2 = `["aaa":10, "bbb":20, "ccc":30]`;
auto aa2 = parse!(int[string])(s2);
writeln(aa2); // ["aaa":10, "bbb":20, "ccc":30]

auto s3 = `["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]`;
auto aa3 = parse!(int[][string])(s3);
writeln(aa3); // ["aaa":[1], "bbb":[2, 3], "ccc":[4, 5, 6]]

Authors

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

License

Boost License 1.0.