consume

Consume a number from the input without converting it. Argument 'fp' enables floating-point consumption. Supports hex input for numbers which are prefixed appropriately

Since version 0.99.9

T[]
consume
(
T
)
(
T[] src
,
bool fp = false
)

Examples

1 char[64] tmp;
2 
3 assert (toInt("1") is 1);
4 assert (toLong("1") is 1);
5 assert (toInt("1", 10) is 1);
6 assert (toLong("1", 10) is 1);
7 assert (toUlong("1", 10) is 1);
8 assert (toUlong("18446744073709551615") is ulong.max);
9 
10 assert (atoi ("12345") is 12345);
11 assert (itoa (tmp, 12345) == "12345");
12 
13 assert(parse( "0"w ) ==  0 );
14 assert(parse( "1"w ) ==  1 );
15 assert(parse( "-1"w ) ==  -1 );
16 assert(parse( "+1"w ) ==  1 );
17 
18 // numerical limits
19 assert(parse( "-2147483648" ) == int.min );
20 assert(parse(  "2147483647" ) == int.max );
21 assert(parse(  "4294967295" ) == uint.max );
22 
23 assert(parse( "-9223372036854775808" ) == long.min );
24 assert(parse( "9223372036854775807" ) == long.max );
25 assert(parse( "18446744073709551615" ) == ulong.max );
26 
27 // hex
28 assert(parse( "a", 16) == 0x0A );
29 assert(parse( "b", 16) == 0x0B );
30 assert(parse( "c", 16) == 0x0C );
31 assert(parse( "d", 16) == 0x0D );
32 assert(parse( "e", 16) == 0x0E );
33 assert(parse( "f", 16) == 0x0F );
34 assert(parse( "A", 16) == 0x0A );
35 assert(parse( "B", 16) == 0x0B );
36 assert(parse( "C", 16) == 0x0C );
37 assert(parse( "D", 16) == 0x0D );
38 assert(parse( "E", 16) == 0x0E );
39 assert(parse( "F", 16) == 0x0F );
40 assert(parse( "FFFF", 16) == ushort.max );
41 assert(parse( "ffffFFFF", 16) == uint.max );
42 assert(parse( "ffffFFFFffffFFFF", 16u ) == ulong.max );
43 // oct
44 assert(parse( "55", 8) == octal!(55) );
45 assert(parse( "100", 8) == octal!(100) );
46 // bin
47 assert(parse( "10000", 2) == 0x10 );
48 // trim
49 assert(parse( "    \t20") == 20 );
50 assert(parse( "    \t-20") == -20 );
51 assert(parse( "-    \t 20") == -20 );
52 // recognise radix prefix
53 assert(parse( "0xFFFF" ) == ushort.max );
54 assert(parse( "0XffffFFFF" ) == uint.max );
55 assert(parse( "0o55") == octal!(55) );
56 assert(parse( "0O55" ) == octal!(55) );
57 assert(parse( "0b10000") == 0x10 );
58 assert(parse( "0B10000") == 0x10 );
59 
60 // prefix tests
61 auto str = "0x";
62 assert(parse( str[0..1] ) ==  0 );
63 assert(parse("0x10", 10) == 0);
64 assert(parse("0b10", 10) == 0);
65 assert(parse("0o10", 10) == 0);
66 assert(parse("0b10") == 0b10);
67 assert(parse("0o10") == octal!(10));
68 assert(parse("0b10", 2) == 0b10);
69 assert(parse("0o10", 8) == octal!(10));
70 
71 // revised tests
72 assert (format(tmp, 10, "d") == "10");
73 assert (format(tmp, -10, "d") == "-10");
74 
75 assert (format(tmp, 10L, "u") == "10");
76 assert (format(tmp, 10L, "U") == "10");
77 assert (format(tmp, 10L, "g") == "10");
78 assert (format(tmp, 10L, "G") == "10");
79 assert (format(tmp, 10L, "o") == "12");
80 assert (format(tmp, 10L, "O") == "12");
81 assert (format(tmp, 10L, "b") == "1010");
82 assert (format(tmp, 10L, "B") == "1010");
83 assert (format(tmp, 10L, "x") == "a");
84 assert (format(tmp, 10L, "X") == "A");
85 
86 assert (format(tmp, 10L, "d+") == "+10");
87 assert (format(tmp, 10L, "d ") == " 10");
88 assert (format(tmp, 10L, "d#") == "10");
89 assert (format(tmp, 10L, "x#") == "0xa");
90 assert (format(tmp, 10L, "X#") == "0XA");
91 assert (format(tmp, 10L, "b#") == "0b1010");
92 assert (format(tmp, 10L, "o#") == "0o12");
93 
94 assert (format(tmp, 10L, "d1") == "10");
95 assert (format(tmp, 10L, "d8") == "00000010");
96 assert (format(tmp, 10L, "x8") == "0000000a");
97 assert (format(tmp, 10L, "X8") == "0000000A");
98 assert (format(tmp, 10L, "b8") == "00001010");
99 assert (format(tmp, 10L, "o8") == "00000012");
100 
101 assert (format(tmp, 10L, "d1#") == "10");
102 assert (format(tmp, 10L, "d6#") == "000010");
103 assert (format(tmp, 10L, "x6#") == "0x00000a");
104 assert (format(tmp, 10L, "X6#") == "0X00000A");
105 
106 char[8] tmp1;
107 assert (format(tmp1, 10L, "b12#") == "0b001010");
108 assert (format(tmp1, 10L, "o12#") == "0o000012");

Meta