summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/string.c39
-rw-r--r--src/utils/string.h6
-rw-r--r--src/utils/type.c21
-rw-r--r--src/utils/type.h20
4 files changed, 81 insertions, 5 deletions
diff --git a/src/utils/string.c b/src/utils/string.c
index 5f9b7de..ee8a354 100644
--- a/src/utils/string.c
+++ b/src/utils/string.c
@@ -1,5 +1,6 @@
#include "string.h"
#include "memory.h"
+#include "utils/type.h"
#include <stdint.h>
#include <string.h>
@@ -15,8 +16,8 @@ size_t searchInStringArray(const char *array[], size_t array_size,
return array_size;
}
-uint64_t decimalToU64(char *str_begin, char *str_end, bool *success) {
- uint64_t result = 0;
+u64 decimalToU64(char *str_begin, char *str_end, bool *success) {
+ u64 result = 0;
while (str_begin < str_end) {
if (*str_begin < '0' || *str_begin > '9') {
@@ -32,7 +33,39 @@ uint64_t decimalToU64(char *str_begin, char *str_end, bool *success) {
return result;
}
-char *u64ToString(uint64_t value) {
+f128 numberToFloat(char *str_begin, char *str_end, bool *success) {
+ f64 left = 0;
+ f64 right = 0;
+ bool isPastPoint = false;
+
+ while (str_begin < str_end) {
+ char c;
+ if (isPastPoint) {
+ c = *--str_end;
+ } else {
+ c = *str_begin++;
+ }
+ if (c >= '0' && c <= '9') {
+ if (isPastPoint) {
+ left *= 10;
+ left += c - '0';
+ } else {
+ right /= 10;
+ right += c - '0';
+ }
+ } else if (c == '.' && !isPastPoint) {
+ isPastPoint = true;
+ } else {
+ *success = false;
+ return 0;
+ }
+ }
+
+ *success = true;
+ return left + right;
+}
+
+char *u64ToString(u64 value) {
char *str = a404m_malloc(21 * sizeof(*str));
size_t i = 0;
diff --git a/src/utils/string.h b/src/utils/string.h
index 8f92bc4..54130e5 100644
--- a/src/utils/string.h
+++ b/src/utils/string.h
@@ -1,10 +1,12 @@
#pragma once
+#include "utils/type.h"
#include <stddef.h>
#include <stdint.h>
size_t searchInStringArray(const char *array[], size_t array_size,
const char *str, size_t str_size);
-uint64_t decimalToU64(char *str_begin, char *str_end, bool *success);
-char* u64ToString(uint64_t value);
+u64 decimalToU64(char *str_begin, char *str_end, bool *success);
+f128 numberToFloat(char *str_begin, char *str_end, bool *success);
+char* u64ToString(u64 value);
diff --git a/src/utils/type.c b/src/utils/type.c
new file mode 100644
index 0000000..dea08ef
--- /dev/null
+++ b/src/utils/type.c
@@ -0,0 +1,21 @@
+#include "type.h"
+
+#include <assert.h>
+
+void checkTypes() {
+ assert(sizeof(i8) == (8 / 8));
+ assert(sizeof(i16) == (16 / 8));
+ assert(sizeof(i32) == (32 / 8));
+ assert(sizeof(i64) == (64 / 8));
+
+ assert(sizeof(u8) == (8 / 8));
+ assert(sizeof(u16) == (16 / 8));
+ assert(sizeof(u32) == (32 / 8));
+ assert(sizeof(u64) == (64 / 8));
+
+ assert(sizeof(f16) == (16 / 8));
+ assert(sizeof(f32) == (32 / 8));
+ assert(sizeof(f64) == (64 / 8));
+ assert(sizeof(f128) == (128 / 8));
+}
+
diff --git a/src/utils/type.h b/src/utils/type.h
new file mode 100644
index 0000000..aad46df
--- /dev/null
+++ b/src/utils/type.h
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <stdint.h>
+
+typedef int8_t i8;
+typedef int16_t i16;
+typedef int32_t i32;
+typedef int64_t i64;
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+typedef _Float16 f16;
+typedef float f32;
+typedef double f64;
+typedef long double f128;
+
+void checkTypes();