diff options
Diffstat (limited to 'base/base.c')
| -rw-r--r-- | base/base.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/base/base.c b/base/base.c new file mode 100644 index 0000000..ba8ba32 --- /dev/null +++ b/base/base.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #include "base/base.h" | ||
| 2 | #include "stddef.h" | ||
| 3 | |||
| 4 | /** | ||
| 5 | * Helper function to parse strings to int using ascii codes | ||
| 6 | * */ | ||
| 7 | local_internal u64 | ||
| 8 | parse_u64(char *buf, size_t len) | ||
| 9 | { | ||
| 10 | u64 value = 0; | ||
| 11 | |||
| 12 | for ( | ||
| 13 | size_t buffer_idx = 0; | ||
| 14 | buffer_idx < len; | ||
| 15 | ++buffer_idx) | ||
| 16 | { | ||
| 17 | char c = buf[buffer_idx]; | ||
| 18 | if (c < '0' || c > '9') | ||
| 19 | { | ||
| 20 | break; | ||
| 21 | } | ||
| 22 | value = value * 10 + (c - '0'); | ||
| 23 | } | ||
| 24 | |||
| 25 | return value; | ||
| 26 | } | ||
| 27 | |||
| 28 | /* | ||
| 29 | * is_numeric - Check if a string contains only digits | ||
| 30 | * @s: String to check | ||
| 31 | * | ||
| 32 | * Return: 1 if string contains only numeric characters, 0 otherwise | ||
| 33 | */ | ||
| 34 | local_internal b8 | ||
| 35 | is_numeric(char *s) | ||
| 36 | { | ||
| 37 | for (; *s; ++s) | ||
| 38 | { | ||
| 39 | if (*s < '0' || *s > '9') | ||
| 40 | { | ||
| 41 | return 0; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | return 1; | ||
| 45 | } | ||
| 46 | |||
| 47 | local_internal b8 | ||
| 48 | compare_string(const char *c1, const char *c2) | ||
| 49 | { | ||
| 50 | if (sizeof(c1) != sizeof(c2)) | ||
| 51 | { | ||
| 52 | return -1; | ||
| 53 | } | ||
| 54 | |||
| 55 | for ( | ||
| 56 | u64 word_idx = 0; | ||
| 57 | word_idx <= sizeof(*c1); | ||
| 58 | ++word_idx) | ||
| 59 | { | ||
| 60 | if (*c1 != *c2) | ||
| 61 | { | ||
| 62 | return -1; | ||
| 63 | } | ||
| 64 | ++c1; | ||
| 65 | ++c2; | ||
| 66 | } | ||
| 67 | |||
| 68 | return 0; | ||
| 69 | } | ||
