CE split lines properly

This commit is contained in:
2026-03-08 21:03:54 +01:00
parent 5682a96d5d
commit 43ce80e764
5 changed files with 59 additions and 8 deletions

View File

@@ -68,6 +68,48 @@ void strtokenize (const char* str, char delim, void* ctx, strtokenize_cb_func_t
}
}
void str_split_lines (const char* str, size_t total_len, void* ctx, strtokenize_cb_func_t cb) {
if (str == NULL)
return;
const char* start = str;
const char* end;
size_t remaining = total_len;
while (remaining > 0) {
end = memchr (start, '\n', remaining);
if (end != NULL) {
size_t line_len = (size_t)(end - start);
if (!cb (ctx, start, line_len))
return;
start = end + 1;
remaining = total_len - (size_t)(start - str);
if (remaining == 0)
cb (ctx, start, 0);
} else {
cb (ctx, start, remaining);
break;
}
}
}
/* https://svnweb.freebsd.org/base/stable/7/lib/libc/string/memchr.c?view=markup */
void* memchr (const void* s, unsigned char c, size_t n) {
if (n != 0) {
const unsigned char* p = s;
do {
if (*p++ == c)
return ((void*)(p - 1));
} while (--n != 0);
}
return NULL;
}
/* https://stackoverflow.com/a/34873406 */
int strcmp (const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {

View File

@@ -33,6 +33,12 @@ char* strcat (char* dest, const char* src);
char* strncat (char* dest, const char* src, size_t n);
void* memmove (void* dest, const void* src, unsigned int n);
void* memchr (const void* s, unsigned char c, size_t n);
void str_split_lines (const char* str, size_t total_len, void* ctx, strtokenize_cb_func_t cb);
int isalnum (int c);
int isalpha (int c);
@@ -59,6 +65,4 @@ int isascii (int c);
int isblank (int c);
void* memmove (void* dest, const void* src, unsigned int n);
#endif // _LIBSTRING_STRING_H