diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 21:11:50 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-06-19 21:11:50 +0200 |
commit | dfe8ddb9ff8b3f90036a0e47220b6c180cc95ea7 (patch) | |
tree | 7b8ca0aa82b923d384f234b7d3364d1f752692c0 /src/str.c | |
parent | 47984162974a8f7d9903d352567005ac569c5a87 (diff) | |
download | silly_game-dfe8ddb9ff8b3f90036a0e47220b6c180cc95ea7.tar.xz |
str.c: fix str_cmp and add str_eq
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/str.c')
-rw-r--r-- | src/str.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -6,12 +6,24 @@ #include <string.h> #include "str.h" -int str_cmp(str s1, str s2) +bool str_eq(str s1, str s2) { if (s1.len != s2.len) - return (int) s1.len - (int) s2.len; + return false; + + return memcmp(s1.data, s2.data, s1.len) == 0; +} + - return memcmp(s1.data, s2.data, s1.len); +int str_cmp(str s1, str s2) +{ + size_t min_len = s1.len < s2.len ? s1.len : s2.len; + int cmp = memcmp(s1.data, s2.data, min_len); + + if (cmp == 0) + return (int) s1.len - (int) s2.len; + else + return cmp; } static bool match_char(char c, str tokens) |