Fri, 06 Jun 2025 15:17:12 +0200
2 files changed,
60 insertions(+),
3 deletions(-)
M
common/string.c
→
common/string.c
@@ -1,5 +1,6 @@
+#include <stdint.h> + #include "string.h" -#include <stdint.h> void *memcpy (void *destination, const void *source, size_t num) { if (destination >= source) {@@ -67,4 +68,56 @@ }
i++; } -}+} + +/** + * Creates a copy of the string @p src into buffer @p dest with whitespace + * stripped from the left side. Truncates the string if the buffer is too small. + * + * @param src Source string to be copied + * @param dest Destination buffer to copy the stripped string into + * @param bufflen Length of buffer dest + * + * @return Length of the resulting string + */ +size_t strlstrip(char *dest, const char *src, size_t bufflen) { + /* skip whitespace at the start */ + while (WHITESPACE(*src)) { + src++; + } + + /* copy until string stops */ + size_t i = 0; + for (; src[i] != 0 && i < bufflen; i++) { + dest[i] = src[i]; + } + + dest[i] = 0; + return i; +} + +/** + * Creates a copy of the string @p src into buffer @p dest with whitespace + * stripped from the right side. + * + * @param src Source string to be copied + * @param dest Destination buffer to copy the stripped string into + * @param srclen Length of the original string + * + * @return Length of the resulting string + */ +size_t strrstrip(char *dest, const char *src, size_t srclen) { + /* skip whitespace at the end */ + while (srclen > 0 && WHITESPACE(src[srclen - 1])) { + srclen--; + } + + /* copy until string stops */ + size_t i = 0; + for (; src[i] != 0 && i < srclen; i++) { + dest[i] = src[i]; + } + + dest[i] = 0; + return i; +}
M
common/string.h
→
common/string.h
@@ -8,6 +8,8 @@ #include <stdint.h>
#define NULL ((void*)0) +#define WHITESPACE(c) ((c) == ' ' || (c) == '\t') + /*! * Unsigned integral type. */@@ -36,5 +38,7 @@ unsigned int strlen (const char *str);
char *strcpy(char *destination, const char *source); int strcmp(const char *str1, const char *str2); const char * strchr ( const char * str, int character ); +size_t strlstrip(char *, const char *, size_t); +size_t strrstrip(char *, const char *, size_t); -#endif+#endif