diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-26 01:21:12 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-04-26 01:21:12 +0330 |
commit | 85bfc724dd2bdaa2259512c1b8ab21f7dfeca8f1 (patch) | |
tree | 0b9075ba9bf70aca9cdf36e5d909b161460b19a8 /src/utils | |
parent | 6edcfb23ffd49e937395b710f7c6213b2c0d93cf (diff) |
clean up
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/file.c | 17 | ||||
-rw-r--r-- | src/utils/log.c | 13 | ||||
-rw-r--r-- | src/utils/log.h | 1 | ||||
-rw-r--r-- | src/utils/memory.c | 2 | ||||
-rw-r--r-- | src/utils/memory.h | 2 | ||||
-rw-r--r-- | src/utils/string.c | 38 | ||||
-rw-r--r-- | src/utils/string.h | 7 | ||||
-rw-r--r-- | src/utils/type.h | 18 |
8 files changed, 64 insertions, 34 deletions
diff --git a/src/utils/file.c b/src/utils/file.c index 55a438c..61165d2 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -2,8 +2,8 @@ #include "utils/log.h" #include "utils/memory.h" +#include "utils/string.h" #include <stdio.h> -#include <string.h> size_t fileCodes_capacity = 0; char **fileCodes = NULL; @@ -36,10 +36,13 @@ void filePush(const char *filePath, char *code) { fileCodes_names = a404m_realloc( fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names)); } + size_t filePath_length = strLength(filePath); fileCodes[fileCodes_length] = code; fileCodes_names[fileCodes_length] = - a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names)); - strcpy(fileCodes_names[fileCodes_length], filePath); + a404m_malloc((filePath_length + 1) * sizeof(**fileCodes_names)); + for (size_t i = 0; i < filePath_length; ++i) { + fileCodes_names[fileCodes_length][i] = filePath[i]; + } fileCodes_length += 1; } @@ -73,7 +76,7 @@ char *readWholeFile(const char *filePath) { size_t getFileIndex(const char *filePath) { for (size_t i = 0; i < fileCodes_length; ++i) { const char *str = fileCodes_names[i]; - if (strcmp(filePath, str) == 0) { + if (strEquals(filePath, str)) { return i; } } @@ -89,9 +92,11 @@ char *joinToPathOf(const char *original, const char *file) { } } char *result = - a404m_malloc((result_size + 1 + strlen(file) + 1) * sizeof(*result)); + a404m_malloc((result_size + 1 + strLength(file) + 1) * sizeof(*result)); - strncpy(result, original, result_size); + for (size_t i = 0; i < result_size; ++i) { + result[i] = original[i]; + } result[result_size++] = '/'; for (size_t i = 0; file[i] != '\0'; ++i) { diff --git a/src/utils/log.c b/src/utils/log.c index 898517d..cab0c48 100644 --- a/src/utils/log.c +++ b/src/utils/log.c @@ -1,10 +1,9 @@ #include "log.h" #include "utils/file.h" +#include "utils/string.h" #include <stdarg.h> -#include <stdint.h> #include <stdio.h> -#include <string.h> void _printLogBack(const char *format, const char *file, int line, ...) { va_list args; @@ -26,7 +25,7 @@ void _printErrorWarningBack(const char *file, int line, char *begin, char *end, size_t file_index = SIZE_MAX; for (size_t i = 0; i < fileCodes_length; ++i) { char *fileCode_begin = fileCodes[i]; - char *fileCode_end = fileCodes[i] + strlen(fileCodes[i]); + char *fileCode_end = fileCodes[i] + strLength(fileCodes[i]); if (begin >= fileCode_begin && end <= fileCode_end) { file_index = i; break; @@ -70,11 +69,11 @@ void _printErrorWarningBack(const char *file, int line, char *begin, char *end, for (char *iter = file_line_begin; iter < file_line_end; ++iter) { if (iter == begin) { - fprintf(stderr, "%s", secondColor); + fputs(secondColor, stderr); } else if (iter == end) { - fprintf(stderr, "%s", FIRST_COLOR); + fputs(FIRST_COLOR, stderr); } - fprintf(stderr, "%c", *iter); + fputc(*iter, stderr); } - fprintf(stderr, "\e[0m\n"); + fputs("\e[0m\n", stderr); } diff --git a/src/utils/log.h b/src/utils/log.h index bcc7aa1..913fd34 100644 --- a/src/utils/log.h +++ b/src/utils/log.h @@ -1,6 +1,5 @@ #pragma once -#include "utils/type.h" #include <stdlib.h> #ifndef __FILE_NAME__ diff --git a/src/utils/memory.c b/src/utils/memory.c index bf3de92..0c9740c 100644 --- a/src/utils/memory.c +++ b/src/utils/memory.c @@ -10,7 +10,7 @@ void *a404m_malloc(size_t size) { } } -void *a404m_realloc(void *restrict pointer, size_t size) { +void *a404m_realloc(void *pointer, size_t size) { if (size == 0) { free(pointer); return NULL; diff --git a/src/utils/memory.h b/src/utils/memory.h index 29bd3db..132fd2b 100644 --- a/src/utils/memory.h +++ b/src/utils/memory.h @@ -3,5 +3,5 @@ #include <stddef.h> extern void *a404m_malloc(size_t size); -extern void *a404m_realloc(void *restrict pointer, size_t size); +extern void *a404m_realloc(void *pointer, size_t size); extern size_t a404m_malloc_usable_size(void *pointer); diff --git a/src/utils/string.c b/src/utils/string.c index 55d2c3e..55e6236 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -2,15 +2,37 @@ #include "memory.h" #include "utils/type.h" -#include <stdint.h> -#include <stdio.h> -#include <string.h> +size_t strLength(const char *str) { + size_t i = 0; + for (; str[i] != '\0'; ++i) + ; + return i; +} + +bool strEquals(const char *left, const char *right) { + size_t i = 0; + for (; left[i] != '\0' && right[i] != '\0'; ++i) { + if (left[i] != right[i]) { + return false; + } + } + return left[i] == right[i]; +} + +bool strnEquals(const char *left, const char *right, size_t len) { + for (size_t i = 0; i < len; ++i) { + if (left[i] != right[i]) { + return false; + } + } + return true; +} size_t searchInStringArray(const char *array[], size_t array_size, const char *str, size_t str_size) { for (size_t i = 0; i < array_size; ++i) { - const size_t el_size = strlen(array[i]); - if (el_size == str_size && strncmp(array[i], str, str_size) == 0) { + const size_t el_size = strLength(array[i]); + if (el_size == str_size && strnEquals(array[i], str, str_size)) { return i; } } @@ -87,8 +109,10 @@ char *u64ToString(u64 value) { } char *strClone(const char *str) { - const size_t str_length = strlen(str) + 1; + const size_t str_length = strLength(str) + 1; char *result = a404m_malloc(str_length * sizeof(*result)); - strncpy(result, str, str_length); + for (size_t i = 0; i < str_length; ++i) { + result[i] = str[i]; + } return result; } diff --git a/src/utils/string.h b/src/utils/string.h index a5a0319..56f6825 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -4,11 +4,16 @@ #include <stddef.h> #include <stdint.h> +size_t strLength(const char *str); + +bool strEquals(const char *left, const char *right); +bool strnEquals(const char *left, const char *right, size_t len); + size_t searchInStringArray(const char *array[], size_t array_size, const char *str, size_t str_size); u64 decimalToU64(char *str_begin, char *str_end, bool *success); f128 numberToFloat(char *str_begin, char *str_end, bool *success); -char* u64ToString(u64 value); +char *u64ToString(u64 value); char *strClone(const char *str); diff --git a/src/utils/type.h b/src/utils/type.h index 4c61bc3..3a0e59e 100644 --- a/src/utils/type.h +++ b/src/utils/type.h @@ -1,20 +1,18 @@ #pragma once -#include <stdint.h> - #ifdef __FLT16_MIN__ #define FLOAT_16_SUPPORT #endif -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; +typedef char i8; +typedef short i16; +typedef int i32; +typedef long long i64; -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; #ifdef FLOAT_16_SUPPORT typedef _Float16 f16; |