aboutsummaryrefslogtreecommitdiff
path: root/lib/libk/string
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2023-02-18 10:12:24 +0200
committeraqua <aqua@iserlohn-fortress.net>2023-02-18 10:12:24 +0200
commit41ee6b43c89ce67808a684ba67f69e964b0636fa (patch)
treefadee7301456711d567df030793c568a745bb522 /lib/libk/string
parentGenerate dependency files for source code (diff)
downloadkernel-41ee6b43c89ce67808a684ba67f69e964b0636fa.tar.xz
Move C stdlib to lib/libk
Diffstat (limited to 'lib/libk/string')
-rw-r--r--lib/libk/string/itoa.c30
1 files changed, 30 insertions, 0 deletions
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 <stdbool.h>
+#include <string.h>
+
+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;
+}