2015-10-24 22:14:33 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
* Four Tech
|
|
|
|
*
|
|
|
|
* public domain -- no warranty is offered or implied; use this code at your own risk
|
|
|
|
*
|
|
|
|
* 23.10.2015
|
|
|
|
*
|
|
|
|
* Buffer data object
|
|
|
|
* type - Gap Buffer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Gap_Buffer{
|
2015-10-24 22:14:33 +00:00
|
|
|
char *data;
|
|
|
|
int size1, gap_size, size2, max;
|
|
|
|
|
|
|
|
float *line_widths;
|
|
|
|
int *line_starts;
|
|
|
|
int line_count;
|
2015-11-02 01:04:37 +00:00
|
|
|
int widths_count;
|
2015-10-24 22:14:33 +00:00
|
|
|
int line_max;
|
|
|
|
int widths_max;
|
|
|
|
} Gap_Buffer;
|
|
|
|
|
2015-10-31 02:33:13 +00:00
|
|
|
inline_4tech int
|
|
|
|
buffer_good(Gap_Buffer *buffer){
|
|
|
|
int good;
|
|
|
|
good = (buffer->data != 0);
|
|
|
|
return(good);
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:59:39 +00:00
|
|
|
inline_4tech int
|
|
|
|
buffer_size(Gap_Buffer *buffer){
|
|
|
|
int size;
|
|
|
|
size = buffer->size1 + buffer->size2;
|
|
|
|
return(size);
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Gap_Buffer_Init{
|
2015-10-31 02:33:13 +00:00
|
|
|
Gap_Buffer *buffer;
|
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
} Gap_Buffer_Init;
|
|
|
|
|
|
|
|
internal_4tech Gap_Buffer_Init
|
|
|
|
buffer_begin_init(Gap_Buffer *buffer, char *data, int size){
|
|
|
|
Gap_Buffer_Init init;
|
|
|
|
init.buffer = buffer;
|
|
|
|
init.data = data;
|
|
|
|
init.size = size;
|
|
|
|
return(init);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
|
|
|
buffer_init_need_more(Gap_Buffer_Init *init){
|
|
|
|
int result;
|
|
|
|
result = 1;
|
|
|
|
if (init->buffer->data) result = 0;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
|
|
|
buffer_init_page_size(Gap_Buffer_Init *init){
|
|
|
|
int result;
|
|
|
|
result = init->size * 2;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech void
|
|
|
|
buffer_init_provide_page(Gap_Buffer_Init *init, void *page, int page_size){
|
|
|
|
Gap_Buffer *buffer;
|
|
|
|
buffer = init->buffer;
|
|
|
|
buffer->data = (char*)page;
|
|
|
|
buffer->max = page_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_4tech int
|
2015-11-03 23:57:04 +00:00
|
|
|
buffer_end_init(Gap_Buffer_Init *init, void *scratch, int scratch_size){
|
2015-10-31 02:33:13 +00:00
|
|
|
Gap_Buffer *buffer;
|
|
|
|
int osize1, size1, size2, size;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
buffer = init->buffer;
|
|
|
|
size = init->size;
|
|
|
|
if (buffer->data){
|
|
|
|
if (buffer->max >= init->size){
|
|
|
|
size2 = size >> 1;
|
|
|
|
size1 = osize1 = size - size2;
|
|
|
|
|
|
|
|
if (size1 > 0){
|
|
|
|
size1 = eol_convert_in(buffer->data, init->data, size1);
|
|
|
|
if (size2 > 0){
|
|
|
|
size2 = eol_convert_in(buffer->data + size1, init->data + osize1, size2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->size1 = size1;
|
|
|
|
buffer->size2 = size2;
|
|
|
|
buffer->gap_size = buffer->max - size1 - size2;
|
|
|
|
memmove_4tech(buffer->data + size1 + buffer->gap_size, buffer->data + size1, size2);
|
|
|
|
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Gap_Buffer_Stringify_Loop{
|
2015-10-24 22:14:33 +00:00
|
|
|
Gap_Buffer *buffer;
|
|
|
|
char *data, *base;
|
|
|
|
int absolute_pos;
|
|
|
|
int pos, end;
|
|
|
|
int size;
|
|
|
|
int separated;
|
|
|
|
} Gap_Buffer_Stringify_Loop;
|
|
|
|
|
2015-10-31 21:41:10 +00:00
|
|
|
internal_4tech Gap_Buffer_Stringify_Loop
|
|
|
|
buffer_stringify_loop(Gap_Buffer *buffer, int start, int end){
|
2015-10-24 22:14:33 +00:00
|
|
|
Gap_Buffer_Stringify_Loop result;
|
|
|
|
if (0 <= start && start < end && end <= buffer->size1 + buffer->size2){
|
|
|
|
result.buffer = buffer;
|
|
|
|
result.base = buffer->data;
|
|
|
|
result.absolute_pos = start;
|
|
|
|
|
|
|
|
if (end <= buffer->size1) result.end = end;
|
|
|
|
else result.end = end + buffer->gap_size;
|
|
|
|
|
|
|
|
if (start < buffer->size1){
|
|
|
|
if (end <= buffer->size1) result.separated = 0;
|
|
|
|
else result.separated = 1;
|
|
|
|
result.pos = start;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
result.separated = 0;
|
|
|
|
result.pos = start + buffer->gap_size;
|
|
|
|
}
|
|
|
|
if (result.separated) result.size = buffer->size1 - start;
|
|
|
|
else result.size = end - start;
|
|
|
|
result.data = buffer->data + result.pos;
|
|
|
|
}
|
|
|
|
else result.buffer = 0;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech int
|
|
|
|
buffer_stringify_good(Gap_Buffer_Stringify_Loop *loop){
|
|
|
|
int result;
|
|
|
|
result = (loop->buffer != 0);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-10-31 21:41:10 +00:00
|
|
|
internal_4tech void
|
2015-10-24 22:14:33 +00:00
|
|
|
buffer_stringify_next(Gap_Buffer_Stringify_Loop *loop){
|
|
|
|
int size1, temp_end;
|
|
|
|
if (loop->separated){
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->separated = 0;
|
2015-10-24 22:14:33 +00:00
|
|
|
size1 = loop->buffer->size1;
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->pos = loop->buffer->gap_size + size1;
|
|
|
|
loop->absolute_pos = size1;
|
|
|
|
temp_end = loop->end;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
else{
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->buffer = 0;
|
|
|
|
temp_end = loop->pos;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
loop->size = temp_end - loop->pos;
|
|
|
|
loop->data = loop->base + loop->pos;
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:14:24 +00:00
|
|
|
typedef struct Gap_Buffer_Backify_Loop{
|
2015-10-24 22:14:33 +00:00
|
|
|
Gap_Buffer *buffer;
|
|
|
|
char *data, *base;
|
|
|
|
int pos, end;
|
|
|
|
int size;
|
|
|
|
int absolute_pos;
|
|
|
|
int separated;
|
|
|
|
} Gap_Buffer_Backify_Loop;
|
|
|
|
|
2015-10-31 21:41:10 +00:00
|
|
|
internal_4tech Gap_Buffer_Backify_Loop
|
|
|
|
buffer_backify_loop(Gap_Buffer *buffer, int start, int end){
|
2015-10-24 22:14:33 +00:00
|
|
|
Gap_Buffer_Backify_Loop result;
|
|
|
|
|
2015-10-28 22:59:39 +00:00
|
|
|
++start;
|
2015-10-24 22:14:33 +00:00
|
|
|
if (0 <= end && end < start && start <= buffer->size1 + buffer->size2){
|
|
|
|
result.buffer = buffer;
|
|
|
|
result.base = buffer->data;
|
|
|
|
|
|
|
|
if (end < buffer->size1) result.end = end;
|
|
|
|
else result.end = end + buffer->gap_size;
|
|
|
|
|
|
|
|
if (start <= buffer->size1){
|
|
|
|
result.separated = 0;
|
2015-10-31 21:41:10 +00:00
|
|
|
result.pos = 0;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (end < buffer->size1) result.separated = 1;
|
|
|
|
else result.separated = 0;
|
2015-10-31 21:41:10 +00:00
|
|
|
result.pos = buffer->size1 + buffer->gap_size;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
2015-10-31 21:41:10 +00:00
|
|
|
if (!result.separated && result.pos < result.end) result.pos = result.end;
|
2015-10-24 22:14:33 +00:00
|
|
|
result.size = start - result.pos;
|
|
|
|
result.absolute_pos = result.pos;
|
|
|
|
if (result.absolute_pos > buffer->size1) result.absolute_pos -= buffer->gap_size;
|
|
|
|
result.data = result.base + result.pos;
|
|
|
|
}
|
|
|
|
else result.buffer = 0;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline_4tech int
|
|
|
|
buffer_backify_good(Gap_Buffer_Backify_Loop *loop){
|
|
|
|
int result;
|
|
|
|
result = (loop->buffer != 0);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-10-31 21:41:10 +00:00
|
|
|
internal_4tech void
|
2015-10-24 22:14:33 +00:00
|
|
|
buffer_backify_next(Gap_Buffer_Backify_Loop *loop){
|
|
|
|
Gap_Buffer *buffer;
|
|
|
|
int temp_end;
|
|
|
|
int chunk2_start;
|
|
|
|
buffer = loop->buffer;
|
|
|
|
chunk2_start = buffer->size1 + buffer->gap_size;
|
|
|
|
if (loop->separated){
|
2015-10-31 21:41:10 +00:00
|
|
|
loop->separated = 0;
|
|
|
|
temp_end = buffer->size1;
|
|
|
|
loop->pos = 0;
|
|
|
|
loop->absolute_pos = 0;
|
|
|
|
if (loop->pos < loop->end){
|
|
|
|
loop->absolute_pos = loop->end;
|
|
|
|
loop->pos = loop->end;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2015-10-31 21:41:10 +00:00
|
|
|
temp_end = 0;
|
|
|
|
loop->buffer = 0;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
loop->size = temp_end - loop->pos;
|
|
|
|
loop->data = loop->base + loop->pos;
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:59:39 +00:00
|
|
|
internal_4tech int
|
2015-11-06 19:54:24 +00:00
|
|
|
buffer_replace_range(Gap_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount,
|
|
|
|
void *scratch, int scratch_memory, int *request_amount){
|
2015-10-28 22:59:39 +00:00
|
|
|
char *data;
|
|
|
|
int result;
|
|
|
|
int size;
|
|
|
|
int move_size;
|
|
|
|
|
|
|
|
size = buffer_size(buffer);
|
|
|
|
assert_4tech(0 <= start);
|
|
|
|
assert_4tech(start <= end);
|
|
|
|
assert_4tech(end <= size);
|
|
|
|
|
|
|
|
*shift_amount = (len - (end - start));
|
|
|
|
if (*shift_amount + size <= buffer->max){
|
|
|
|
data = buffer->data;
|
|
|
|
if (end < buffer->size1){
|
|
|
|
move_size = buffer->size1 - end;
|
|
|
|
memmove_4tech(data + buffer->size1 + buffer->gap_size - move_size, data + end, move_size);
|
|
|
|
buffer->size1 -= move_size;
|
|
|
|
buffer->size2 += move_size;
|
|
|
|
}
|
|
|
|
if (start > buffer->size1){
|
|
|
|
move_size = start - buffer->size1;
|
|
|
|
memmove_4tech(data + buffer->size1, data + buffer->size1 + buffer->gap_size, move_size);
|
|
|
|
buffer->size1 += move_size;
|
|
|
|
buffer->size2 -= move_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy_4tech(data + start, str, len);
|
|
|
|
buffer->size2 = size - end;
|
|
|
|
buffer->size1 = start + len;
|
|
|
|
buffer->gap_size -= *shift_amount;
|
|
|
|
|
|
|
|
assert_4tech(buffer->size1 + buffer->size2 == size + *shift_amount);
|
|
|
|
assert_4tech(buffer->size1 + buffer->gap_size + buffer->size2 == buffer->max);
|
|
|
|
|
|
|
|
result = 0;
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
2015-10-28 22:59:39 +00:00
|
|
|
else{
|
2015-11-02 01:04:37 +00:00
|
|
|
*request_amount = round_up_4tech(2*(*shift_amount + size), 4 << 10);
|
|
|
|
result = 1;
|
2015-10-28 22:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
2015-10-24 22:14:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 00:48:30 +00:00
|
|
|
// NOTE(allen): This could should be optimized for Gap_Buffer
|
|
|
|
internal_4tech int
|
2015-11-03 23:57:04 +00:00
|
|
|
buffer_batch_edit_step(Buffer_Batch_State *state, Gap_Buffer *buffer, Buffer_Edit *sorted_edits,
|
2015-11-06 04:31:53 +00:00
|
|
|
char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){
|
2015-10-30 00:48:30 +00:00
|
|
|
Buffer_Edit *edit;
|
|
|
|
int i, result;
|
|
|
|
int shift_total, shift_amount;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
shift_total = state->shift_total;
|
|
|
|
i = state->i;
|
|
|
|
|
|
|
|
edit = sorted_edits + i;
|
|
|
|
for (; i < edit_count; ++i, ++edit){
|
|
|
|
result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total,
|
2015-11-06 04:31:53 +00:00
|
|
|
strings + edit->str_start, edit->len, &shift_amount,
|
|
|
|
scratch, scratch_size, request_amount);
|
2015-10-30 00:48:30 +00:00
|
|
|
if (result) break;
|
|
|
|
shift_total += shift_amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->shift_total = shift_total;
|
|
|
|
state->i = i;
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:43:36 +00:00
|
|
|
internal_4tech void*
|
|
|
|
buffer_edit_provide_memory(Gap_Buffer *buffer, void *new_data, int new_max){
|
|
|
|
void *result;
|
|
|
|
int new_gap_size;
|
|
|
|
|
|
|
|
assert_4tech(new_max >= buffer_size(buffer));
|
|
|
|
|
|
|
|
result = buffer->data;
|
|
|
|
new_gap_size = new_max - buffer_size(buffer);
|
|
|
|
memcpy_4tech(new_data, buffer->data, buffer->size1);
|
|
|
|
memcpy_4tech((char*)new_data + buffer->size1 + new_gap_size, buffer->data + buffer->size1 + buffer->gap_size, buffer->size2);
|
|
|
|
|
|
|
|
buffer->data = (char*)new_data;
|
|
|
|
buffer->gap_size = new_gap_size;
|
|
|
|
buffer->max = new_max;
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:14:33 +00:00
|
|
|
// BOTTOM
|
|
|
|
|