diff options
author | Marvin Borner | 2020-06-17 18:31:46 +0200 |
---|---|---|
committer | Marvin Borner | 2020-06-17 18:31:46 +0200 |
commit | eed77bd2970a00d1394ed027ceca5b646e4671ce (patch) | |
tree | c44643d98aed2b6818f2b33417c0dea9c5853094 /src/kernel/lib/stdlib | |
parent | 49dfa1f4021026bf7c4d77817959c8aa24067016 (diff) |
Started rewrite
Diffstat (limited to 'src/kernel/lib/stdlib')
-rw-r--r-- | src/kernel/lib/stdlib/atoi.c | 28 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/htoa.c | 31 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/htoi.c | 23 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/itoa.c | 73 |
4 files changed, 0 insertions, 155 deletions
diff --git a/src/kernel/lib/stdlib/atoi.c b/src/kernel/lib/stdlib/atoi.c deleted file mode 100644 index b90958a..0000000 --- a/src/kernel/lib/stdlib/atoi.c +++ /dev/null @@ -1,28 +0,0 @@ -#include <lib/math.h> -#include <lib/string.h> -#include <stddef.h> -#include <stdint.h> - -int atoi(char *str) -{ - u32 s_str = strlen(str); - if (!s_str) - return 0; - - u8 negative = 0; - if (str[0] == '-') - negative = 1; - - u32 i = 0; - if (negative) - i++; - - int ret = 0; - for (; i < s_str; i++) { - ret += (str[i] - '0') * pow(10, (int)((s_str - i) - 1)); - } - - if (negative) - ret *= -1; - return ret; -}
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoa.c b/src/kernel/lib/stdlib/htoa.c deleted file mode 100644 index 9cf57cc..0000000 --- a/src/kernel/lib/stdlib/htoa.c +++ /dev/null @@ -1,31 +0,0 @@ -#include <lib/string.h> -#include <memory/alloc.h> -#include <stdint.h> - -static const char HTOA_TABLE[] = "0123456789ABCDEF"; - -char *htoa(u32 n) -{ - char *ret = (char *)malloc(10); - - int i = 0; - while (n) { - ret[i++] = HTOA_TABLE[n & 0xF]; - n >>= 4; - } - - if (!i) { - ret[0] = '0'; - i++; - } - - for (; i <= 9; i++) - ret[i] = 0; - - char *aux = strdup(ret); - free(ret); - ret = aux; - - strinv(ret); - return ret; -}
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoi.c b/src/kernel/lib/stdlib/htoi.c deleted file mode 100644 index 367d30e..0000000 --- a/src/kernel/lib/stdlib/htoi.c +++ /dev/null @@ -1,23 +0,0 @@ -#include <lib/math.h> -#include <lib/string.h> -#include <stddef.h> - -int htoi(char *str) -{ - u32 s_str = strlen(str); - - u32 i = 0; - int ret = 0; - for (; i < s_str; i++) { - char c = str[i]; - int aux = 0; - if (c >= '0' && c <= '9') - aux = c - '0'; - else if (c >= 'A' && c <= 'F') - aux = (c - 'A') + 10; - - ret += aux * pow(16, (int)((s_str - i) - 1)); - } - - return ret; -}
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c deleted file mode 100644 index b15befd..0000000 --- a/src/kernel/lib/stdlib/itoa.c +++ /dev/null @@ -1,73 +0,0 @@ -#include <lib/math.h> -#include <lib/string.h> -#include <memory/alloc.h> -#include <memory/paging.h> -#include <stdint.h> - -static const char ITOA_TABLE[] = "0123456789"; - -char *itoa(int n) -{ - if (!n) { - char *ret = (char *)malloc(2); - ret[0] = '0'; - ret[1] = 0; - return ret; - } - u8 negative = (u8)(n < 0); - if (negative) - n *= -1; - - int sz; - for (sz = 0; n % pow(10, sz) != n; sz++) { - } - - char *ret = (char *)malloc((u32)(sz + 1)); - - for (int i = 0; i < sz; i++) { - int digit = (n % pow(10, i + 1)) / pow(10, i); - ret[i] = ITOA_TABLE[digit]; - } - ret[sz] = 0; - - if (negative) { - char *aux = (char *)malloc((u32)(sz + 2)); - strcpy(aux, ret); - aux[sz] = '-'; - aux[sz + 1] = 0; - free(ret); - ret = aux; - } - - strinv(ret); - return ret; -} - -// TODO: Rename itoa_base -char *itoa_base(int value, char *result, int base, int is_signed) -{ - if (base < 2 || base > 36) { - *result = '\0'; - return result; - } - - char *ptr = result, *ptr1 = result, tmp_char; - int tmp_value; - - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" - [35 + (tmp_value - value * base)]; - } while (value); - - if (is_signed && tmp_value < 0) - *ptr++ = '-'; - *ptr-- = '\0'; - while (ptr1 < ptr) { - tmp_char = *ptr; - *ptr-- = *ptr1; - *ptr1++ = tmp_char; - } - return result; -}
\ No newline at end of file |