summaryrefslogtreecommitdiff
path: root/users/mtei/pseudo_sprintf.c
blob: 446e3efc655a8fac8bee89f481c6f3babf97b7ae (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
// Copyright 2022 Takeshi Ishii (@mtei)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "pseudo_sprintf.h"

static char *sprint_decimal(char *buf, int data) {
    if (data > 9) {
        buf = sprint_decimal(buf, data/10);
    }
    *buf++ = "0123456789"[data%10];
    *buf = '\0';
    return buf;
}

static char *sprint_hex(char *buf, uint32_t data) {
    if (data > 0xf) {
        buf = sprint_hex(buf, data/0x10);
    }
    *buf++ = "0123456789abcdef"[data & 0xf];
    *buf = '\0';
    return buf;
}

char *sprints(char *buf, char *src) {
    while (*src) {
        *buf++ = *src++;
    }
    *buf = '\0';
    return buf;
}

char *sprintx(char *buf, char *leadstr, uint32_t data) {
    buf = sprints(buf, leadstr);
    buf = sprint_hex(buf, data);
    return buf;
}

char *sprintd(char *buf, char *leadstr, int data) {
    buf = sprints(buf, leadstr);
    buf = sprint_decimal(buf, data);
    return buf;
}

char *sprint2d(char *buf, char *leadstr, int data) {
    buf = sprints(buf, leadstr);
    if (data > 99) {
        return sprint_decimal(buf, data);
    }
    if (data < 10) {
        *buf++ = ' ';
    }
    return sprint_decimal(buf, data);
}