| 1 |
@safe pure unittest |
| 2 |
{ |
| 3 |
import std.format.read; |
| 4 |
|
| 5 |
import std.format.spec : singleSpec; |
| 6 |
|
| 7 |
auto str = "false"; |
| 8 |
auto spec = singleSpec("%s"); |
| 9 |
assert(str.unformatValue!bool(spec) == false); |
| 10 |
|
| 11 |
str = "1"; |
| 12 |
spec = singleSpec("%d"); |
| 13 |
assert(str.unformatValue!bool(spec) == true); |
| 14 |
} |
| 15 |
|
| 16 |
@safe pure unittest |
| 17 |
{ |
| 18 |
import std.format.read; |
| 19 |
|
| 20 |
import std.format.spec : singleSpec; |
| 21 |
|
| 22 |
auto str = "null"; |
| 23 |
auto spec = singleSpec("%s"); |
| 24 |
assert(str.unformatValue!(typeof(null))(spec) == null); |
| 25 |
} |
| 26 |
|
| 27 |
@safe pure unittest |
| 28 |
{ |
| 29 |
import std.format.read; |
| 30 |
|
| 31 |
import std.format.spec : singleSpec; |
| 32 |
|
| 33 |
// signed decimal values |
| 34 |
auto str = "123"; |
| 35 |
auto spec = singleSpec("%s"); |
| 36 |
assert(str.unformatValue!int(spec) == 123); |
| 37 |
|
| 38 |
// hexadecimal values |
| 39 |
str = "ABC"; |
| 40 |
spec = singleSpec("%X"); |
| 41 |
assert(str.unformatValue!int(spec) == 2748); |
| 42 |
|
| 43 |
// octal values |
| 44 |
str = "11610"; |
| 45 |
spec = singleSpec("%o"); |
| 46 |
assert(str.unformatValue!int(spec) == 5000); |
| 47 |
|
| 48 |
// raw read, depends on endianess |
| 49 |
str = "\x75\x01"; |
| 50 |
spec = singleSpec("%r"); |
| 51 |
auto result = str.unformatValue!short(spec); |
| 52 |
assert(result == 373 /* little endian */ || result == 29953 /* big endian */ ); |
| 53 |
} |
| 54 |
|
| 55 |
@safe pure unittest |
| 56 |
{ |
| 57 |
import std.format.read; |
| 58 |
|
| 59 |
import std.format.spec : singleSpec; |
| 60 |
import std.math.operations : isClose; |
| 61 |
|
| 62 |
// natural notation |
| 63 |
auto str = "123.456"; |
| 64 |
auto spec = singleSpec("%s"); |
| 65 |
assert(str.unformatValue!double(spec).isClose(123.456)); |
| 66 |
|
| 67 |
// scientific notation |
| 68 |
str = "1e17"; |
| 69 |
spec = singleSpec("%e"); |
| 70 |
assert(str.unformatValue!double(spec).isClose(1e17)); |
| 71 |
|
| 72 |
// raw read, depends on endianess |
| 73 |
str = "\x40\x00\x00\xBF"; |
| 74 |
spec = singleSpec("%r"); |
| 75 |
auto result = str.unformatValue!float(spec); |
| 76 |
assert(isClose(result, -0.5) /* little endian */ || isClose(result, 2.0) /* big endian */ ); |
| 77 |
} |
| 78 |
|
| 79 |
@safe pure unittest |
| 80 |
{ |
| 81 |
import std.format.read; |
| 82 |
|
| 83 |
import std.format.spec : singleSpec; |
| 84 |
|
| 85 |
// only the first character is read |
| 86 |
auto str = "abc"; |
| 87 |
auto spec = singleSpec("%s"); |
| 88 |
assert(str.unformatValue!char(spec) == 'a'); |
| 89 |
|
| 90 |
// using a numerical format character treats the read number as unicode code point |
| 91 |
str = "65"; |
| 92 |
spec = singleSpec("%d"); |
| 93 |
assert(str.unformatValue!char(spec) == 'A'); |
| 94 |
|
| 95 |
str = "41"; |
| 96 |
spec = singleSpec("%x"); |
| 97 |
assert(str.unformatValue!char(spec) == 'A'); |
| 98 |
|
| 99 |
str = "10003"; |
| 100 |
spec = singleSpec("%d"); |
| 101 |
assert(str.unformatValue!dchar(spec) == '✓'); |
| 102 |
} |
| 103 |
|
| 104 |
@safe pure unittest |
| 105 |
{ |
| 106 |
import std.format.read; |
| 107 |
|
| 108 |
import std.format.spec : singleSpec; |
| 109 |
|
| 110 |
// string value |
| 111 |
string str = "aaa"; |
| 112 |
auto spec = singleSpec("%s"); |
| 113 |
assert(str.unformatValue!(dchar[])(spec) == "aaa"d); |
| 114 |
|
| 115 |
// fixed size array with characters |
| 116 |
str = "aaa"; |
| 117 |
spec = singleSpec("%s"); |
| 118 |
dchar[3] ret = ['a', 'a', 'a']; |
| 119 |
assert(str.unformatValue!(dchar[3])(spec) == ret); |
| 120 |
|
| 121 |
// dynamic array |
| 122 |
str = "[1, 2, 3, 4]"; |
| 123 |
spec = singleSpec("%s"); |
| 124 |
assert(str.unformatValue!(int[])(spec) == [1, 2, 3, 4]); |
| 125 |
|
| 126 |
// fixed size array with integers |
| 127 |
str = "[1, 2, 3, 4]"; |
| 128 |
spec = singleSpec("%s"); |
| 129 |
int[4] ret2 = [1, 2, 3, 4]; |
| 130 |
assert(str.unformatValue!(int[4])(spec) == ret2); |
| 131 |
|
| 132 |
// compound specifiers can be used for more control |
| 133 |
str = "1,2,3"; |
| 134 |
spec = singleSpec("%(%s,%)"); |
| 135 |
assert(str.unformatValue!(int[])(spec) == [1, 2, 3]); |
| 136 |
|
| 137 |
str = "cool"; |
| 138 |
spec = singleSpec("%(%c%)"); |
| 139 |
assert(str.unformatValue!(char[])(spec) == ['c', 'o', 'o', 'l']); |
| 140 |
} |
| 141 |
|
| 142 |
@safe pure unittest |
| 143 |
{ |
| 144 |
import std.format.read; |
| 145 |
|
| 146 |
import std.format.spec : singleSpec; |
| 147 |
|
| 148 |
// as single value |
| 149 |
auto str = `["one": 1, "two": 2]`; |
| 150 |
auto spec = singleSpec("%s"); |
| 151 |
assert(str.unformatValue!(int[string])(spec) == ["one": 1, "two": 2]); |
| 152 |
|
| 153 |
// with compound specifier for more control |
| 154 |
str = "1/1, 2/4, 3/9"; |
| 155 |
spec = singleSpec("%(%d/%d%|, %)"); |
| 156 |
assert(str.unformatValue!(int[int])(spec) == [1: 1, 2: 4, 3: 9]); |
| 157 |
} |
| 158 |
|
| 159 |
@safe pure unittest |
| 160 |
{ |
| 161 |
import std.format.read; |
| 162 |
|
| 163 |
string object; |
| 164 |
char cmp; |
| 165 |
int value; |
| 166 |
|
| 167 |
assert(formattedRead("angle < 36", "%s %c %d", object, cmp, value) == 3); |
| 168 |
assert(object == "angle"); |
| 169 |
assert(cmp == '<'); |
| 170 |
assert(value == 36); |
| 171 |
|
| 172 |
// reading may end early: |
| 173 |
assert(formattedRead("length >", "%s %c %d", object, cmp, value) == 2); |
| 174 |
assert(object == "length"); |
| 175 |
assert(cmp == '>'); |
| 176 |
// value is not changed: |
| 177 |
assert(value == 36); |
| 178 |
} |
| 179 |
|
| 180 |
@safe pure unittest |
| 181 |
{ |
| 182 |
import std.format.read; |
| 183 |
|
| 184 |
string a; |
| 185 |
int b; |
| 186 |
double c; |
| 187 |
|
| 188 |
assert("hello!124:34.5".formattedRead!"%s!%s:%s"(a, b, c) == 3); |
| 189 |
assert(a == "hello"); |
| 190 |
assert(b == 124); |
| 191 |
assert(c == 34.5); |
| 192 |
} |
| 193 |
|
| 194 |
@safe pure unittest |
| 195 |
{ |
| 196 |
import std.format.read; |
| 197 |
|
| 198 |
string item; |
| 199 |
double amount; |
| 200 |
|
| 201 |
assert("orange: (12%) 15.25".formattedRead("%s: (%*d%%) %f", item, amount) == 2); |
| 202 |
assert(item == "orange"); |
| 203 |
assert(amount == 15.25); |
| 204 |
|
| 205 |
// can also be used with tuples |
| 206 |
import std.typecons : Tuple; |
| 207 |
|
| 208 |
Tuple!(int, float) t; |
| 209 |
char[] line = "1 7643 2.125".dup; |
| 210 |
formattedRead(line, "%s %*u %s", t); |
| 211 |
assert(t[0] == 1 && t[1] == 2.125); |
| 212 |
} |
| 213 |
|
| 214 |
@safe pure unittest |
| 215 |
{ |
| 216 |
import std.format.read; |
| 217 |
|
| 218 |
import std.exception : assertThrown; |
| 219 |
import std.format : FormatException; |
| 220 |
import std.typecons : tuple; |
| 221 |
|
| 222 |
auto complete = "hello!34.5:124".formattedRead!(string, double, int)("%s!%s:%s"); |
| 223 |
assert(complete == tuple("hello", 34.5, 124)); |
| 224 |
|
| 225 |
// reading ends early |
| 226 |
assertThrown!FormatException("hello!34.5:".formattedRead!(string, double, int)("%s!%s:%s")); |
| 227 |
} |
| 228 |
|
| 229 |
@safe pure unittest |
| 230 |
{ |
| 231 |
import std.format.read; |
| 232 |
|
| 233 |
import std.format : FormatException; |
| 234 |
import std.typecons : tuple; |
| 235 |
|
| 236 |
auto result = "orange: (12%) 15.25".formattedRead!(string, double)("%s: (%*d%%) %f"); |
| 237 |
assert(result == tuple("orange", 15.25)); |
| 238 |
} |
| 239 |
|
| 240 |
@safe pure unittest |
| 241 |
{ |
| 242 |
import std.format.read; |
| 243 |
|
| 244 |
import std.exception : assertThrown; |
| 245 |
import std.format : FormatException; |
| 246 |
import std.typecons : tuple; |
| 247 |
|
| 248 |
auto expected = tuple("hello", 124, 34.5); |
| 249 |
auto result = "hello!124:34.5".formattedRead!("%s!%s:%s", string, int, double); |
| 250 |
assert(result == expected); |
| 251 |
|
| 252 |
assertThrown!FormatException("hello!34.5:".formattedRead!("%s!%s:%s", string, double, int)); |
| 253 |
} |
| 254 |
|
| 255 |
@safe pure unittest |
| 256 |
{ |
| 257 |
import std.format.read; |
| 258 |
|
| 259 |
import std.format : FormatException; |
| 260 |
import std.typecons : tuple; |
| 261 |
|
| 262 |
static assert(!__traits(compiles, "orange: (12%) 15.25".formattedRead!("%s: (%*d%%) %f", string, double))); |
| 263 |
} |
| 264 |
|
| 265 |
@safe pure unittest |
| 266 |
{ |
| 267 |
import std.format.read; |
| 268 |
|
| 269 |
import std.format.spec : singleSpec; |
| 270 |
|
| 271 |
string s = "42"; |
| 272 |
auto spec = singleSpec("%s"); |
| 273 |
assert(unformatValue!int(s, spec) == 42); |
| 274 |
} |
| 275 |
|