mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
Expand the examples for std.format.unformatValue to show all uses cases
This commit is contained in:
parent
eb2bf6fd6b
commit
316b4501b7
1 changed files with 61 additions and 6 deletions
67
std/format.d
67
std/format.d
|
@ -4719,15 +4719,19 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Booleans
|
/// Booleans
|
||||||
@system pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
auto str = "false";
|
auto str = "false";
|
||||||
auto spec = singleSpec("%s");
|
auto spec = singleSpec("%s");
|
||||||
assert(unformatValue!bool(str, spec) == false);
|
assert(unformatValue!bool(str, spec) == false);
|
||||||
|
|
||||||
|
str = "1";
|
||||||
|
spec = singleSpec("%d");
|
||||||
|
assert(unformatValue!bool(str, spec));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Null values
|
/// Null values
|
||||||
@system pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
auto str = "null";
|
auto str = "null";
|
||||||
auto spec = singleSpec("%s");
|
auto spec = singleSpec("%s");
|
||||||
|
@ -4735,15 +4739,23 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Integrals
|
/// Integrals
|
||||||
@system pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
auto str = "123";
|
auto str = "123";
|
||||||
auto spec = singleSpec("%s");
|
auto spec = singleSpec("%s");
|
||||||
assert(str.unformatValue!int(spec) == 123);
|
assert(str.unformatValue!int(spec) == 123);
|
||||||
|
|
||||||
|
str = "ABC";
|
||||||
|
spec = singleSpec("%X");
|
||||||
|
assert(str.unformatValue!int(spec) == 2748);
|
||||||
|
|
||||||
|
str = "11610";
|
||||||
|
spec = singleSpec("%o");
|
||||||
|
assert(str.unformatValue!int(spec) == 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Floating point numbers
|
/// Floating point numbers
|
||||||
@system pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
import std.math : approxEqual;
|
import std.math : approxEqual;
|
||||||
|
|
||||||
|
@ -4753,11 +4765,54 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Character input ranges
|
/// Character input ranges
|
||||||
@system pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
auto str = "aaa";
|
auto str = "aaa";
|
||||||
auto spec = singleSpec("%s");
|
auto spec = singleSpec("%s");
|
||||||
assert(unformatValue!char(str, spec) == 'a');
|
assert(str.unformatValue!char(spec) == 'a');
|
||||||
|
|
||||||
|
// Using a numerical format spec reads a Unicode value from a string
|
||||||
|
str = "65";
|
||||||
|
spec = singleSpec("%d");
|
||||||
|
assert(str.unformatValue!char(spec) == 'A');
|
||||||
|
|
||||||
|
str = "41";
|
||||||
|
spec = singleSpec("%x");
|
||||||
|
assert(str.unformatValue!char(spec) == 'A');
|
||||||
|
|
||||||
|
str = "10003";
|
||||||
|
spec = singleSpec("%d");
|
||||||
|
assert(str.unformatValue!dchar(spec) == '✓');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Arrays and static arrays
|
||||||
|
@safe pure unittest
|
||||||
|
{
|
||||||
|
string str = "aaa";
|
||||||
|
auto spec = singleSpec("%s");
|
||||||
|
assert(str.unformatValue!(dchar[])(spec) == "aaa"d);
|
||||||
|
|
||||||
|
str = "aaa";
|
||||||
|
spec = singleSpec("%s");
|
||||||
|
dchar[3] ret = ['a', 'a', 'a'];
|
||||||
|
assert(str.unformatValue!(dchar[3])(spec) == ret);
|
||||||
|
|
||||||
|
str = "[1, 2, 3, 4]";
|
||||||
|
spec = singleSpec("%s");
|
||||||
|
assert(str.unformatValue!(int[])(spec) == [1, 2, 3, 4]);
|
||||||
|
|
||||||
|
str = "[1, 2, 3, 4]";
|
||||||
|
spec = singleSpec("%s");
|
||||||
|
int[4] ret2 = [1, 2, 3, 4];
|
||||||
|
assert(str.unformatValue!(int[4])(spec) == ret2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Associative arrays
|
||||||
|
@safe pure unittest
|
||||||
|
{
|
||||||
|
auto str = `["one": 1, "two": 2]`;
|
||||||
|
auto spec = singleSpec("%s");
|
||||||
|
assert(str.unformatValue!(int[string])(spec) == ["one": 1, "two": 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@safe pure unittest
|
@safe pure unittest
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue