summaryrefslogtreecommitdiff
path: root/AK/PrintfImplementation.h
blob: f9a46ce10f44f65ca6b2a327aa2d0d11dc311ae5 (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
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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Format.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
#include <stdarg.h>
#include <wchar.h>

#ifndef KERNEL
#    include <math.h>
#endif

#ifdef __serenity__
extern "C" size_t strlen(char const*);
#else
#    include <string.h>
#endif

namespace PrintfImplementation {

template<typename PutChFunc, typename T, typename CharType>
ALWAYS_INLINE int print_hex(PutChFunc putch, CharType*& bufptr, T number, bool upper_case, bool alternate_form, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
{
    constexpr char const* printf_hex_digits_lower = "0123456789abcdef";
    constexpr char const* printf_hex_digits_upper = "0123456789ABCDEF";

    u32 digits = 0;
    for (T n = number; n > 0; n >>= 4)
        ++digits;
    if (digits == 0)
        digits = 1;

    bool not_zero = number != 0;

    char buf[16];
    char* p = buf;

    if (!(has_precision && precision == 0 && !not_zero)) {
        if (number == 0) {
            (*p++) = '0';
            if (precision > 0)
                precision--;
        } else {
            u8 shift_count = digits * 4;
            while (shift_count) {
                shift_count -= 4;
                (*p++) = upper_case
                    ? printf_hex_digits_upper[(number >> shift_count) & 0x0f]
                    : printf_hex_digits_lower[(number >> shift_count) & 0x0f];
                if (precision > 0)
                    precision--;
            }
        }
    }

    size_t numlen = p - buf;

    if (!field_width || field_width < (numlen + has_precision * precision + (alternate_form * 2 * not_zero)))
        field_width = numlen + has_precision * precision + alternate_form * 2 * not_zero;

    if ((zero_pad && !has_precision) && (alternate_form && not_zero)) {
        putch(bufptr, '0');
        putch(bufptr, 'x');
    }

    if (!left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision - alternate_form * 2 * not_zero; ++i) {
            putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
        }
    }

    if (!(zero_pad && !has_precision) && (alternate_form && not_zero)) {
        putch(bufptr, '0');
        putch(bufptr, 'x');
    }

    if (has_precision) {
        for (u32 i = 0; i < precision; ++i) {
            putch(bufptr, '0');
        }
    }

    for (unsigned i = 0; i < numlen; ++i) {
        putch(bufptr, buf[i]);
    }

    if (left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision - alternate_form * 2 * not_zero; ++i) {
            putch(bufptr, ' ');
        }
    }

    return field_width;
}

template<typename PutChFunc, typename CharType>
ALWAYS_INLINE int print_decimal(PutChFunc putch, CharType*& bufptr, u64 number, bool sign, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
{
    u64 divisor = 10000000000000000000LLU;
    char ch;
    char padding = 1;
    char buf[21];
    char* p = buf;

    if (!(has_precision && precision == 0 && number == 0)) {
        for (;;) {
            ch = '0' + (number / divisor);
            number %= divisor;
            if (ch != '0')
                padding = 0;
            if (!padding || divisor == 1) {
                *(p++) = ch;
                if (precision > 0)
                    precision--;
            }
            if (divisor == 1)
                break;
            divisor /= 10;
        }
    }

    size_t numlen = p - buf;

    if (!field_width || field_width < (numlen + has_precision * precision + (sign || always_sign)))
        field_width = numlen + has_precision * precision + (sign || always_sign);

    if ((zero_pad && !has_precision) && (sign || always_sign)) {
        putch(bufptr, sign ? '-' : '+');
    }

    if (!left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision - (sign || always_sign); ++i) {
            putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
        }
    }

    if (!(zero_pad && !has_precision) && (sign || always_sign)) {
        putch(bufptr, sign ? '-' : '+');
    }

    if (has_precision) {
        for (u32 i = 0; i < precision; ++i) {
            putch(bufptr, '0');
        }
    }

    for (unsigned i = 0; i < numlen; ++i) {
        putch(bufptr, buf[i]);
    }

    if (left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision - (sign || always_sign); ++i) {
            putch(bufptr, ' ');
        }
    }

    return field_width;
}
#ifndef KERNEL
template<typename PutChFunc, typename CharType>
ALWAYS_INLINE int print_double(PutChFunc putch, CharType*& bufptr, double number, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, u32 precision)
{
    int length = 0;

    u32 whole_width = (field_width >= precision + 1) ? field_width - precision - 1 : 0;

    bool sign = signbit(number);
    bool nan = isnan(number);
    bool inf = isinf(number);

    if (nan || inf) {
        for (unsigned i = 0; i < field_width - 3 - sign; i++) {
            putch(bufptr, ' ');
            length++;
        }
        if (sign) {
            putch(bufptr, '-');
            length++;
        }
        if (nan) {
            putch(bufptr, 'n');
            putch(bufptr, 'a');
            putch(bufptr, 'n');
        } else {
            putch(bufptr, 'i');
            putch(bufptr, 'n');
            putch(bufptr, 'f');
        }
        return length + 3;
    }

    if (sign)
        number = -number;

    length = print_decimal(putch, bufptr, (i64)number, sign, always_sign, left_pad, zero_pad, whole_width, false, 1);
    if (precision > 0) {
        putch(bufptr, '.');
        length++;
        double fraction = number - (i64)number;

        for (u32 i = 0; i < precision; ++i)
            fraction = fraction * 10;

        return length + print_decimal(putch, bufptr, (i64)fraction, false, false, false, true, precision, false, 1);
    }

    return length;
}
#endif
template<typename PutChFunc, typename CharType>
ALWAYS_INLINE int print_octal_number(PutChFunc putch, CharType*& bufptr, u64 number, bool alternate_form, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
{
    u32 divisor = 134217728;
    char ch;
    char padding = 1;
    char buf[32];
    char* p = buf;

    if (alternate_form) {
        (*p++) = '0';
        if (precision > 0)
            precision--;
    }

    if (!(has_precision && precision == 0 && number == 0)) {
        for (;;) {
            ch = '0' + (number / divisor);
            number %= divisor;
            if (ch != '0')
                padding = 0;
            if (!padding || divisor == 1) {
                *(p++) = ch;
                if (precision > 0)
                    precision--;
            }
            if (divisor == 1)
                break;
            divisor /= 8;
        }
    }

    size_t numlen = p - buf;

    if (!field_width || field_width < (numlen + has_precision * precision))
        field_width = numlen + has_precision * precision;

    if (!left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision; ++i) {
            putch(bufptr, (zero_pad && !has_precision) ? '0' : ' ');
        }
    }

    if (has_precision) {
        for (u32 i = 0; i < precision; ++i) {
            putch(bufptr, '0');
        }
    }

    for (unsigned i = 0; i < numlen; ++i) {
        putch(bufptr, buf[i]);
    }

    if (left_pad) {
        for (unsigned i = 0; i < field_width - numlen - has_precision * precision; ++i) {
            putch(bufptr, ' ');
        }
    }

    return field_width;
}

template<typename PutChFunc, typename T, typename CharType>
ALWAYS_INLINE int print_string(PutChFunc putch, CharType*& bufptr, T str, size_t len, bool left_pad, size_t field_width, bool dot, size_t precision, bool has_fraction)
{
    if (has_fraction)
        len = min(len, precision);

    if (!dot && (!field_width || field_width < len))
        field_width = len;

    if (has_fraction && !field_width)
        field_width = len;

    size_t pad_amount = field_width > len ? field_width - len : 0;

    if (!left_pad) {
        for (size_t i = 0; i < pad_amount; ++i)
            putch(bufptr, ' ');
    }
    for (size_t i = 0; i < min(len, field_width); ++i) {
        putch(bufptr, str[i]);
    }
    if (left_pad) {
        for (size_t i = 0; i < pad_amount; ++i)
            putch(bufptr, ' ');
    }
    return field_width;
}

template<typename PutChFunc, typename CharType>
ALWAYS_INLINE int print_signed_number(PutChFunc putch, CharType*& bufptr, i64 number, bool always_sign, bool left_pad, bool zero_pad, u32 field_width, bool has_precision, u32 precision)
{
    // FIXME: `0 - number` overflows if we are trying to negate the smallest possible value.
    return print_decimal(putch, bufptr, (number < 0) ? 0 - number : number, number < 0, always_sign, left_pad, zero_pad, field_width, has_precision, precision);
}

struct ModifierState {
    bool left_pad { false };
    bool zero_pad { false };
    bool dot { false };
    unsigned field_width { 0 };
    bool has_precision { false };
    unsigned precision { 6 };
    unsigned short_qualifiers { 0 }; // TODO: Unimplemented.
    unsigned long_qualifiers { 0 };
    bool intmax_qualifier { false };      // TODO: Unimplemented.
    bool ptrdiff_qualifier { false };     // TODO: Unimplemented.
    bool long_double_qualifier { false }; // TODO: Unimplemented.
    bool size_qualifier { false };        // TODO: Unimplemented.
    bool alternate_form { 0 };
    bool always_sign { false };
};

template<typename PutChFunc, typename ArgumentListRefT, template<typename T, typename U = ArgumentListRefT> typename NextArgument, typename CharType = char>
struct PrintfImpl {
    ALWAYS_INLINE PrintfImpl(PutChFunc& putch, CharType*& bufptr, int const& nwritten)
        : m_bufptr(bufptr)
        , m_nwritten(nwritten)
        , m_putch(putch)
    {
    }

    ALWAYS_INLINE int format_s(ModifierState const& state, ArgumentListRefT ap) const
    {
        // FIXME: Narrow characters should be converted to wide characters on the fly and vice versa.
        // https://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html
        // https://pubs.opengroup.org/onlinepubs/9699919799/functions/wprintf.html
#ifndef KERNEL
        if (state.long_qualifiers) {
            wchar_t const* sp = NextArgument<wchar_t const*>()(ap);
            if (!sp)
                sp = L"(null)";
            return print_string(m_putch, m_bufptr, sp, wcslen(sp), state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
        }
#endif
        char const* sp = NextArgument<char const*>()(ap);
        if (!sp)
            sp = "(null)";
        return print_string(m_putch, m_bufptr, sp, strlen(sp), state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
    }
    ALWAYS_INLINE int format_d(ModifierState const& state, ArgumentListRefT ap) const
    {
        i64 number = [&]() -> i64 {
            if (state.long_qualifiers >= 2)
                return NextArgument<long long int>()(ap);
            if (state.long_qualifiers == 1)
                return NextArgument<long int>()(ap);
            return NextArgument<int>()(ap);
        }();

        return print_signed_number(m_putch, m_bufptr, number, state.always_sign, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
    }
    ALWAYS_INLINE int format_i(ModifierState const& state, ArgumentListRefT ap) const
    {
        return format_d(state, ap);
    }
    ALWAYS_INLINE int format_u(ModifierState const& state, ArgumentListRefT ap) const
    {
        u64 number = [&]() -> u64 {
            if (state.long_qualifiers >= 2)
                return NextArgument<unsigned long long int>()(ap);
            if (state.long_qualifiers == 1)
                return NextArgument<unsigned long int>()(ap);
            return NextArgument<unsigned int>()(ap);
        }();

        return print_decimal(m_putch, m_bufptr, number, false, false, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
    }
    ALWAYS_INLINE int format_Q(ModifierState const& state, ArgumentListRefT ap) const
    {
        return print_decimal(m_putch, m_bufptr, NextArgument<u64>()(ap), false, false, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
    }
    ALWAYS_INLINE int format_q(ModifierState const& state, ArgumentListRefT ap) const
    {
        return print_hex(m_putch, m_bufptr, NextArgument<u64>()(ap), false, false, state.left_pad, state.zero_pad, 16, false, 1);
    }
#ifndef KERNEL
    ALWAYS_INLINE int format_g(ModifierState const& state, ArgumentListRefT ap) const
    {
        return format_f(state, ap);
    }
    ALWAYS_INLINE int format_f(ModifierState const& state, ArgumentListRefT ap) const
    {
        return print_double(m_putch, m_bufptr, NextArgument<double>()(ap), state.always_sign, state.left_pad, state.zero_pad, state.field_width, state.precision);
    }
#endif
    ALWAYS_INLINE int format_o(ModifierState const& state, ArgumentListRefT ap) const
    {
        return print_octal_number(m_putch, m_bufptr, NextArgument<u32>()(ap), state.alternate_form, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
    }
    ALWAYS_INLINE int format_unsigned_hex(ModifierState const& state, ArgumentListRefT ap, bool uppercase) const
    {
        u64 number = [&]() -> u64 {
            if (state.long_qualifiers >= 2)
                return NextArgument<unsigned long long int>()(ap);
            if (state.long_qualifiers == 1)
                return NextArgument<unsigned long int>()(ap);
            return NextArgument<unsigned int>()(ap);
        }();

        return print_hex(m_putch, m_bufptr, number, uppercase, state.alternate_form, state.left_pad, state.zero_pad, state.field_width, state.has_precision, state.precision);
    }
    ALWAYS_INLINE int format_x(ModifierState const& state, ArgumentListRefT ap) const
    {
        return format_unsigned_hex(state, ap, false);
    }
    ALWAYS_INLINE int format_X(ModifierState const& state, ArgumentListRefT ap) const
    {
        return format_unsigned_hex(state, ap, true);
    }
    ALWAYS_INLINE int format_n(ModifierState const&, ArgumentListRefT ap) const
    {
        *NextArgument<int*>()(ap) = m_nwritten;
        return 0;
    }
    ALWAYS_INLINE int format_p(ModifierState const&, ArgumentListRefT ap) const
    {
        return print_hex(m_putch, m_bufptr, NextArgument<FlatPtr>()(ap), false, true, false, true, 8, false, 1);
    }
    ALWAYS_INLINE int format_P(ModifierState const&, ArgumentListRefT ap) const
    {
        return print_hex(m_putch, m_bufptr, NextArgument<FlatPtr>()(ap), true, true, false, true, 8, false, 1);
    }
    ALWAYS_INLINE int format_percent(ModifierState const&, ArgumentListRefT) const
    {
        m_putch(m_bufptr, '%');
        return 1;
    }
    ALWAYS_INLINE int format_c(ModifierState const& state, ArgumentListRefT ap) const
    {
        char c = NextArgument<int>()(ap);
        return print_string(m_putch, m_bufptr, &c, 1, state.left_pad, state.field_width, state.dot, state.precision, state.has_precision);
    }
    ALWAYS_INLINE int format_unrecognized(CharType format_op, CharType const* fmt, ModifierState const&, ArgumentListRefT) const
    {
        dbgln("printf_internal: Unimplemented format specifier {} (fmt: {})", format_op, fmt);
        return 0;
    }

protected:
    CharType*& m_bufptr;
    int const& m_nwritten;
    PutChFunc& m_putch;
};

template<typename T, typename V>
struct VaArgNextArgument {
    ALWAYS_INLINE T operator()(V ap) const
    {
        return va_arg(ap, T);
    }
};

#define PRINTF_IMPL_DELEGATE_TO_IMPL(c)    \
    case* #c:                              \
        ret += impl.format_##c(state, ap); \
        break;

template<typename PutChFunc, template<typename T, typename U, template<typename X, typename Y> typename V, typename C = char> typename Impl = PrintfImpl, typename ArgumentListT = va_list, template<typename T, typename V = decltype(declval<ArgumentListT&>())> typename NextArgument = VaArgNextArgument, typename CharType = char>
ALWAYS_INLINE int printf_internal(PutChFunc putch, IdentityType<CharType>* buffer, CharType const*& fmt, ArgumentListT ap)
{
    int ret = 0;
    CharType* bufptr = buffer;

    Impl<PutChFunc, ArgumentListT&, NextArgument, CharType> impl { putch, bufptr, ret };

    for (CharType const* p = fmt; *p; ++p) {
        ModifierState state;
        if (*p == '%' && *(p + 1)) {
        one_more:
            ++p;
            if (*p == '.') {
                state.dot = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == '-') {
                state.left_pad = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == '+') {
                state.always_sign = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (!state.zero_pad && !state.field_width && !state.dot && *p == '0') {
                state.zero_pad = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p >= '0' && *p <= '9') {
                if (!state.dot) {
                    state.field_width *= 10;
                    state.field_width += *p - '0';
                    if (*(p + 1))
                        goto one_more;
                } else {
                    if (!state.has_precision) {
                        state.has_precision = true;
                        state.precision = 0;
                    }
                    state.precision *= 10;
                    state.precision += *p - '0';
                    if (*(p + 1))
                        goto one_more;
                }
            }
            if (*p == '*') {
                if (state.dot) {
                    state.has_precision = true;
                    state.zero_pad = true;
                    state.precision = NextArgument<int>()(ap);
                } else {
                    state.field_width = NextArgument<int>()(ap);
                }

                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 'h') {
                ++state.short_qualifiers;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 'l') {
                ++state.long_qualifiers;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 'j') {
                state.intmax_qualifier = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 't') {
                state.ptrdiff_qualifier = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 'L') {
                state.long_double_qualifier = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == 'z') {
                state.size_qualifier = true;
                if (*(p + 1))
                    goto one_more;
            }
            if (*p == '#') {
                state.alternate_form = true;
                if (*(p + 1))
                    goto one_more;
            }
            switch (*p) {
            case '%':
                ret += impl.format_percent(state, ap);
                break;

                PRINTF_IMPL_DELEGATE_TO_IMPL(P);
                PRINTF_IMPL_DELEGATE_TO_IMPL(Q);
                PRINTF_IMPL_DELEGATE_TO_IMPL(X);
                PRINTF_IMPL_DELEGATE_TO_IMPL(c);
                PRINTF_IMPL_DELEGATE_TO_IMPL(d);
#ifndef KERNEL
                PRINTF_IMPL_DELEGATE_TO_IMPL(f);
                PRINTF_IMPL_DELEGATE_TO_IMPL(g);
#endif
                PRINTF_IMPL_DELEGATE_TO_IMPL(i);
                PRINTF_IMPL_DELEGATE_TO_IMPL(n);
                PRINTF_IMPL_DELEGATE_TO_IMPL(o);
                PRINTF_IMPL_DELEGATE_TO_IMPL(p);
                PRINTF_IMPL_DELEGATE_TO_IMPL(q);
                PRINTF_IMPL_DELEGATE_TO_IMPL(s);
                PRINTF_IMPL_DELEGATE_TO_IMPL(u);
                PRINTF_IMPL_DELEGATE_TO_IMPL(x);
            default:
                ret += impl.format_unrecognized(*p, fmt, state, ap);
                break;
            }
        } else {
            putch(bufptr, *p);
            ++ret;
        }
    }
    return ret;
}

#undef PRINTF_IMPL_DELEGATE_TO_IMPL

}

using PrintfImplementation::printf_internal;