lib/libc: add misc utilities

This commit is contained in:
xvanc 2023-09-13 09:22:50 -05:00 committed by mintsuki
parent 2200161e7d
commit 09c430f286
2 changed files with 23 additions and 0 deletions

View File

@ -6,6 +6,8 @@
bool isprint(int c); bool isprint(int c);
bool isspace(int c); bool isspace(int c);
bool isalpha(int c);
bool isdigit(int c);
int toupper(int c); int toupper(int c);
int tolower(int c); int tolower(int c);
@ -23,6 +25,7 @@ size_t strlen(const char *);
int strcmp(const char *, const char *); int strcmp(const char *, const char *);
int strcasecmp(const char *, const char *); int strcasecmp(const char *, const char *);
int strncmp(const char *, const char *, size_t); int strncmp(const char *, const char *, size_t);
int strncasecmp(const char *, const char *, size_t);
int inet_pton(const char *src, void *dst); int inet_pton(const char *src, void *dst);
#endif #endif

View File

@ -12,6 +12,14 @@ bool isspace(int c) {
return (c >= '\t' && c <= 0xD) || 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) { int toupper(int c) {
if (c >= 'a' && c <= 'z') { if (c >= 'a' && c <= 'z') {
return c - 0x20; return c - 0x20;
@ -84,6 +92,18 @@ int strncmp(const char *s1, const char *s2, size_t n) {
return 0; 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 strlen(const char *str) {
size_t len; size_t len;