1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
// Written in the D programming language.
/*
Copyright: Copyright The D Language Foundation 2000-2013.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
Authors: $(HTTP walterbright.com, Walter Bright), $(HTTP erdani.com,
Andrei Alexandrescu), and Kenji Hara
Source: $(PHOBOSSRC std/format/internal/read.d)
*/
module std.format.internal.read;
import std.range.primitives : ElementEncodingType, ElementType, isInputRange;
import std.traits : isAggregateType, isArray, isAssociativeArray,
isDynamicArray, isFloatingPoint, isIntegral, isSomeChar, isSomeString,
isStaticArray, StringTypeOf;
import std.format.spec : FormatSpec;
package(std.format):
void skipData(Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
{
import std.ascii : isDigit, isWhite;
import std.range.primitives : empty, front, popFront;
switch (spec.spec)
{
case 'c': input.popFront(); break;
case 'd':
if (input.front == '+' || input.front == '-') input.popFront();
goto case 'u';
case 's':
while (!input.empty && !isWhite(input.front)) input.popFront();
break;
case 'u':
while (!input.empty && isDigit(input.front)) input.popFront();
break;
default:
assert(0, "Format specifier not understood: %" ~ spec.spec);
}
}
private template acceptedSpecs(T)
{
static if (isIntegral!T)
enum acceptedSpecs = "bdosuxX";
else static if (isFloatingPoint!T)
enum acceptedSpecs = "seEfgG";
else static if (isSomeChar!T)
enum acceptedSpecs = "bcdosuxX"; // integral + 'c'
else
enum acceptedSpecs = "";
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range && is(immutable T == immutable bool))
{
import std.algorithm.searching : find;
import std.conv : parse, text;
import std.format : enforceFmt, unformatValue;
if (spec.spec == 's') return parse!T(input);
enforceFmt(find(acceptedSpecs!long, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
return unformatValue!long(input, spec) != 0;
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range && is(T == typeof(null)))
{
import std.conv : parse, text;
import std.format : enforceFmt;
enforceFmt(spec.spec == 's',
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
return parse!T(input);
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range && isIntegral!T && !is(T == enum) && isSomeChar!(ElementType!Range))
{
import std.algorithm.searching : find;
import std.conv : parse, text;
import std.format : enforceFmt, FormatException;
if (spec.spec == 'r')
{
static if (is(immutable ElementEncodingType!Range == immutable char)
|| is(immutable ElementEncodingType!Range == immutable byte)
|| is(immutable ElementEncodingType!Range == immutable ubyte))
return rawRead!T(input);
else
throw new FormatException(
"The raw read specifier %r may only be used with narrow strings and ranges of bytes."
);
}
enforceFmt(find(acceptedSpecs!T, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
enforceFmt(spec.width == 0, "Parsing integers with a width specification is not implemented"); // TODO
immutable uint base =
spec.spec == 'x' || spec.spec == 'X' ? 16 :
spec.spec == 'o' ? 8 :
spec.spec == 'b' ? 2 :
spec.spec == 's' || spec.spec == 'd' || spec.spec == 'u' ? 10 : 0;
assert(base != 0, "base must be not equal to zero");
return parse!T(input, base);
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isFloatingPoint!T && !is(T == enum) && isInputRange!Range
&& isSomeChar!(ElementType!Range)&& !is(Range == enum))
{
import std.algorithm.searching : find;
import std.conv : parse, text;
import std.format : enforceFmt, FormatException;
if (spec.spec == 'r')
{
static if (is(immutable ElementEncodingType!Range == immutable char)
|| is(immutable ElementEncodingType!Range == immutable byte)
|| is(immutable ElementEncodingType!Range == immutable ubyte))
return rawRead!T(input);
else
throw new FormatException(
"The raw read specifier %r may only be used with narrow strings and ranges of bytes."
);
}
enforceFmt(find(acceptedSpecs!T, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
return parse!T(input);
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range && isSomeChar!T && !is(T == enum) && isSomeChar!(ElementType!Range))
{
import std.algorithm.searching : find;
import std.conv : to, text;
import std.range.primitives : empty, front, popFront;
import std.format : enforceFmt, unformatValue;
if (spec.spec == 's' || spec.spec == 'c')
{
auto result = to!T(input.front);
input.popFront();
return result;
}
enforceFmt(find(acceptedSpecs!T, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
enum int size = T.sizeof;
static if (size == 1)
return unformatValue!ubyte(input, spec);
else static if (size == 2)
return unformatValue!ushort(input, spec);
else static if (size == 4)
return unformatValue!uint(input, spec);
else
static assert(false, T.stringof ~ ".sizeof must be 1, 2, or 4 not " ~
size.stringof);
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char fmt)
if (isInputRange!Range && is(StringTypeOf!T) && !isAggregateType!T && !is(T == enum))
{
import std.conv : text;
import std.range.primitives : empty, front, popFront, put;
import std.format : enforceFmt;
const spec = fmt.spec;
if (spec == '(')
{
return unformatRange!T(input, fmt);
}
enforceFmt(spec == 's',
text("Wrong unformat specifier '%", spec , "' for ", T.stringof));
static if (isStaticArray!T)
{
T result;
auto app = result[];
}
else
{
import std.array : appender;
auto app = appender!T();
}
if (fmt.trailing.empty)
{
for (; !input.empty; input.popFront())
{
static if (isStaticArray!T)
if (app.empty)
break;
app.put(input.front);
}
}
else
{
immutable end = fmt.trailing.front;
for (; !input.empty && input.front != end; input.popFront())
{
static if (isStaticArray!T)
if (app.empty)
break;
app.put(input.front);
}
}
static if (isStaticArray!T)
{
enforceFmt(app.empty, "need more input");
return result;
}
else
return app.data;
}
T unformatValueImpl(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char fmt)
if (isInputRange!Range && !is(StringTypeOf!T) && !isAggregateType!T
&& (isArray!T || isAssociativeArray!T || is(T == enum)))
{
import std.conv : parse, text;
import std.format : enforceFmt;
const spec = fmt.spec;
if (spec == '(')
{
return unformatRange!T(input, fmt);
}
enforceFmt(spec == 's',
text("Wrong unformat specifier '%", spec , "' for ", T.stringof));
return parse!T(input);
}
/*
* Function that performs raw reading. Used by unformatValue
* for integral and float types.
*/
private T rawRead(T, Range)(ref Range input)
if (is(immutable ElementEncodingType!Range == immutable char)
|| is(immutable ElementEncodingType!Range == immutable byte)
|| is(immutable ElementEncodingType!Range == immutable ubyte))
{
import std.range.primitives : popFront;
union X
{
ubyte[T.sizeof] raw;
T typed;
}
X x;
foreach (i; 0 .. T.sizeof)
{
static if (isSomeString!Range)
{
x.raw[i] = input[0];
input = input[1 .. $];
}
else
{
// TODO: recheck this
x.raw[i] = input.front;
input.popFront();
}
}
return x.typed;
}
private T unformatRange(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
in (spec.spec == '(', "spec.spec must be '(' not " ~ spec.spec)
{
import std.range.primitives : empty, front, popFront;
import std.format : enforceFmt, format;
T result;
static if (isStaticArray!T)
{
size_t i;
}
const(Char)[] cont = spec.trailing;
for (size_t j = 0; j < spec.trailing.length; ++j)
{
if (spec.trailing[j] == '%')
{
cont = spec.trailing[0 .. j];
break;
}
}
bool checkEnd()
{
return input.empty || !cont.empty && input.front == cont.front;
}
if (!checkEnd())
{
for (;;)
{
auto fmt = FormatSpec!Char(spec.nested);
fmt.readUpToNextSpec(input);
enforceFmt(!input.empty, "Unexpected end of input when parsing range");
static if (isStaticArray!T)
{
result[i++] = unformatElement!(typeof(T.init[0]))(input, fmt);
}
else static if (isDynamicArray!T)
{
import std.conv : WideElementType;
result ~= unformatElement!(WideElementType!T)(input, fmt);
}
else static if (isAssociativeArray!T)
{
auto key = unformatElement!(typeof(T.init.keys[0]))(input, fmt);
fmt.readUpToNextSpec(input); // eat key separator
result[key] = unformatElement!(typeof(T.init.values[0]))(input, fmt);
}
static if (isStaticArray!T)
{
enforceFmt(i <= T.length,
"Too many format specifiers for static array of length %d".format(T.length));
}
if (spec.sep !is null)
fmt.readUpToNextSpec(input);
auto sep = spec.sep !is null ? spec.sep : fmt.trailing;
if (checkEnd())
break;
if (!sep.empty && input.front == sep.front)
{
while (!sep.empty)
{
enforceFmt(!input.empty,
"Unexpected end of input when parsing range separator");
enforceFmt(input.front == sep.front,
"Unexpected character when parsing range separator");
input.popFront();
sep.popFront();
}
}
}
}
static if (isStaticArray!T)
{
enforceFmt(i == T.length,
"Too few (%d) format specifiers for static array of length %d".format(i, T.length));
}
return result;
}
T unformatElement(T, Range, Char)(ref Range input, scope const ref FormatSpec!Char spec)
if (isInputRange!Range)
{
import std.conv : parseElement;
import std.format.read : unformatValue;
static if (isSomeString!T)
{
if (spec.spec == 's')
{
return parseElement!T(input);
}
}
else static if (isSomeChar!T)
{
if (spec.spec == 's')
{
return parseElement!T(input);
}
}
return unformatValue!T(input, spec);
}
|