Add iota and atoi functions
This commit is contained in:
parent
97341b9c46
commit
e8cef8bd59
7
src/kernel/include/stdlib.h
Normal file
7
src/kernel/include/stdlib.h
Normal 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
66
src/kernel/lib/stdlib.c
Normal 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;
|
||||
}
|
||||
|
Loading…
Reference in a new issue