-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat.h
641 lines (606 loc) · 17.5 KB
/
format.h
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#pragma once
#include "engine.h"
#include "format_templates.h"
#include "objects/class.h"
#include "objects/file.h"
#include "objects/formatspec.h"
#include "objects/string.h"
#include "objects/symtab.h"
#include "scanner.h"
#include "stream.h"
#include "utf8.h"
#include "value.h"
#include <cstdint>
#include <typeinfo>
template <> struct FormatHandler<Value> {
static Value Error(const void *error);
static Value EngineError();
static Value Success();
};
template <> struct FormatHandler<std::size_t> {
static std::size_t Error(const void *error);
static std::size_t EngineError();
static std::size_t Success();
};
template <typename R> struct Format<R, Value> {
R fmt(const Value &val, FormatSpec *spec, WritableStream &stream) {
// spec is stack allocated, allocate it to the heap
FormatSpec2 spec2 = FormatSpec::from(
spec->align, spec->fill, spec->sign, spec->isalt, spec->signaware,
spec->width, spec->precision, spec->type);
const Class *c = val.getClass();
Value fspecv = Value(spec2);
Value ret;
if(c->has_fn(SymbolTable2::const_sig_fmt2)) {
Function *f = c->get_fn(SymbolTable2::const_sig_fmt2).toFunction();
File2 fs = File::create(stream);
Value args[] = {fspecv, fs};
if(!ExecutionEngine::execute(val, f, args, 2, &ret, true)) {
return FormatHandler<R>::EngineError();
}
return FormatHandler<R>::Success();
} else if(c->has_fn(SymbolTable2::const_sig_fmt)) {
// call it
Function *f = c->get_fn(SymbolTable2::const_sig_fmt).toFunction();
// execute that fmt(_)
if(!ExecutionEngine::execute(val, f, &fspecv, 1, &ret, true))
return FormatHandler<R>::EngineError();
stream.write(ret);
return FormatHandler<R>::Success();
}
return FormatHandler<R>::Error(
"No method fmt(_) found in object's class!");
}
};
template <typename R> struct Format<R, Utf8Source> {
R fmt(const Utf8Source &s, FormatSpec *f, WritableStream &stream) {
if(f->type != 0 && f->type != 's') {
return FormatHandler<R>::Error(
"Invalid type specifier for string!");
}
if(f->sign) {
return FormatHandler<R>::Error("Sign is invalid for string!");
}
if(f->isalt) {
return FormatHandler<R>::Error("'#' is invalid for string!");
}
if(f->signaware) {
return FormatHandler<R>::Error("'0' is invalid for string!");
}
Utf8Source source = s;
int size = s.len();
int width = size;
int precision = size;
if(f->width != -1) {
width = f->width;
}
if(f->precision != -1 && f->precision < precision) {
precision = f->precision;
}
char fill = ' ';
char align = '<';
if(f->fill)
fill = f->fill;
if(f->align)
align = f->align;
if(width > precision) {
// if numfill > 0, we have space to fill
int numfill = width - precision;
// if the string is left aligned, first fill
// 'precision' characters from string, then fill
// the rest with 'fill'
if(align == '<') {
for(int i = 0; i < precision; i++, ++source) {
stream.write(*source);
}
for(int i = precision; i < width; i++) {
stream.write(fill);
}
} else if(align == '>') {
// if the string is right aligned, first fill
// 'numfill' characters with fill, then fill
// the 'precision' characters with string
for(int i = 0; i < numfill; i++) {
stream.write(fill);
}
for(int i = numfill; i < width; i++, ++source) {
stream.write(*source);
}
} else {
// centered
// fill numfill/2 characters from the left
// precision from the string
// the rest from the right
int k = numfill / 2;
for(int i = 0; i < k; i++) {
stream.write(fill);
}
for(int i = 0; i < precision; i++, ++source) {
stream.write(*source);
}
for(int i = k; i < numfill; i++) {
stream.write(fill);
}
}
} else if(precision < size) {
source += precision;
stream.writebytes(s.source,
(uintptr_t)source.source - (uintptr_t)s.source);
} else {
stream.write(s);
}
return FormatHandler<R>::Success();
}
};
template <typename R> struct Format<R, const char *> {
R fmt(const char *const &val, FormatSpec *f, WritableStream &stream) {
return Format<R, Utf8Source>().fmt(Utf8Source(val), f, stream);
}
};
template <typename R, std::size_t N> struct Format<R, char[N]> {
R fmt(const char (&val)[N], FormatSpec *f, WritableStream &stream) {
return Format<R, Utf8Source>().fmt(Utf8Source(val), f, stream);
}
};
template <typename R> struct Format<R, String *> {
R fmt(const String *const &val, FormatSpec *f, WritableStream &stream) {
return Format<R, Utf8Source>().fmt(val->str(), f, stream);
}
};
struct Formatter {
static bool isalign(utf8_int32_t c) {
return c == '^' || c == '>' || c == '<';
}
static bool issign(utf8_int32_t c) {
return c == ' ' || c == '+' || c == '-';
}
static int todigit(utf8_int32_t c) { return c - '0'; }
static int isdigit(utf8_int32_t c) { return c >= '0' && c <= '9'; }
static int next_int(Utf8Source &val) {
int res = 0;
Utf8Source start = val;
while(*start && isdigit(*start)) {
int d = todigit(*start);
res *= 10;
res += d;
start++;
}
val = start;
return res;
}
template <typename R, typename K>
static R ConvertToInt(const K &val, int64_t *dest) {
(void)val;
(void)dest;
return FormatHandler<R>::Error("Cannot convert to int!");
}
template <typename R>
static R ConvertToInt(const int64_t &val, int64_t *dest) {
*dest = val;
return FormatHandler<R>::Success();
}
template <typename R> static R ConvertToInt(const int &val, int64_t *dest) {
*dest = val;
return FormatHandler<R>::Success();
}
template <typename R>
static R ConvertToInt(const Value &val, int64_t *dest) {
if(val.isInteger()) {
*dest = val.toInteger();
return FormatHandler<R>::Success();
}
return FormatHandler<R>::Error("Cannot convert value to int!");
}
template <typename Out, typename T, typename... R> struct FormatArg;
struct Empty {};
template <typename Out, typename T = Empty, typename... V>
struct FormatArg {
const T & val;
typedef T value_type;
FormatArg<Out, V...> n;
explicit FormatArg(const T &v, const V &...rest)
: val(v), n(FormatArg<Out, V...>(rest...)) {}
const FormatArg<Out, V...> &next() { return n; }
Out format_nth(WritableStream &stream, FormatSpec *f, int idx,
int at = 0) {
if(idx == at) {
if(f)
return Format<Out, T>().fmt(val, f, stream);
else {
stream.write(val);
return FormatHandler<Out>::Success();
}
}
return n.format_nth(stream, f, idx, at + 1);
};
Out format_int(int idx, int64_t *v, int at = 0) {
if(idx == at) {
return ConvertToInt<Out>(val, v);
}
return n.format_int(idx, v, at + 1);
}
};
template <typename Out> struct FormatArg<Out, Value const *, int> {
const Value *const &val;
FormatArg<Out, int> n;
explicit FormatArg(const Value *const &v, const int &rest)
: val(v), n(FormatArg<Out, int>(rest)) {}
const FormatArg<Out, int> &next() { return n; }
Out format_nth(WritableStream &stream, FormatSpec *f, int idx,
int at = 0) {
(void)at;
if(f) {
return Format<Out, Value>().fmt(val[idx], f, stream);
} else {
stream.write(val[idx]);
return FormatHandler<Out>::Success();
}
};
Out format_int(int idx, int64_t *v, int at = 0) {
(void)at;
return ConvertToInt<Out>(val[idx], v);
}
};
template <typename Out> struct FormatArg<Out> {
int val;
explicit FormatArg() { val = 0; }
const FormatArg<Out> &next() { return *this; }
Out format_nth(WritableStream &stream, FormatSpec *f, int idx,
int at = 0) {
(void)idx;
(void)at;
(void)f;
(void)stream;
return FormatHandler<Out>::Error("Reached at the end!");
}
Out format_int(int idx, int64_t *v, int at = 0) {
(void)idx;
(void)at;
(void)v;
return FormatHandler<Out>::Error("Reached at the end!");
}
};
// this is the base method, all other methods call this
// writes to the given stream
template <typename R, typename... T>
static R fmt(WritableStream &stream, const void *source, const T &...args) {
FormatArg<R, T...> argvalues = FormatArg<R, T...>(args...);
int64_t size = sizeof...(args);
if(size > 0 && typeid(argvalues.val) == typeid(const Value *&)) {
R ret = ConvertToInt<R>(argvalues.next().val, &size);
size--; // ignore the source, which is also counted to size
if(ret != FormatHandler<R>::Success()) {
return FormatHandler<R>::Error(
"Expected numargs after Value* as first argument!");
}
}
// arguments start from index 1
// ids start from 0
int argid = 0;
int arg_consumed = 0;
Utf8Source start = Utf8Source(source);
Utf8Source end = Utf8Source(start);
// argid, once given, cannot be omitted.
// so we force that using this flag
// -1 denotes the flag is unset
// 0 denotes automatic numbering
// 1 denotes manual numbering
int argid_present = -1;
while(*end) {
start = end;
// proceed until the next format
while(*end && *end != '{') ++end;
// if we're at the end, nothing to format,
// copy the rest and return the string
if(*end == 0) {
// we good, return the result string
stream.writebytes(start.source, end - start);
break;
} else {
// we're not
// so copy whatever we consumed
if(start != end) {
stream.writebytes(start.source, end - start);
}
}
// we're halted on a '{'
// if the next character is '{', it's an escape
end++;
// we don't use switch here to reduce indent
if(*end == '{') {
// it's an escape
// copy it to the result
stream.writebytes(start.source, end - start);
// proceed and continue
end++;
continue;
}
// now we start formatting
// first, parse the format string
int arg = 0;
char align = 0;
char fill = 0;
char sign = 0;
bool isalt = false;
bool signaware = false;
int width = -1;
bool widtharg = false;
int precision = -1;
bool precarg = false;
char type = 0;
if(argid_present == -1) {
// first time
// we set the state
if(isdigit(*end))
argid_present = 1;
else
argid_present = 0;
}
if(argid_present == 0 && isdigit(*end)) {
return FormatHandler<R>::Error(
"Cannot switch from "
"automatic to manual argument numbering!");
}
// if the argument id is forced and it is
// not a digit, error
else if(argid_present == 1 && !isdigit(*end)) {
return FormatHandler<R>::Error("Argument ID must be present "
"for all arguments!");
}
// if argument id is forced, collect the id
if(argid_present > 0)
arg = next_int(end);
else
arg = argid++;
arg_consumed++;
// check if there is actually an argument to format
if(arg > size - 1) {
if(argid_present) {
return FormatHandler<R>::Error("Invalid argument id!");
} else {
return FormatHandler<R>::Error("Not enough arguments to "
"format!");
}
}
bool has_format_spec = false;
// if the next character is ':', we got format spec
if(*end == ':') {
has_format_spec = true;
end++;
// align
if(isalign(*end))
align = end++;
else if(*end && isalign(end + 1)) {
// fill
fill = end++;
if(fill == '{' || fill == '}') {
return FormatHandler<R>::Error("'{' or '}' cannot "
"be a fill character!");
}
align = end++;
}
// sign
if(issign(*end))
sign = end++;
// alt representation
if(*end == '#') {
end++;
isalt = true;
}
// signaware 0 pad
if(*end == '0') {
end++;
signaware = true;
}
// width
if(isdigit(*end)) {
width = next_int(end);
} else if(*end == '{') {
end++;
// if we're not specifying argids,
// we'd expect this to be '}'
if(!argid_present) {
if(*end == '}') {
width = argid++;
arg_consumed++;
widtharg = true;
end++;
} else if(isdigit(*end)) {
return FormatHandler<R>::Error(
"Expected '}' for width since implicit argument"
"ids are used!");
} else {
return FormatHandler<R>::Error(
"Invalid "
"character for width!");
}
} else {
// we're specifying argids, so this must be an argid
if(isdigit(*end)) {
width = next_int(end);
arg_consumed++;
widtharg = true;
if(*end == '}') {
end++;
} else {
return FormatHandler<R>::Error(
"Expected "
"'}' after width argument id!");
}
} else if(*end == '}') {
return FormatHandler<R>::Error(
"Expected width "
"argument id since explicit "
"argument ids are used!");
} else {
return FormatHandler<R>::Error(
"Invalid character for width!");
}
}
}
// precision
if(*end == '.') {
end++;
if(isdigit(*end)) {
precision = next_int(end);
} else if(*end != '{') {
// there must be a { or a digit after dot
return FormatHandler<R>::Error(
"Expected '{' or precision after '.'!");
} else {
// {
end++;
// if we're not specifying argids,
// we'd expect this to be '}'
if(!argid_present) {
if(*end == '}') {
precision = argid++;
arg_consumed++;
precarg = true;
end++;
} else if(isdigit(*end)) {
return FormatHandler<R>::Error(
"Expected '}' for precision since implicit "
"argument ids are used!");
} else {
return FormatHandler<R>::Error(
"Invalid character for precision!");
}
} else {
// we're specifying argids, so this must be an argid
if(isdigit(*end)) {
precision = next_int(end);
arg_consumed++;
precarg = true;
if(*end == '}') {
end++;
} else {
return FormatHandler<R>::Error(
"Expected '} after argument id of "
"precision!");
}
} else if(*end == '}') {
return FormatHandler<R>::Error(
"Expected argument id of precision since "
"explicit argument ids are used!");
} else {
return FormatHandler<R>::Error(
"Invalid character for argument id of "
"precision!");
}
}
}
}
switch(*end) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 's':
case 'b':
case 'B':
case 'd':
case 'o':
case 'x':
case 'X': type = end++; break;
}
}
if(*end != '}') {
if(*end != 0) {
char msg[] = "Expected '}' in place of 'c'!";
msg[26] = *end;
return FormatHandler<R>::Error(msg);
} else {
return FormatHandler<R>::Error("Unclosed format string!");
}
}
end++;
// first, if the width/precision is from arg,
// check their types
if(widtharg) {
if(width >= size) {
return FormatHandler<R>::Error(
"Invalid argument id for width!");
}
int64_t res = 0;
R ret = argvalues.format_int(width, &res);
if(ret != FormatHandler<R>::Success())
return ret;
width = res;
}
if(precarg) {
if(precision >= size) {
return FormatHandler<R>::Error(
"Invalid argument id for precision!");
}
int64_t res = 0;
R ret = argvalues.format_int(precision, &res);
if(ret != FormatHandler<R>::Success())
return ret;
precision = res;
}
// since the formatspec does not leave this function,
// it should cause no harm to allocate it on the stack,
// as long as we're doing it correctly. moreover, whenever
// Format<Value> is called, that function explicitly
// allocates a formatspec on the heap, and passes that
FormatSpec f;
f.obj.setType(GcObject::Type::FormatSpec, Classes::get<FormatSpec>());
FormatSpec *f2 = NULL;
if(has_format_spec) {
f.align = align;
f.fill = fill;
f.sign = sign;
f.isalt = isalt;
f.signaware = signaware;
f.width = width;
f.precision = precision;
f.type = type;
f2 = &f;
}
// if f2 is NULL, format_nth calls stream.write directly
R ret = argvalues.format_nth(stream, f2, arg);
if(ret != FormatHandler<R>::Success()) {
return ret;
}
}
// but we still got arguments, not permitted
if(arg_consumed < size) {
return FormatHandler<R>::Error("Extra arguments for format!");
}
return FormatHandler<R>::Success();
}
template <typename... K>
static std::size_t fmt1(WritableStream &stream, const void *source,
const K &...args) {
return fmt<std::size_t>(stream, source, args...);
}
// returns a String
template <typename... K>
static Value fmt(const void *source, const K &...args) {
StringStream s;
Value ret = fmt<Value>(s, source, args...);
if(ret != ValueTrue) {
return ret;
}
return s.toString();
}
// first set of methods output to the given stream
// second set returns a string implicitly
static Value valuefmt(WritableStream &stream, const void *fmt,
const Value *args, int numarg);
// format string at args[0]
static Value valuefmt(WritableStream &stream, const Value *args,
int numarg);
static Value valuefmt(const Value *args, int numarg);
};
template <typename... K>
size_t WritableStream::fmt(const void *fmt, const K &...args) {
return Formatter::fmt1(*this, fmt, args...);
}
#include "objects/boolean.h"
#include "objects/number.h"