Skip to content

Commit

Permalink
Merge pull request #4614 from JackStouffer/conv
Browse files Browse the repository at this point in the history
Improved docs for std.conv.parse (part 3)
  • Loading branch information
WalterBright committed Jul 21, 2016
2 parents 1be4377 + 8b073fe commit c234a91
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion std/conv.d
Expand Up @@ -2409,8 +2409,22 @@ Lerr:
assertThrown!ConvOverflowException(s.parse!ubyte(16));
}

/**
* Takes a string representing an `enum` type and returns that type.
*
* Params:
* Target = the `enum` type to convert to
* s = the lvalue of the range to _parse
*
* Returns:
* An `enum` of type `Target`
*
* Throws:
* A $(LREF ConvException) if type `Target` does not have a member
* represented by `s`.
*/
Target parse(Target, Source)(ref Source s)
if (isExactSomeString!Source &&
if (isSomeString!Source && !is(Source == enum) &&
is(Target == enum))
{
import std.algorithm.searching : startsWith;
Expand Down Expand Up @@ -2438,6 +2452,15 @@ Target parse(Target, Source)(ref Source s)
~ to!string(s) ~ "'");
}

///
@safe unittest
{
enum EnumType : bool { a = true, b = false, c = a }

auto str = "a";
assert(parse!EnumType(str) == EnumType.a);
}

@safe unittest
{
import std.exception;
Expand Down Expand Up @@ -2469,6 +2492,20 @@ Target parse(Target, Source)(ref Source s)
assert(parse!A(s) == A.member111 && s == "1");
}

/**
* Parses a character range to a floating point number.
*
* Params:
* Target = a floating point type
* p = the lvalue of the range to _parse
*
* Returns:
* A floating point number of type `Target`
*
* Throws:
* A $(LREF ConvException) if `p` is empty, if no number could be
* parsed, or if an overflow occurred.
*/
Target parse(Target, Source)(ref Source p)
if (isInputRange!Source && isSomeChar!(ElementType!Source) && !is(Source == enum) &&
isFloatingPoint!Target && !is(Target == enum))
Expand Down Expand Up @@ -2870,6 +2907,15 @@ Target parse(Target, Source)(ref Source p)
return (sign) ? -ldval : ldval;
}

///
@safe unittest
{
import std.math : approxEqual;
auto str = "123.456";

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

@safe unittest
{
import std.exception;
Expand Down

0 comments on commit c234a91

Please sign in to comment.