-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.cpp
70 lines (59 loc) · 1.79 KB
/
format.cpp
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
#include "format.h"
#include "objects/errors.h"
#include "objects/string.h"
#include "printer.h"
#include "value.h"
// see https://fmt.dev/latest/syntax.html
/*
* replacement_field ::= "{" [arg_id] [":" format_spec] "}"
* arg_id ::= integer
* integer ::= digit+
* digit ::= "0"..."9"
*/
/*
* format_spec ::= [[fill]align][sign]["#"]["0"][width]["." precision][type]
* fill ::= <a character other than '{' or '}'>
* align ::= "<" | ">" | "^"
* sign ::= "+" | "-" | " "
* width ::= integer | "{" arg_id "}"
* precision ::= integer | "{" arg_id "}"
* type ::= int_type | "a" | "A" | "e" | "E" | "f" | "F" | "g" |
* "G" | "s"
* int_type ::= "b" | "B" | "d" | "o" | "x" | "X"
*/
Value FormatHandler<Value>::Error(const void *err) {
return FormatError::sete(String::from(err, utf8len(err)));
}
Value FormatHandler<Value>::EngineError() {
return ValueNil;
}
Value FormatHandler<Value>::Success() {
return ValueTrue;
}
size_t FormatHandler<size_t>::Error(const void *e) {
panic((const char *)e);
return 0;
}
size_t FormatHandler<size_t>::EngineError() {
return -1;
}
size_t FormatHandler<size_t>::Success() {
return 0;
}
Value Formatter::valuefmt(WritableStream &stream, const void *source,
const Value *args, int numargs) {
Value ret = fmt<Value>(stream, source, args, numargs);
return ret;
}
Value Formatter::valuefmt(WritableStream &stream, const Value *args,
int numargs) {
return valuefmt(stream, args[0].toString()->strb(), &args[1], numargs);
}
Value Formatter::valuefmt(const Value *args, int numargs) {
StringStream s;
Value ret = valuefmt(s, args, numargs);
if(ret != ValueTrue) {
return ret;
}
return s.toString();
}