From 41ee6b43c89ce67808a684ba67f69e964b0636fa Mon Sep 17 00:00:00 2001 From: aqua Date: Sat, 18 Feb 2023 10:12:24 +0200 Subject: Move C stdlib to lib/libk --- lib/libk/string/itoa.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/libk/string/itoa.c (limited to 'lib/libk/string') diff --git a/lib/libk/string/itoa.c b/lib/libk/string/itoa.c new file mode 100644 index 0000000..2db9768 --- /dev/null +++ b/lib/libk/string/itoa.c @@ -0,0 +1,30 @@ +#include +#include + +static const char *numbers = "0123456789abcdef"; + +char * +utoa(char *p, unsigned x, int base) +{ + p += 3 * sizeof(unsigned); + *--p = '\0'; + + do { + *--p = numbers[x % base]; + x /= base; + } while (x); + + return p; +} + +char * +itoa(char *p, int x, int base) +{ + const bool is_negative = (x < 0); + if (is_negative) x = -x; + + p = utoa(p, x, base); + + if (is_negative) *--p = '-'; + return p; +} -- cgit v1.2.1