feat(string): complete string.h + strdup/memccpy

This commit is contained in:
2026-09-03 20:05:49 -04:00
parent 20f514d91c
commit 06d2ec7c34
19 changed files with 1860 additions and 2 deletions
+52
View File
@@ -0,0 +1,52 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
/*
* Return a pointer to the first occurrence of c (converted to char) in s,
* or NULL when absent. The terminating NUL is part of the string, so
* strchr(s, '\0') returns a pointer to it.
*/
char *
strchr(const char *s, int c) // NOLINT(bugprone-easily-swappable-parameters)
{
const char cc = (char)c;
for (;; s++)
{
if (*s == cc)
{
return (char *)s;
}
if (*s == '\0')
{
return NULL;
}
}
}
/*
* Return a pointer to the last occurrence of c (converted to char) in s, or
* NULL when absent. The terminating NUL is part of the string, so
* strrchr(s, '\0') returns a pointer to it.
*/
char *
strrchr(const char *s, int c) // NOLINT(bugprone-easily-swappable-parameters)
{
const char cc = (char)c;
const char *last = NULL;
for (;; s++)
{
if (*s == cc)
{
last = s;
}
if (*s == '\0')
{
return (char *)last;
}
}
}