From 43ce80e76484b7f33ed1259f64cd8d35bcc7ae74 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 8 Mar 2026 21:03:54 +0100 Subject: [PATCH] CE split lines properly --- ce/edit.c | 11 +++++++---- ce/edit.h | 4 +++- ce/interp.c | 2 +- libstring/string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ libstring/string.h | 8 ++++++-- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/ce/edit.c b/ce/edit.c index d2457f3..cf89bf1 100644 --- a/ce/edit.c +++ b/ce/edit.c @@ -72,7 +72,8 @@ static bool prepare_lines_cb (void* ctx, const char* start, size_t len) { return false; memset (line, 0, sizeof (*line)); - gapbuffer_init (&wmalloc, NULL, &line->gb, len); + size_t init_len = len > 0 ? len : 32; + gapbuffer_init (&wmalloc, NULL, &line->gb, init_len); for (size_t chr = 0; chr < len; chr++) gapbuffer_insert (&wrealloc, NULL, &line->gb, start[chr]); @@ -87,7 +88,9 @@ static bool prepare_lines_cb (void* ctx, const char* start, size_t len) { return true; } -static void prepare_lines (const char* text) { strtokenize (text, '\n', NULL, &prepare_lines_cb); } +static void prepare_lines (const char* text, size_t len) { + str_split_lines (text, len, NULL, &prepare_lines_cb); +} static void update_horz_scroll (size_t screen_cols) { if (editor.cursor.col < editor.col_offset) @@ -118,10 +121,10 @@ static size_t count_digits (size_t n) { return count; } -void edit_start (const char* file_path, const char* text) { +void edit_start (const char* file_path, const char* text, size_t text_len) { mprintf (ANSIQ_SCR_SAVE); - prepare_lines (text); + prepare_lines (text, text_len); struct arena temp_arena; memset (&temp_arena, 0, sizeof (temp_arena)); size_t cols, rows; diff --git a/ce/edit.h b/ce/edit.h index 15df3ce..ff6ade8 100644 --- a/ce/edit.h +++ b/ce/edit.h @@ -1,6 +1,8 @@ #ifndef _EDIT_H #define _EDIT_H -void edit_start (const char* path, const char* text); +#include + +void edit_start (const char* path, const char* text, size_t text_len); #endif // _EDIT_H diff --git a/ce/interp.c b/ce/interp.c index a899a90..5834bbb 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -265,7 +265,7 @@ static void edit (struct context* context, const char* path_string) { return; } - edit_start (path_string, str); + edit_start (path_string, str, desc.size); volume_close (); } diff --git a/libstring/string.c b/libstring/string.c index be49330..ccca77f 100644 --- a/libstring/string.c +++ b/libstring/string.c @@ -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)) { diff --git a/libstring/string.h b/libstring/string.h index ca1537c..cd3f702 100644 --- a/libstring/string.h +++ b/libstring/string.h @@ -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