чување 15c5784059f1ad3a4798f0a7c92a59a078f5a619
родитељ d2fd74cb080f794c4f64dac569198ef7c285b233
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sat, 19 Feb 2022 00:42:01 +0100
Consolidate handling of DrawState.buffer
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | sled.c | | | 24 | ++++++++++++++---------- |
измењених датотека: 1, додавања: 14(+), брисања: 10(-)
diff --git a/sled.c b/sled.c
@@ -236,13 +236,16 @@ load_file(struct DrawState* state, long* lineno, long* col)
return LOAD_ERR_CANT_ALLOC;
state->buffer = newchunk;
line = state->buffer + state->buffer_size - ALLOC_DELTA;
+ init_bufferline(line);
+ }
+ if (!line->text)
+ {
+ newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
+ if (!newtext)
+ return LOAD_ERR_CANT_ALLOC;
+ line->length = u32_u8_strcpy(newtext, input_line);
+ line->text = newtext;
}
- init_bufferline(line);
- newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
- return LOAD_ERR_CANT_ALLOC;
- line->length = u32_u8_strcpy(newtext, input_line);
- line->text = newtext;
state->rows_count++;
}
fclose(input);
@@ -860,8 +863,8 @@ insert_line(struct DrawState* state)
struct BufferLine* line = NULL;
uint32_t* newtext = NULL;
- state->rows_count++;
- if (state->rows_count == state->buffer_size)
+ line = state->buffer + state->rows_count;
+ if (line == state->buffer + state->buffer_size)
{
state->buffer_size += ALLOC_DELTA;
newchunk = realloc(state->buffer,
@@ -872,9 +875,9 @@ insert_line(struct DrawState* state)
return 0;
}
state->buffer = newchunk;
+ line = state->buffer + state->buffer_size - ALLOC_DELTA;
+ init_bufferline(line);
}
- line = state->buffer + state->rows_count - 1;
- init_bufferline(line);
if (!line->text)
{
newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
@@ -885,6 +888,7 @@ insert_line(struct DrawState* state)
}
line->text = newtext;
}
+ state->rows_count++;
for (size_t i = state->rows_count - 1; i > state->row + 1; i--)
{
u32_strncpy(state->buffer[i].text, state->buffer[i - 1].text,