Add iota and atoi functions

This commit is contained in:
Robert 2023-09-05 23:12:06 +02:00
parent 97341b9c46
commit e8cef8bd59
2 changed files with 73 additions and 0 deletions

View file

@ -0,0 +1,7 @@
#ifndef _CLIPPER_STDLIB_H_
#define _CLIPPER_STDLIB_H_
char* itoa(int value, char* buffer, int base);
char* utoa(unsigned int value, char* buffer, int base);
#endif // _CLIPPER_STDLIB_H_

66
src/kernel/lib/stdlib.c Normal file
View file

@ -0,0 +1,66 @@
#include "stdlib.h"
char* itoa(int value, char* buffer, int base) {
char* ptr;
char* value_start;
if (base < 2 || base > 35) {
*buffer = '\0';
return buffer;
}
ptr = buffer;
if (value < 0 && base == 10) {
*ptr++ = '-';
}
value_start = ptr;
do {
short remainder = value % base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + remainder];
value /= base;
} while(value);
*ptr-- = '\0';
while (value_start < ptr) {
char tmp = *ptr;
*ptr-- = *value_start;
*value_start++ = tmp;
}
return buffer;
}
char* utoa(unsigned int value, char* buffer, int base) {
char* ptr;
char* value_start;
if (base < 2 || base > 35) {
*buffer = '\0';
return buffer;
}
ptr = value_start = buffer;
do {
short remainder = value % base;
*ptr++ = "0123456789abcdefghijklmnopqrstuvwxyz"[remainder];
value /= base;
} while(value != 0);
*ptr-- = '\0';
while (value_start < ptr) {
char tmp = *ptr;
*ptr-- = *value_start;
*value_start++ = tmp;
}
return buffer;
}