Skip to content

Commit 316b450

Browse files
committed
Expand the examples for std.format.unformatValue to show all uses cases
1 parent eb2bf6f commit 316b450

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

std/format.d

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4719,31 +4719,43 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
47194719
}
47204720

47214721
/// Booleans
4722-
@system pure unittest
4722+
@safe pure unittest
47234723
{
47244724
auto str = "false";
47254725
auto spec = singleSpec("%s");
47264726
assert(unformatValue!bool(str, spec) == false);
4727+
4728+
str = "1";
4729+
spec = singleSpec("%d");
4730+
assert(unformatValue!bool(str, spec));
47274731
}
47284732

47294733
/// Null values
4730-
@system pure unittest
4734+
@safe pure unittest
47314735
{
47324736
auto str = "null";
47334737
auto spec = singleSpec("%s");
47344738
assert(str.unformatValue!(typeof(null))(spec) == null);
47354739
}
47364740

47374741
/// Integrals
4738-
@system pure unittest
4742+
@safe pure unittest
47394743
{
47404744
auto str = "123";
47414745
auto spec = singleSpec("%s");
47424746
assert(str.unformatValue!int(spec) == 123);
4747+
4748+
str = "ABC";
4749+
spec = singleSpec("%X");
4750+
assert(str.unformatValue!int(spec) == 2748);
4751+
4752+
str = "11610";
4753+
spec = singleSpec("%o");
4754+
assert(str.unformatValue!int(spec) == 5000);
47434755
}
47444756

47454757
/// Floating point numbers
4746-
@system pure unittest
4758+
@safe pure unittest
47474759
{
47484760
import std.math : approxEqual;
47494761

@@ -4753,11 +4765,54 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
47534765
}
47544766

47554767
/// Character input ranges
4756-
@system pure unittest
4768+
@safe pure unittest
47574769
{
47584770
auto str = "aaa";
47594771
auto spec = singleSpec("%s");
4760-
assert(unformatValue!char(str, spec) == 'a');
4772+
assert(str.unformatValue!char(spec) == 'a');
4773+
4774+
// Using a numerical format spec reads a Unicode value from a string
4775+
str = "65";
4776+
spec = singleSpec("%d");
4777+
assert(str.unformatValue!char(spec) == 'A');
4778+
4779+
str = "41";
4780+
spec = singleSpec("%x");
4781+
assert(str.unformatValue!char(spec) == 'A');
4782+
4783+
str = "10003";
4784+
spec = singleSpec("%d");
4785+
assert(str.unformatValue!dchar(spec) == '');
4786+
}
4787+
4788+
/// Arrays and static arrays
4789+
@safe pure unittest
4790+
{
4791+
string str = "aaa";
4792+
auto spec = singleSpec("%s");
4793+
assert(str.unformatValue!(dchar[])(spec) == "aaa"d);
4794+
4795+
str = "aaa";
4796+
spec = singleSpec("%s");
4797+
dchar[3] ret = ['a', 'a', 'a'];
4798+
assert(str.unformatValue!(dchar[3])(spec) == ret);
4799+
4800+
str = "[1, 2, 3, 4]";
4801+
spec = singleSpec("%s");
4802+
assert(str.unformatValue!(int[])(spec) == [1, 2, 3, 4]);
4803+
4804+
str = "[1, 2, 3, 4]";
4805+
spec = singleSpec("%s");
4806+
int[4] ret2 = [1, 2, 3, 4];
4807+
assert(str.unformatValue!(int[4])(spec) == ret2);
4808+
}
4809+
4810+
/// Associative arrays
4811+
@safe pure unittest
4812+
{
4813+
auto str = `["one": 1, "two": 2]`;
4814+
auto spec = singleSpec("%s");
4815+
assert(str.unformatValue!(int[string])(spec) == ["one": 1, "two": 2]);
47614816
}
47624817

47634818
@safe pure unittest

0 commit comments

Comments
 (0)