Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix str[n]len implementations #39

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions libc/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,14 @@ char *strcpy(char *dst, const char *src)
size_t strlen(const char *s)
{
const char *it = s;
while (*(++it) != 0) {}
return ((it - s) < 0) ? 0 : (size_t)(it - s);
for (; *it; it++);
return (size_t)(it - s);
}

size_t strnlen(const char *s, size_t count)
{
const char *it = s;
while ((*(++it) != 0) && --count) {}
return ((it - s) < 0) ? 0 : (size_t)(it - s);
const char *p = memchr(s, 0, count);
return p ? (size_t)(p-s) : count;
}

int strcmp(const char *s1, const char *s2)
Expand Down
9 changes: 4 additions & 5 deletions mentos/src/klib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,14 @@ char *strcpy(char *dst, const char *src)
size_t strlen(const char *s)
{
const char *it = s;
while (*(++it) != 0) {}
return ((it - s) < 0) ? 0 : (size_t)(it - s);
for(; *it; it++);
return (size_t)(it - s);
}

size_t strnlen(const char *s, size_t count)
{
const char *it = s;
while ((*(++it) != 0) && --count) {}
return ((it - s) < 0) ? 0 : (size_t)(it - s);
const char *p = memchr(s, 0, count);
return p ? (size_t)(p-s) : count;
}

int strcmp(const char *s1, const char *s2)
Expand Down
Loading