aboutsummaryrefslogtreecommitdiff
path: root/sys/libfmt/do.c
blob: 3b8ff5ab1172510db1e532c291b2b31c6e2dcf2c (plain)
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
#include "internal.h"
#include <stdatomic.h>

#define atomic _Atomic
#define MaxFmt 128
#define atomic·load  atomic_load
#define atomic·store atomic_store

static struct
{
    atomic int len;
    Verb verb[MaxFmt];
} formatter =
{
    ATOMIC_VAR_INIT(27),
    {
        {' ', ·fmtflag},
        {'#', ·fmtflag},
        {'%', ·fmtpercent},
        {'\'',·fmtflag},
        {'+', ·fmtflag},
        {',', ·fmtflag},
        {'-', ·fmtflag},
        {'C', ·fmtrune},
        {'E', ·fmtfloat},
        {'G', ·fmtfloat},
        {'S', ·fmtutf8},
        {'X', ·fmtint},
        {'b', ·fmtint},
        {'c', ·fmtchar},
        {'d', ·fmtint},
        {'e', ·fmtfloat},
        {'f', ·fmtfloat},
        {'g', ·fmtfloat},
        {'h', ·fmtflag},
        {'l', ·fmtflag},
        {'n', ·fmtcount},
        {'o', ·fmtint},
        {'p', ·fmtint},
        {'r', ·fmterr},
        {'s', ·fmtstr},
        {'u', ·fmtflag},
        {'u', ·fmtint},
        {'x', ·fmtint},
    }
};

static Formatter
format(int c)
{
    Verb *v, *e;
    e = &formatter.verb[atomic·load(&formatter.len)];
    for(v=e; v > formatter.verb; --v){
        if(v->c == c)
            return v->fmt;
    }

    return ·fmterr;
}

static char *
dispatch(fmt·State *io, char *fmt)
{
    rune r;
    int i, n;

    io->flag  = 0;
    io->width = io->prec = 0;

    /*
     * the form of each print verb:
     * % [flags] verb
     *   + the verb is a single character
     *   + each flag is either
     *      - a single character
     *      - a decimal numeric string
     *      - up to 2 decimal strings can be used
     *      - [width|*].[prec|*]
     *      - if missing, set to 0
     *      - if *, grab from varargs
     */
    for(;;){
        fmt += utf8·decode(fmt, &r);
        io->verb = r;
        switch(r){
        case 0:
            return nil;
        case '.':
            io->flag |= fmt·Width|fmt·Prec;
            continue;
        case '0':
            if(!(io->flag & fmt·Width)){
                io->flag |= fmt·Zero;
                continue;
            }
            /* fallthrough */
        case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            i = 0;
            while('0' <= r && r <= '9'){
                i = 10*i + (r-'0');
                r = *fmt++;
            }
            fmt--;
        number:
            if(io->flag & fmt·Width){
                io->flag |= fmt·Prec;
                io->prec = i;
            }else{
                io->flag |= fmt·Width;
                io->width = i;
            }
            continue;
        case '*':
            i = va_arg(io->args, int);
            if(i < 0){
                if(io->flag&fmt·Prec){
                    io->flag ^= fmt·Prec;
                    io->prec = 0;
                }
                i = -i;
                io->flag |= fmt·Left;
            }
            goto number;
        }
        n = format(r)(io);
        if(n < 0)
            return nil;
        if(!n)
            return fmt;
    }
}

static char *
flush(fmt·State *io, char *b, int len)
{
    io->n += b - io->buffer.cur;
    io->buffer.cur = b;
    if(!io->flush || !(*io->flush)(io) || io->buffer.cur + len >= io->buffer.end) {
        io->buffer.end = io->buffer.cur;
        return nil;
    }
    return io->buffer.cur;
}

int
fmt·do(fmt·State *io, char *fmt)
{
    rune r;
    int  c, n;
    char *b, *e;

    for(;;){
        b = io->buffer.cur;
        e = io->buffer.end;

        while((c = *(uchar *)fmt) && c != '%'){
            if(utf8·onebyte(c)){
                if(b + 1 > e){
                    if(!(b=flush(io, b, sizeof(*b))))
                        return -1;
                    e = io->buffer.end;
                }
                *b++ = *fmt++;
            }else{
                n = utf8·decode(fmt, &r);
                if(b + n > e){
                    if(!(b=flush(io, b, sizeof(*b))))
                        return -1;
                    e = io->buffer.end;
                }
                while(n--)
                    *b++ = *fmt++;
            }
            fmt++;
            io->n += b - io->buffer.cur;
            io->buffer.cur = b;
            if(c=='\0') /* we hit our nul terminator */
                return io->n = n;
            io->buffer.end = e;
        }

        if(!(fmt=dispatch(io, fmt)))
            return -1;
    }
}