feat(string): complete string.h + strdup/memccpy
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Append src (including its terminating NUL) to the end of dst; return dst.
|
||||
* The strings must not overlap (restrict).
|
||||
*
|
||||
* See strcpy.c: the copy loop is a recognizable pattern, so disable
|
||||
* -ftree-loop-distribute-patterns here too.
|
||||
*/
|
||||
__attribute__((optimize("no-tree-loop-distribute-patterns"))) char *
|
||||
strcat(char *restrict dst, const char *restrict src) // NOLINT(bugprone-*)
|
||||
{
|
||||
char *d = dst;
|
||||
|
||||
/* Find the terminating NUL of dst. */
|
||||
while (*d != '\0')
|
||||
{
|
||||
d++;
|
||||
}
|
||||
|
||||
/* Append src including its NUL. */
|
||||
for (;;)
|
||||
{
|
||||
*d++ = *src;
|
||||
if (*src == '\0')
|
||||
{
|
||||
return dst;
|
||||
}
|
||||
src++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Append at most n bytes of src to dst and always NUL-terminate; return dst.
|
||||
*
|
||||
* See strcpy.c: the copy loop is a recognizable pattern, so disable
|
||||
* -ftree-loop-distribute-patterns here too.
|
||||
*/
|
||||
__attribute__((optimize("no-tree-loop-distribute-patterns"))) char *
|
||||
strncat(char *restrict dst, const char *restrict src, size_t n) // NOLINT(bugprone-*)
|
||||
{
|
||||
char *d = dst;
|
||||
|
||||
/* Find the terminating NUL of dst. */
|
||||
while (*d != '\0')
|
||||
{
|
||||
d++;
|
||||
}
|
||||
|
||||
/* Append at most n bytes of src, stopping early at its NUL. */
|
||||
while (n != 0 && *src != '\0')
|
||||
{
|
||||
*d++ = *src++;
|
||||
n--;
|
||||
}
|
||||
|
||||
/* strncat always NUL-terminates. */
|
||||
*d = '\0';
|
||||
|
||||
return dst;
|
||||
}
|
||||
Reference in New Issue
Block a user