feat(string): complete string.h + strdup/memccpy
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* True when c appears anywhere in set (including as the NUL of set — but set
|
||||
* is a string, so scanning stops at that NUL, as intended).
|
||||
*/
|
||||
static int
|
||||
str_in_set(const char *set, char c) // NOLINT(bugprone-easily-swappable-parameters)
|
||||
{
|
||||
for (; *set != '\0'; set++)
|
||||
{
|
||||
if (*set == c)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the length of the initial span of s consisting entirely of bytes
|
||||
* that occur in accept.
|
||||
*/
|
||||
size_t
|
||||
strspn(const char *s, const char *accept) // NOLINT(bugprone-easily-swappable-parameters)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
while (s[n] != '\0' && str_in_set(accept, s[n]))
|
||||
{
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the length of the initial span of s consisting entirely of bytes
|
||||
* that do NOT occur in reject.
|
||||
*/
|
||||
size_t
|
||||
strcspn(const char *s, const char *reject) // NOLINT(bugprone-easily-swappable-parameters)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
while (s[n] != '\0' && !str_in_set(reject, s[n]))
|
||||
{
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a pointer to the first byte in s that also occurs in accept, or
|
||||
* NULL when none occurs.
|
||||
*/
|
||||
char *
|
||||
strpbrk(const char *s, const char *accept) // NOLINT(bugprone-easily-swappable-parameters)
|
||||
{
|
||||
for (; *s != '\0'; s++)
|
||||
{
|
||||
if (str_in_set(accept, *s))
|
||||
{
|
||||
return (char *)s;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user