diff options
author | QMK Bot <hello@qmk.fm> | 2022-04-21 21:49:28 +0000 |
---|---|---|
committer | QMK Bot <hello@qmk.fm> | 2022-04-21 21:49:28 +0000 |
commit | f44d569144c3303dd0db6be94b5d6e7c7942577a (patch) | |
tree | 9718242eaaf87bad798abde6f79dadc839d83129 /users/mtei/pseudo_sprintf.c | |
parent | 8f692e22e3ce176671bbdde51478ce0e6a57875b (diff) | |
parent | c2939bf03833e013742ecc74d8229832c88656c2 (diff) |
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'users/mtei/pseudo_sprintf.c')
-rw-r--r-- | users/mtei/pseudo_sprintf.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/users/mtei/pseudo_sprintf.c b/users/mtei/pseudo_sprintf.c new file mode 100644 index 0000000000..446e3efc65 --- /dev/null +++ b/users/mtei/pseudo_sprintf.c @@ -0,0 +1,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); +} |