summaryrefslogtreecommitdiff
path: root/AK/Format.cpp
blob: f9316aff382b435875768e9a945eb093f6b6a99e (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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/CharacterTypes.h>
#include <AK/Format.h>
#include <AK/GenericLexer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/kstdio.h>

#if defined(__serenity__) && !defined(KERNEL)
#    include <serenity.h>
#endif

#ifdef KERNEL
#    include <Kernel/Process.h>
#    include <Kernel/Thread.h>
#else
#    include <math.h>
#    include <stdio.h>
#    include <string.h>
#endif

namespace AK {

class FormatParser : public GenericLexer {
public:
    struct FormatSpecifier {
        StringView flags;
        size_t index;
    };

    explicit FormatParser(StringView input);

    StringView consume_literal();
    bool consume_number(size_t& value);
    bool consume_specifier(FormatSpecifier& specifier);
    bool consume_replacement_field(size_t& index);
};

namespace {

static constexpr size_t use_next_index = NumericLimits<size_t>::max();

// The worst case is that we have the largest 64-bit value formatted as binary number, this would take
// 65 bytes. Choosing a larger power of two won't hurt and is a bit of mitigation against out-of-bounds accesses.
static constexpr size_t convert_unsigned_to_string(u64 value, Array<u8, 128>& buffer, u8 base, bool upper_case)
{
    VERIFY(base >= 2 && base <= 16);

    constexpr const char* lowercase_lookup = "0123456789abcdef";
    constexpr const char* uppercase_lookup = "0123456789ABCDEF";

    if (value == 0) {
        buffer[0] = '0';
        return 1;
    }

    size_t used = 0;
    while (value > 0) {
        if (upper_case)
            buffer[used++] = uppercase_lookup[value % base];
        else
            buffer[used++] = lowercase_lookup[value % base];

        value /= base;
    }

    for (size_t i = 0; i < used / 2; ++i)
        swap(buffer[i], buffer[used - i - 1]);

    return used;
}

void vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builder, FormatParser& parser)
{
    const auto literal = parser.consume_literal();
    builder.put_literal(literal);

    FormatParser::FormatSpecifier specifier;
    if (!parser.consume_specifier(specifier)) {
        VERIFY(parser.is_eof());
        return;
    }

    if (specifier.index == use_next_index)
        specifier.index = params.take_next_index();

    auto& parameter = params.parameters().at(specifier.index);

    FormatParser argparser { specifier.flags };
    parameter.formatter(params, builder, argparser, parameter.value);

    vformat_impl(params, builder, parser);
}

} // namespace AK::{anonymous}

FormatParser::FormatParser(StringView input)
    : GenericLexer(input)
{
}
StringView FormatParser::consume_literal()
{
    const auto begin = tell();

    while (!is_eof()) {
        if (consume_specific("{{"))
            continue;

        if (consume_specific("}}"))
            continue;

        if (next_is(is_any_of("{}")))
            return m_input.substring_view(begin, tell() - begin);

        consume();
    }

    return m_input.substring_view(begin);
}
bool FormatParser::consume_number(size_t& value)
{
    value = 0;

    bool consumed_at_least_one = false;
    while (next_is(is_ascii_digit)) {
        value *= 10;
        value += parse_ascii_digit(consume());
        consumed_at_least_one = true;
    }

    return consumed_at_least_one;
}
bool FormatParser::consume_specifier(FormatSpecifier& specifier)
{
    VERIFY(!next_is('}'));

    if (!consume_specific('{'))
        return false;

    if (!consume_number(specifier.index))
        specifier.index = use_next_index;

    if (consume_specific(':')) {
        const auto begin = tell();

        size_t level = 1;
        while (level > 0) {
            VERIFY(!is_eof());

            if (consume_specific('{')) {
                ++level;
                continue;
            }

            if (consume_specific('}')) {
                --level;
                continue;
            }

            consume();
        }

        specifier.flags = m_input.substring_view(begin, tell() - begin - 1);
    } else {
        if (!consume_specific('}'))
            VERIFY_NOT_REACHED();

        specifier.flags = "";
    }

    return true;
}
bool FormatParser::consume_replacement_field(size_t& index)
{
    if (!consume_specific('{'))
        return false;

    if (!consume_number(index))
        index = use_next_index;

    if (!consume_specific('}'))
        VERIFY_NOT_REACHED();

    return true;
}

void FormatBuilder::put_padding(char fill, size_t amount)
{
    for (size_t i = 0; i < amount; ++i)
        m_builder.append(fill);
}
void FormatBuilder::put_literal(StringView value)
{
    for (size_t i = 0; i < value.length(); ++i) {
        m_builder.append(value[i]);
        if (value[i] == '{' || value[i] == '}')
            ++i;
    }
}
void FormatBuilder::put_string(
    StringView value,
    Align align,
    size_t min_width,
    size_t max_width,
    char fill)
{
    const auto used_by_string = min(max_width, value.length());
    const auto used_by_padding = max(min_width, used_by_string) - used_by_string;

    if (used_by_string < value.length())
        value = value.substring_view(0, used_by_string);

    if (align == Align::Left || align == Align::Default) {
        m_builder.append(value);
        put_padding(fill, used_by_padding);
    } else if (align == Align::Center) {
        const auto used_by_left_padding = used_by_padding / 2;
        const auto used_by_right_padding = ceil_div<size_t, size_t>(used_by_padding, 2);

        put_padding(fill, used_by_left_padding);
        m_builder.append(value);
        put_padding(fill, used_by_right_padding);
    } else if (align == Align::Right) {
        put_padding(fill, used_by_padding);
        m_builder.append(value);
    }
}
void FormatBuilder::put_u64(
    u64 value,
    u8 base,
    bool prefix,
    bool upper_case,
    bool zero_pad,
    Align align,
    size_t min_width,
    char fill,
    SignMode sign_mode,
    bool is_negative)
{
    if (align == Align::Default)
        align = Align::Right;

    Array<u8, 128> buffer;

    const auto used_by_digits = convert_unsigned_to_string(value, buffer, base, upper_case);

    size_t used_by_prefix = 0;
    if (align == Align::Right && zero_pad) {
        // We want String::formatted("{:#08x}", 32) to produce '0x00000020' instead of '0x000020'. This
        // behaviour differs from both fmtlib and printf, but is more intuitive.
        used_by_prefix = 0;
    } else {
        if (is_negative || sign_mode != SignMode::OnlyIfNeeded)
            used_by_prefix += 1;

        if (prefix) {
            if (base == 8)
                used_by_prefix += 1;
            else if (base == 16)
                used_by_prefix += 2;
            else if (base == 2)
                used_by_prefix += 2;
        }
    }

    const auto used_by_field = used_by_prefix + used_by_digits;
    const auto used_by_padding = max(used_by_field, min_width) - used_by_field;

    const auto put_prefix = [&]() {
        if (is_negative)
            m_builder.append('-');
        else if (sign_mode == SignMode::Always)
            m_builder.append('+');
        else if (sign_mode == SignMode::Reserved)
            m_builder.append(' ');

        if (prefix) {
            if (base == 2) {
                if (upper_case)
                    m_builder.append("0B");
                else
                    m_builder.append("0b");
            } else if (base == 8) {
                m_builder.append("0");
            } else if (base == 16) {
                if (upper_case)
                    m_builder.append("0X");
                else
                    m_builder.append("0x");
            }
        }
    };
    const auto put_digits = [&]() {
        for (size_t i = 0; i < used_by_digits; ++i)
            m_builder.append(buffer[i]);
    };

    if (align == Align::Left) {
        const auto used_by_right_padding = used_by_padding;

        put_prefix();
        put_digits();
        put_padding(fill, used_by_right_padding);
    } else if (align == Align::Center) {
        const auto used_by_left_padding = used_by_padding / 2;
        const auto used_by_right_padding = ceil_div<size_t, size_t>(used_by_padding, 2);

        put_padding(fill, used_by_left_padding);
        put_prefix();
        put_digits();
        put_padding(fill, used_by_right_padding);
    } else if (align == Align::Right) {
        const auto used_by_left_padding = used_by_padding;

        if (zero_pad) {
            put_prefix();
            put_padding('0', used_by_left_padding);
            put_digits();
        } else {
            put_padding(fill, used_by_left_padding);
            put_prefix();
            put_digits();
        }
    }
}
void FormatBuilder::put_i64(
    i64 value,
    u8 base,
    bool prefix,
    bool upper_case,
    bool zero_pad,
    Align align,
    size_t min_width,
    char fill,
    SignMode sign_mode)
{
    const auto is_negative = value < 0;
    value = is_negative ? -value : value;

    put_u64(static_cast<u64>(value), base, prefix, upper_case, zero_pad, align, min_width, fill, sign_mode, is_negative);
}

#ifndef KERNEL
void FormatBuilder::put_f64(
    double value,
    u8 base,
    bool upper_case,
    bool zero_pad,
    Align align,
    size_t min_width,
    size_t precision,
    char fill,
    SignMode sign_mode)
{
    StringBuilder string_builder;
    FormatBuilder format_builder { string_builder };

    if (isnan(value) || isinf(value)) [[unlikely]] {
        if (value < 0.0)
            string_builder.append('-');
        else if (sign_mode == SignMode::Always)
            string_builder.append('+');
        else
            string_builder.append(' ');

        if (isnan(value))
            string_builder.append(upper_case ? "NAN"sv : "nan"sv);
        else
            string_builder.append(upper_case ? "INF"sv : "inf"sv);
        format_builder.put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
        return;
    }

    bool is_negative = value < 0.0;
    if (is_negative)
        value = -value;

    format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative);

    if (precision > 0) {
        // FIXME: This is a terrible approximation but doing it properly would be a lot of work. If someone is up for that, a good
        // place to start would be the following video from CppCon 2019:
        // https://youtu.be/4P_kbF0EbZM (Stephan T. Lavavej “Floating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss”)
        value -= static_cast<i64>(value);

        double epsilon = 0.5;
        for (size_t i = 0; i < precision; ++i)
            epsilon /= 10.0;

        size_t visible_precision = 0;
        for (; visible_precision < precision; ++visible_precision) {
            if (value - static_cast<i64>(value) < epsilon)
                break;
            value *= 10.0;
            epsilon *= 10.0;
        }

        if (zero_pad || visible_precision > 0)
            string_builder.append('.');

        if (visible_precision > 0)
            format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision);

        if (zero_pad && (precision - visible_precision) > 0)
            format_builder.put_u64(0, base, false, false, true, Align::Right, precision - visible_precision);
    }

    put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
}

void FormatBuilder::put_f80(
    long double value,
    u8 base,
    bool upper_case,
    Align align,
    size_t min_width,
    size_t precision,
    char fill,
    SignMode sign_mode)
{
    StringBuilder string_builder;
    FormatBuilder format_builder { string_builder };

    if (isnan(value) || isinf(value)) [[unlikely]] {
        if (value < 0.0l)
            string_builder.append('-');
        else if (sign_mode == SignMode::Always)
            string_builder.append('+');
        else
            string_builder.append(' ');

        if (isnan(value))
            string_builder.append(upper_case ? "NAN"sv : "nan"sv);
        else
            string_builder.append(upper_case ? "INF"sv : "inf"sv);
        format_builder.put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
        return;
    }

    bool is_negative = value < 0.0l;
    if (is_negative)
        value = -value;

    format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, false, Align::Right, 0, ' ', sign_mode, is_negative);

    if (precision > 0) {
        // FIXME: This is a terrible approximation but doing it properly would be a lot of work. If someone is up for that, a good
        // place to start would be the following video from CppCon 2019:
        // https://youtu.be/4P_kbF0EbZM (Stephan T. Lavavej “Floating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss”)
        value -= static_cast<i64>(value);

        long double epsilon = 0.5l;
        for (size_t i = 0; i < precision; ++i)
            epsilon /= 10.0l;

        size_t visible_precision = 0;
        for (; visible_precision < precision; ++visible_precision) {
            if (value - static_cast<i64>(value) < epsilon)
                break;
            value *= 10.0l;
            epsilon *= 10.0l;
        }

        if (visible_precision > 0) {
            string_builder.append('.');
            format_builder.put_u64(static_cast<u64>(value), base, false, upper_case, true, Align::Right, visible_precision);
        }
    }

    put_string(string_builder.string_view(), align, min_width, NumericLimits<size_t>::max(), fill);
}

#endif

void FormatBuilder::put_hexdump(ReadonlyBytes bytes, size_t width, char fill)
{
    auto put_char_view = [&](auto i) {
        put_padding(fill, 4);
        for (size_t j = i - width; j < i; ++j) {
            auto ch = bytes[j];
            m_builder.append(ch >= 32 && ch <= 127 ? ch : '.'); // silly hack
        }
    };
    for (size_t i = 0; i < bytes.size(); ++i) {
        if (width > 0) {
            if (i % width == 0 && i) {
                put_char_view(i);
                put_literal("\n"sv);
            }
        }
        put_u64(bytes[i], 16, false, false, true, Align::Right, 2);
    }

    if (width > 0 && bytes.size() && bytes.size() % width == 0)
        put_char_view(bytes.size());
}

void vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams params)
{
    FormatBuilder fmtbuilder { builder };
    FormatParser parser { fmtstr };

    vformat_impl(params, fmtbuilder, parser);
}

void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& parser)
{
    if (StringView { "<^>" }.contains(parser.peek(1))) {
        VERIFY(!parser.next_is(is_any_of("{}")));
        m_fill = parser.consume();
    }

    if (parser.consume_specific('<'))
        m_align = FormatBuilder::Align::Left;
    else if (parser.consume_specific('^'))
        m_align = FormatBuilder::Align::Center;
    else if (parser.consume_specific('>'))
        m_align = FormatBuilder::Align::Right;

    if (parser.consume_specific('-'))
        m_sign_mode = FormatBuilder::SignMode::OnlyIfNeeded;
    else if (parser.consume_specific('+'))
        m_sign_mode = FormatBuilder::SignMode::Always;
    else if (parser.consume_specific(' '))
        m_sign_mode = FormatBuilder::SignMode::Reserved;

    if (parser.consume_specific('#'))
        m_alternative_form = true;

    if (parser.consume_specific('0'))
        m_zero_pad = true;

    if (size_t index = 0; parser.consume_replacement_field(index)) {
        if (index == use_next_index)
            index = params.take_next_index();

        m_width = params.parameters().at(index).to_size();
    } else if (size_t width = 0; parser.consume_number(width)) {
        m_width = width;
    }

    if (parser.consume_specific('.')) {
        if (size_t index = 0; parser.consume_replacement_field(index)) {
            if (index == use_next_index)
                index = params.take_next_index();

            m_precision = params.parameters().at(index).to_size();
        } else if (size_t precision = 0; parser.consume_number(precision)) {
            m_precision = precision;
        }
    }

    if (parser.consume_specific('b'))
        m_mode = Mode::Binary;
    else if (parser.consume_specific('B'))
        m_mode = Mode::BinaryUppercase;
    else if (parser.consume_specific('d'))
        m_mode = Mode::Decimal;
    else if (parser.consume_specific('o'))
        m_mode = Mode::Octal;
    else if (parser.consume_specific('x'))
        m_mode = Mode::Hexadecimal;
    else if (parser.consume_specific('X'))
        m_mode = Mode::HexadecimalUppercase;
    else if (parser.consume_specific('c'))
        m_mode = Mode::Character;
    else if (parser.consume_specific('s'))
        m_mode = Mode::String;
    else if (parser.consume_specific('p'))
        m_mode = Mode::Pointer;
    else if (parser.consume_specific('f'))
        m_mode = Mode::Float;
    else if (parser.consume_specific('a'))
        m_mode = Mode::Hexfloat;
    else if (parser.consume_specific('A'))
        m_mode = Mode::HexfloatUppercase;
    else if (parser.consume_specific("hex-dump"))
        m_mode = Mode::HexDump;

    if (!parser.is_eof())
        dbgln("{} did not consume '{}'", __PRETTY_FUNCTION__, parser.remaining());

    VERIFY(parser.is_eof());
}

void Formatter<StringView>::format(FormatBuilder& builder, StringView value)
{
    if (m_sign_mode != FormatBuilder::SignMode::Default)
        VERIFY_NOT_REACHED();
    if (m_alternative_form)
        VERIFY_NOT_REACHED();
    if (m_zero_pad)
        VERIFY_NOT_REACHED();
    if (m_mode != Mode::Default && m_mode != Mode::String && m_mode != Mode::Character && m_mode != Mode::HexDump)
        VERIFY_NOT_REACHED();

    m_width = m_width.value_or(0);
    m_precision = m_precision.value_or(NumericLimits<size_t>::max());

    if (m_mode == Mode::HexDump)
        builder.put_hexdump(value.bytes(), m_width.value(), m_fill);
    else
        builder.put_string(value, m_align, m_width.value(), m_precision.value(), m_fill);
}

void Formatter<FormatString>::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams params)
{
    return Formatter<String>::format(builder, String::vformatted(fmtstr, params));
}

template<typename T>
void Formatter<T, typename EnableIf<IsIntegral<T>>::Type>::format(FormatBuilder& builder, T value)
{
    if (m_mode == Mode::Character) {
        // FIXME: We just support ASCII for now, in the future maybe unicode?
        VERIFY(value >= 0 && value <= 127);

        m_mode = Mode::String;

        Formatter<StringView> formatter { *this };
        return formatter.format(builder, StringView { reinterpret_cast<const char*>(&value), 1 });
    }

    if (m_precision.has_value())
        VERIFY_NOT_REACHED();

    if (m_mode == Mode::Pointer) {
        if (m_sign_mode != FormatBuilder::SignMode::Default)
            VERIFY_NOT_REACHED();
        if (m_align != FormatBuilder::Align::Default)
            VERIFY_NOT_REACHED();
        if (m_alternative_form)
            VERIFY_NOT_REACHED();
        if (m_width.has_value())
            VERIFY_NOT_REACHED();

        m_mode = Mode::Hexadecimal;
        m_alternative_form = true;
        m_width = 2 * sizeof(void*);
        m_zero_pad = true;
    }

    u8 base = 0;
    bool upper_case = false;
    if (m_mode == Mode::Binary) {
        base = 2;
    } else if (m_mode == Mode::BinaryUppercase) {
        base = 2;
        upper_case = true;
    } else if (m_mode == Mode::Octal) {
        base = 8;
    } else if (m_mode == Mode::Decimal || m_mode == Mode::Default) {
        base = 10;
    } else if (m_mode == Mode::Hexadecimal) {
        base = 16;
    } else if (m_mode == Mode::HexadecimalUppercase) {
        base = 16;
        upper_case = true;
    } else if (m_mode == Mode::HexDump) {
        m_width = m_width.value_or(32);
        builder.put_hexdump({ &value, sizeof(value) }, m_width.value(), m_fill);
        return;
    } else {
        VERIFY_NOT_REACHED();
    }

    m_width = m_width.value_or(0);

    if constexpr (IsSame<MakeUnsigned<T>, T>)
        builder.put_u64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, m_width.value(), m_fill, m_sign_mode);
    else
        builder.put_i64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, m_width.value(), m_fill, m_sign_mode);
}

void Formatter<char>::format(FormatBuilder& builder, char value)
{
    if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) {
        // Trick: signed char != char. (Sometimes weird features are actually helpful.)
        Formatter<signed char> formatter { *this };
        return formatter.format(builder, static_cast<signed char>(value));
    } else {
        Formatter<StringView> formatter { *this };
        return formatter.format(builder, { &value, 1 });
    }
}
void Formatter<bool>::format(FormatBuilder& builder, bool value)
{
    if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) {
        Formatter<u8> formatter { *this };
        return formatter.format(builder, static_cast<u8>(value));
    } else if (m_mode == Mode::HexDump) {
        return builder.put_hexdump({ &value, sizeof(value) }, m_width.value_or(32), m_fill);
    } else {
        Formatter<StringView> formatter { *this };
        return formatter.format(builder, value ? "true" : "false");
    }
}
#ifndef KERNEL
void Formatter<long double>::format(FormatBuilder& builder, long double value)
{
    u8 base;
    bool upper_case;
    if (m_mode == Mode::Default || m_mode == Mode::Float) {
        base = 10;
        upper_case = false;
    } else if (m_mode == Mode::Hexfloat) {
        base = 16;
        upper_case = false;
    } else if (m_mode == Mode::HexfloatUppercase) {
        base = 16;
        upper_case = true;
    } else {
        VERIFY_NOT_REACHED();
    }

    m_width = m_width.value_or(0);
    m_precision = m_precision.value_or(6);

    builder.put_f80(value, base, upper_case, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
}

void Formatter<double>::format(FormatBuilder& builder, double value)
{
    u8 base;
    bool upper_case;
    if (m_mode == Mode::Default || m_mode == Mode::Float) {
        base = 10;
        upper_case = false;
    } else if (m_mode == Mode::Hexfloat) {
        base = 16;
        upper_case = false;
    } else if (m_mode == Mode::HexfloatUppercase) {
        base = 16;
        upper_case = true;
    } else {
        VERIFY_NOT_REACHED();
    }

    m_width = m_width.value_or(0);
    m_precision = m_precision.value_or(6);

    builder.put_f64(value, base, upper_case, m_zero_pad, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
}
void Formatter<float>::format(FormatBuilder& builder, float value)
{
    Formatter<double> formatter { *this };
    formatter.format(builder, value);
}
#endif

#ifndef KERNEL
void vout(FILE* file, StringView fmtstr, TypeErasedFormatParams params, bool newline)
{
    StringBuilder builder;
    vformat(builder, fmtstr, params);

    if (newline)
        builder.append('\n');

    const auto string = builder.string_view();
    const auto retval = ::fwrite(string.characters_without_null_termination(), 1, string.length(), file);
    if (static_cast<size_t>(retval) != string.length()) {
        auto error = ferror(file);
        dbgln("vout() failed ({} written out of {}), error was {} ({})", retval, string.length(), error, strerror(error));
    }
}
#endif

static bool is_debug_enabled = true;

void set_debug_enabled(bool value)
{
    is_debug_enabled = value;
}

void vdbgln(StringView fmtstr, TypeErasedFormatParams params)
{
    if (!is_debug_enabled)
        return;

    StringBuilder builder;

#ifdef __serenity__
#    ifdef KERNEL
    if (Kernel::Processor::is_initialized() && Kernel::Thread::current()) {
        auto& thread = *Kernel::Thread::current();
        builder.appendff("\033[34;1m[#{} {}({}:{})]\033[0m: ", Kernel::Processor::id(), thread.process().name(), thread.pid().value(), thread.tid().value());
    } else {
        builder.appendff("\033[34;1m[#{} Kernel]\033[0m: ", Kernel::Processor::id());
    }
#    else
    static TriState got_process_name = TriState::Unknown;
    static char process_name_buffer[256];

    if (got_process_name == TriState::Unknown) {
        if (get_process_name(process_name_buffer, sizeof(process_name_buffer)) == 0)
            got_process_name = TriState::True;
        else
            got_process_name = TriState::False;
    }
    if (got_process_name == TriState::True)
        builder.appendff("\033[33;1m{}({}:{})\033[0m: ", process_name_buffer, getpid(), gettid());
#    endif
#endif

    vformat(builder, fmtstr, params);
    builder.append('\n');

    const auto string = builder.string_view();

    dbgputstr(string.characters_without_null_termination(), string.length());
}

#ifdef KERNEL
void vdmesgln(StringView fmtstr, TypeErasedFormatParams params)
{
    StringBuilder builder;

#    ifdef __serenity__
    if (Kernel::Processor::is_initialized() && Kernel::Thread::current()) {
        auto& thread = *Kernel::Thread::current();
        builder.appendff("\033[34;1m[{}({}:{})]\033[0m: ", thread.process().name(), thread.pid().value(), thread.tid().value());
    } else {
        builder.appendff("\033[34;1m[Kernel]\033[0m: ");
    }
#    endif

    vformat(builder, fmtstr, params);
    builder.append('\n');

    const auto string = builder.string_view();
    kernelputstr(string.characters_without_null_termination(), string.length());
}

void v_critical_dmesgln(StringView fmtstr, TypeErasedFormatParams params)
{
    // FIXME: Try to avoid memory allocations further to prevent faulting
    // at OOM conditions.

    StringBuilder builder;
#    ifdef __serenity__
    if (Kernel::Processor::is_initialized() && Kernel::Thread::current()) {
        auto& thread = *Kernel::Thread::current();
        builder.appendff("[{}({}:{})]: ", thread.process().name(), thread.pid().value(), thread.tid().value());
    } else {
        builder.appendff("[Kernel]: ");
    }
#    endif

    vformat(builder, fmtstr, params);
    builder.append('\n');

    const auto string = builder.string_view();
    kernelcriticalputstr(string.characters_without_null_termination(), string.length());
}

#endif

template struct Formatter<unsigned char, void>;
template struct Formatter<unsigned short, void>;
template struct Formatter<unsigned int, void>;
template struct Formatter<unsigned long, void>;
template struct Formatter<unsigned long long, void>;
template struct Formatter<short, void>;
template struct Formatter<int, void>;
template struct Formatter<long, void>;
template struct Formatter<long long, void>;
template struct Formatter<signed char, void>;

} // namespace AK