From 09c430f2866b0457cd561d01aac5a7a9b7d6fbe6 Mon Sep 17 00:00:00 2001 From: xvanc Date: Wed, 13 Sep 2023 09:22:50 -0500 Subject: [PATCH] lib/libc: add misc utilities --- common/lib/libc.h | 3 +++ common/lib/libc.s2.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/common/lib/libc.h b/common/lib/libc.h index 19b50fc9..551d91a1 100644 --- a/common/lib/libc.h +++ b/common/lib/libc.h @@ -6,6 +6,8 @@ bool isprint(int c); bool isspace(int c); +bool isalpha(int c); +bool isdigit(int c); int toupper(int c); int tolower(int c); @@ -23,6 +25,7 @@ size_t strlen(const char *); int strcmp(const char *, const char *); int strcasecmp(const char *, const char *); int strncmp(const char *, const char *, size_t); +int strncasecmp(const char *, const char *, size_t); int inet_pton(const char *src, void *dst); #endif diff --git a/common/lib/libc.s2.c b/common/lib/libc.s2.c index c3eda16d..11bd4461 100644 --- a/common/lib/libc.s2.c +++ b/common/lib/libc.s2.c @@ -12,6 +12,14 @@ bool isspace(int c) { return (c >= '\t' && c <= 0xD) || c == ' '; } +bool isalpha(int c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +bool isdigit(int c) { + return c >= '0' && c <= '9'; +} + int toupper(int c) { if (c >= 'a' && c <= 'z') { return c - 0x20; @@ -84,6 +92,18 @@ int strncmp(const char *s1, const char *s2, size_t n) { return 0; } +int strncasecmp(const char *s1, const char *s2, size_t n) { + for (size_t i = 0; i < n; i++) { + char c1 = s1[i], c2 = s2[i]; + if (tolower(c1) != tolower(c2)) + return c1 < c2 ? -1 : 1; + if (!c1) + return 0; + } + + return 0; +} + size_t strlen(const char *str) { size_t len;