summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-22 23:09:47 -0500
committerpommicket <pommicket@gmail.com>2022-12-22 23:09:47 -0500
commit08b2a1e746a871ae38f78bb8a26bb39bc9f304f6 (patch)
treef9c7414d29f0672ed1b05144b0835dd7bf21009a /buffer.c
parentb9ac4215ef8d02ad6da257315891c408bc395165 (diff)
parent806638e5ec9f43fb087e01620f8370c0d2ff47b3 (diff)
Merge branch 'trunk' into lsp
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/buffer.c b/buffer.c
index 19a55f2..bdc23d2 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1444,12 +1444,13 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32
// create a copy of str. we need to do this to remove carriage returns and newlines in the case of line buffers
char32_t str_copy[256];
- bool str_is_allocated = str.len > arr_count(str_copy);
- if (str_is_allocated) {
- char32_t *new_str = buffer_calloc(buffer, str.len, sizeof *new_str);
- memcpy(new_str, str.str, str.len * sizeof *str.str);
- str.str = new_str;
+ char32_t *str_alloc = NULL;
+ if (str.len > arr_count(str_copy)) {
+ str_alloc = buffer_calloc(buffer, str.len, sizeof *str_alloc);
+ memcpy(str_alloc, str.str, str.len * sizeof *str.str);
+ str.str = str_alloc;
} else {
+ // most insertions are small, so it's better to do this
memcpy(str_copy, str.str, str.len * sizeof *str.str);
str.str = str_copy;
}
@@ -1545,8 +1546,7 @@ BufferPos buffer_insert_text_at_pos(TextBuffer *buffer, BufferPos pos, String32
buffer_lines_modified(buffer, pos.line, line_idx);
BufferPos b = {.line = line_idx, .index = index};
- if (str_is_allocated)
- free(str.str);
+ free(str_alloc);
return b;
}