CE split lines properly
This commit is contained in:
11
ce/edit.c
11
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;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef _EDIT_H
|
||||
#define _EDIT_H
|
||||
|
||||
void edit_start (const char* path, const char* text);
|
||||
#include <stddef.h>
|
||||
|
||||
void edit_start (const char* path, const char* text, size_t text_len);
|
||||
|
||||
#endif // _EDIT_H
|
||||
|
||||
@@ -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 ();
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user