cleared out old buffer code
							parent
							
								
									c551c081a7
								
							
						
					
					
						commit
						b4d1a2205d
					
				| 
						 | 
				
			
			@ -19,8 +19,6 @@
 | 
			
		|||
 | 
			
		||||
#include "4coder_custom.h"
 | 
			
		||||
 | 
			
		||||
#define BUFFER_EXPERIMENT_SCALPEL 1
 | 
			
		||||
 | 
			
		||||
#include "4ed_math.h"
 | 
			
		||||
 | 
			
		||||
#include "4ed_system.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +39,13 @@
 | 
			
		|||
#include "4ed_style.h"
 | 
			
		||||
#include "4ed_style.cpp"
 | 
			
		||||
#include "4ed_command.cpp"
 | 
			
		||||
 | 
			
		||||
#include "buffer/4coder_shared.cpp"
 | 
			
		||||
#include "buffer/4coder_gap_buffer.cpp"
 | 
			
		||||
#define Buffer_Type Gap_Buffer
 | 
			
		||||
#include "buffer/4coder_buffer_abstract.cpp"
 | 
			
		||||
#include "4ed_file.cpp"
 | 
			
		||||
 | 
			
		||||
#include "4ed_gui.cpp"
 | 
			
		||||
#include "4ed_layout.cpp"
 | 
			
		||||
#include "4ed_app_models.h"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										18
									
								
								4ed_file.cpp
								
								
								
								
							
							
						
						
									
										18
									
								
								4ed_file.cpp
								
								
								
								
							| 
						 | 
				
			
			@ -9,24 +9,6 @@
 | 
			
		|||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
#include "buffer/4coder_shared.cpp"
 | 
			
		||||
 | 
			
		||||
#if BUFFER_EXPERIMENT_SCALPEL == 0
 | 
			
		||||
#include "buffer/4coder_golden_array.cpp"
 | 
			
		||||
#define Buffer_Type Buffer
 | 
			
		||||
#elif BUFFER_EXPERIMENT_SCALPEL == 1
 | 
			
		||||
#include "buffer/4coder_gap_buffer.cpp"
 | 
			
		||||
#define Buffer_Type Gap_Buffer
 | 
			
		||||
#elif BUFFER_EXPERIMENT_SCALPEL == 2
 | 
			
		||||
#include "buffer/4coder_multi_gap_buffer.cpp"
 | 
			
		||||
#define Buffer_Type Multi_Gap_Buffer
 | 
			
		||||
#else
 | 
			
		||||
#include "buffer/4coder_rope_buffer.cpp"
 | 
			
		||||
#define Buffer_Type Rope_Buffer
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "buffer/4coder_buffer_abstract.cpp"
 | 
			
		||||
 | 
			
		||||
enum Edit_Pos_Set_Type{
 | 
			
		||||
    EditPos_None,
 | 
			
		||||
    EditPos_CursorSet,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +0,0 @@
 | 
			
		|||
#define external_name "awebster_windows"
 | 
			
		||||
| 
						 | 
				
			
			@ -1,235 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 16.10.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Buffer data object
 | 
			
		||||
 *  type - Golden Array
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
typedef struct Buffer{
 | 
			
		||||
    char *data;
 | 
			
		||||
    int size, max;
 | 
			
		||||
    
 | 
			
		||||
    int *line_starts;
 | 
			
		||||
    float *line_widths;
 | 
			
		||||
    int line_count;
 | 
			
		||||
    int widths_count;
 | 
			
		||||
    int line_max;
 | 
			
		||||
    int widths_max;
 | 
			
		||||
} Buffer;
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_good(Buffer *buffer){
 | 
			
		||||
    int good;
 | 
			
		||||
    good = (buffer->data != 0);
 | 
			
		||||
    return(good);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_size(Buffer *buffer){
 | 
			
		||||
    return buffer->size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Buffer_Init{
 | 
			
		||||
    Buffer *buffer;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int size;
 | 
			
		||||
} Buffer_Init;
 | 
			
		||||
 | 
			
		||||
internal_4tech Buffer_Init
 | 
			
		||||
buffer_begin_init(Buffer *buffer, char *data, int size){
 | 
			
		||||
    Buffer_Init init;
 | 
			
		||||
    init.buffer = buffer;
 | 
			
		||||
    init.data = data;
 | 
			
		||||
    init.size = size;
 | 
			
		||||
    return(init);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_init_need_more(Buffer_Init *init){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = 1;
 | 
			
		||||
    if (init->buffer->data) result = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_init_page_size(Buffer_Init *init){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = init->size * 2;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech void
 | 
			
		||||
buffer_init_provide_page(Buffer_Init *init, void *page, int page_size){
 | 
			
		||||
    Buffer *buffer;
 | 
			
		||||
    buffer = init->buffer;
 | 
			
		||||
    buffer->data = (char*)page;
 | 
			
		||||
    buffer->max = page_size;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_end_init(Buffer_Init *init, void *scratch, int scratch_size){
 | 
			
		||||
    Buffer *buffer;
 | 
			
		||||
    int result;
 | 
			
		||||
 | 
			
		||||
    result = 0;
 | 
			
		||||
    buffer = init->buffer;
 | 
			
		||||
    if (buffer->data){
 | 
			
		||||
        if (buffer->max >= init->size){
 | 
			
		||||
            buffer->size = eol_convert_in(buffer->data, init->data, init->size);
 | 
			
		||||
            result = 1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Buffer_Stringify_Loop{
 | 
			
		||||
    Buffer *buffer;
 | 
			
		||||
    char *data, *end;
 | 
			
		||||
    int absolute_pos;
 | 
			
		||||
    int size;
 | 
			
		||||
} Buffer_Stringify_Loop;
 | 
			
		||||
 | 
			
		||||
inline_4tech Buffer_Stringify_Loop
 | 
			
		||||
buffer_stringify_loop(Buffer *buffer, int start, int end){
 | 
			
		||||
    Buffer_Stringify_Loop result;
 | 
			
		||||
    if (0 <= start && start < end && end <= buffer->size){
 | 
			
		||||
        result.buffer = buffer;
 | 
			
		||||
        result.absolute_pos = start;
 | 
			
		||||
        result.data = buffer->data + start;
 | 
			
		||||
        result.size = end - start;
 | 
			
		||||
        result.end = buffer->data + end;
 | 
			
		||||
    }
 | 
			
		||||
    else result.buffer = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_stringify_good(Buffer_Stringify_Loop *loop){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = (loop->buffer != 0);
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech void
 | 
			
		||||
buffer_stringify_next(Buffer_Stringify_Loop *loop){
 | 
			
		||||
    loop->buffer = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Buffer_Backify_Loop{
 | 
			
		||||
    Buffer *buffer;
 | 
			
		||||
    char *data, *end;
 | 
			
		||||
    int absolute_pos;
 | 
			
		||||
    int size;
 | 
			
		||||
} Buffer_Backify_Loop;
 | 
			
		||||
 | 
			
		||||
inline_4tech Buffer_Backify_Loop
 | 
			
		||||
buffer_backify_loop(Buffer *buffer, int start, int end){
 | 
			
		||||
    Buffer_Backify_Loop result;
 | 
			
		||||
    
 | 
			
		||||
    ++start;
 | 
			
		||||
    if (0 <= end && end < start && start <= buffer->size){
 | 
			
		||||
        result.buffer = buffer;
 | 
			
		||||
        result.end = buffer->data + end;
 | 
			
		||||
        result.size = start - end;
 | 
			
		||||
        result.absolute_pos = start - result.size;
 | 
			
		||||
        result.data = buffer->data + result.absolute_pos;
 | 
			
		||||
    }
 | 
			
		||||
    else result.buffer = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_backify_good(Buffer_Backify_Loop *loop){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = (loop->buffer != 0);
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech void
 | 
			
		||||
buffer_backify_next(Buffer_Backify_Loop *loop){
 | 
			
		||||
    loop->buffer = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount,
 | 
			
		||||
    void *scratch, int scratch_size, int *request_amount){
 | 
			
		||||
 | 
			
		||||
    char *data;
 | 
			
		||||
    int result;
 | 
			
		||||
    int 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 = (char*)buffer->data;
 | 
			
		||||
        memmove_4tech(data + end + *shift_amount, data + end, buffer->size - end);
 | 
			
		||||
        buffer->size += *shift_amount;
 | 
			
		||||
        if (len != 0) memcpy_4tech(data + start, str, len);
 | 
			
		||||
 | 
			
		||||
        result = 0;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        *request_amount = round_up_4tech(2*(*shift_amount + size), 4 << 10);
 | 
			
		||||
        result = 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_batch_edit_step(Buffer_Batch_State *state, Buffer *buffer, Buffer_Edit *sorted_edits,
 | 
			
		||||
    char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){
 | 
			
		||||
    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){
 | 
			
		||||
        assert(edit->end + shift_total <= buffer_size(buffer));
 | 
			
		||||
        result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total,
 | 
			
		||||
            strings + edit->str_start, edit->len, &shift_amount,
 | 
			
		||||
            scratch, scratch_size, request_amount);
 | 
			
		||||
        if (result) break;
 | 
			
		||||
        shift_total += shift_amount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    state->shift_total = shift_total;
 | 
			
		||||
    state->i = i;
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech void*
 | 
			
		||||
buffer_edit_provide_memory(Buffer *buffer, void *new_data, int new_max){
 | 
			
		||||
    void *result;
 | 
			
		||||
    
 | 
			
		||||
    assert_4tech(new_max >= buffer->size);
 | 
			
		||||
    
 | 
			
		||||
    result = buffer->data;
 | 
			
		||||
    memcpy_4tech(new_data, buffer->data, buffer->size);
 | 
			
		||||
    buffer->data = (char*)new_data;
 | 
			
		||||
    buffer->max = new_max;
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1,706 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 30.10.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Buffer data object
 | 
			
		||||
 *  type - Multi Gap Buffer
 | 
			
		||||
 *
 | 
			
		||||
 * This scheme was originally introduced to me by Martin Cohen,
 | 
			
		||||
 * who calls it a "Fixed Width Gap Buffer".
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
typedef struct Fixed_Width_Gap_Buffer{
 | 
			
		||||
    char *data;
 | 
			
		||||
    int size1, gap_size, size2;
 | 
			
		||||
    int start_pos;
 | 
			
		||||
} Fixed_Width_Gap_Buffer;
 | 
			
		||||
 | 
			
		||||
#define fixed_width_buffer_size (8 << 10)
 | 
			
		||||
#define fixed_width_buffer_half_size (4 << 10)
 | 
			
		||||
 | 
			
		||||
typedef struct Multi_Gap_Buffer{
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gaps;
 | 
			
		||||
    int chunk_count;
 | 
			
		||||
    int chunk_alloced;
 | 
			
		||||
    int chunk_max;
 | 
			
		||||
    int size;
 | 
			
		||||
    
 | 
			
		||||
    float *line_widths;
 | 
			
		||||
    int *line_starts;
 | 
			
		||||
    int line_count;
 | 
			
		||||
    int widths_count;
 | 
			
		||||
    int line_max;
 | 
			
		||||
    int widths_max;
 | 
			
		||||
    
 | 
			
		||||
    int grow_gaps;
 | 
			
		||||
    int edit_stage;
 | 
			
		||||
} Multi_Gap_Buffer;
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_good(Multi_Gap_Buffer *buffer){
 | 
			
		||||
    int good;
 | 
			
		||||
    good = (buffer->gaps != 0);
 | 
			
		||||
    return(good);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_size(Multi_Gap_Buffer *buffer){
 | 
			
		||||
    int size;
 | 
			
		||||
    size = buffer->size;
 | 
			
		||||
    return(size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Multi_Gap_Buffer_Init{
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int size;
 | 
			
		||||
    int chunk_i;
 | 
			
		||||
    int chunk_count;
 | 
			
		||||
    int chunk_alloc;
 | 
			
		||||
} Multi_Gap_Buffer_Init;
 | 
			
		||||
 | 
			
		||||
internal_4tech Multi_Gap_Buffer_Init
 | 
			
		||||
buffer_begin_init(Multi_Gap_Buffer *buffer, char *data, int size){
 | 
			
		||||
    Multi_Gap_Buffer_Init init;
 | 
			
		||||
    init.buffer = buffer;
 | 
			
		||||
    init.data = data;
 | 
			
		||||
    init.size = size;
 | 
			
		||||
    init.chunk_i = 0;
 | 
			
		||||
    init.chunk_alloc = div_ceil_4tech(size, fixed_width_buffer_half_size);
 | 
			
		||||
    init.chunk_count = init.chunk_alloc;
 | 
			
		||||
    if (init.chunk_alloc < 4) init.chunk_alloc = 4;
 | 
			
		||||
    return(init);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_init_need_more(Multi_Gap_Buffer_Init *init){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = 1;
 | 
			
		||||
    if (init->buffer->gaps && init->chunk_i == init->chunk_alloc)
 | 
			
		||||
        result = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_init_page_size(Multi_Gap_Buffer_Init *init){
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    int result;
 | 
			
		||||
    buffer = init->buffer;
 | 
			
		||||
    if (buffer->gaps) result = fixed_width_buffer_size;
 | 
			
		||||
    else result = init->chunk_alloc * 2 * sizeof(*buffer->gaps);
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech void
 | 
			
		||||
buffer_init_provide_page(Multi_Gap_Buffer_Init *init, void *page, int page_size){
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    buffer = init->buffer;
 | 
			
		||||
    
 | 
			
		||||
    if (buffer->gaps){
 | 
			
		||||
        assert_4tech(page_size >= fixed_width_buffer_size);
 | 
			
		||||
        buffer->gaps[init->chunk_i].data = (char*)page;
 | 
			
		||||
        ++init->chunk_i;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        buffer->gaps = (Fixed_Width_Gap_Buffer*)page;
 | 
			
		||||
        buffer->chunk_max = page_size / sizeof(*buffer->gaps);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_end_init(Multi_Gap_Buffer_Init *init, void *scratch, int scratch_size){
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int result;
 | 
			
		||||
    int i, count;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int pos, size, total_size, start_pos;
 | 
			
		||||
    int osize1, size1, size2;
 | 
			
		||||
 | 
			
		||||
    result = 0;
 | 
			
		||||
    buffer = init->buffer;
 | 
			
		||||
    if (buffer->gaps){
 | 
			
		||||
        if (buffer->chunk_max >= div_ceil_4tech(init->size, fixed_width_buffer_half_size)){
 | 
			
		||||
            buffer->chunk_count = init->chunk_count;
 | 
			
		||||
            if (buffer->chunk_count == 0) buffer->chunk_count = 1;
 | 
			
		||||
            buffer->chunk_alloced = init->chunk_alloc;
 | 
			
		||||
            result = 1;
 | 
			
		||||
            
 | 
			
		||||
            data = init->data;
 | 
			
		||||
            total_size = init->size;
 | 
			
		||||
            gap = buffer->gaps;
 | 
			
		||||
            count = buffer->chunk_count;
 | 
			
		||||
            size = fixed_width_buffer_half_size;
 | 
			
		||||
            pos = 0;
 | 
			
		||||
            start_pos = 0;
 | 
			
		||||
            
 | 
			
		||||
            for (i = 0; i < count; ++i, ++gap, pos += size){
 | 
			
		||||
                assert_4tech(size == fixed_width_buffer_half_size);
 | 
			
		||||
                if (pos + size > total_size) size = total_size - pos;
 | 
			
		||||
                
 | 
			
		||||
                if (gap->data){
 | 
			
		||||
                    size2 = size >> 1;
 | 
			
		||||
                    size1 = osize1 = size - size2;
 | 
			
		||||
                    
 | 
			
		||||
                    if (size1 > 0){
 | 
			
		||||
                        size1 = eol_convert_in(gap->data, data + pos, size1);
 | 
			
		||||
                        if (size2 > 0){
 | 
			
		||||
                            size2 = eol_convert_in(gap->data + size1, data + pos + osize1, size2);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    
 | 
			
		||||
                    gap->size1 = size1;
 | 
			
		||||
                    gap->size2 = size2;
 | 
			
		||||
                    gap->gap_size = fixed_width_buffer_size - size1 - size2;
 | 
			
		||||
                    memmove_4tech(gap->data + size1 + gap->gap_size, gap->data + size1, size2);
 | 
			
		||||
                    
 | 
			
		||||
                    gap->start_pos = start_pos;
 | 
			
		||||
                    start_pos += size1 + size2;
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    result = 0;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            buffer->size = start_pos;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_find_chunk(Multi_Gap_Buffer *buffer, int pos){
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gaps;
 | 
			
		||||
    int start, end, m, this_pos;
 | 
			
		||||
    
 | 
			
		||||
    gaps = buffer->gaps;
 | 
			
		||||
    start = 0;
 | 
			
		||||
    end = buffer->chunk_count;
 | 
			
		||||
    for(;;){
 | 
			
		||||
        m = (start + end) / 2;
 | 
			
		||||
        this_pos = gaps[m].start_pos;
 | 
			
		||||
        if (this_pos < pos) start = m;
 | 
			
		||||
        else if (this_pos > pos) end = m;
 | 
			
		||||
        else{
 | 
			
		||||
            --m;
 | 
			
		||||
            if (m < 0) m = 0;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (start+1 == end){
 | 
			
		||||
            m = start; break;
 | 
			
		||||
        }
 | 
			
		||||
        assert_4tech(start < end);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return(m);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Multi_Gap_Buffer_Stringify_Loop{
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gaps;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int absolute_pos;
 | 
			
		||||
    int size;
 | 
			
		||||
    int chunk_i;
 | 
			
		||||
    int chunk_end;
 | 
			
		||||
    int pos, end;
 | 
			
		||||
} Multi_Gap_Buffer_Stringify_Loop;
 | 
			
		||||
 | 
			
		||||
internal_4tech Multi_Gap_Buffer_Stringify_Loop
 | 
			
		||||
buffer_stringify_loop(Multi_Gap_Buffer *buffer, int start, int end){
 | 
			
		||||
    Multi_Gap_Buffer_Stringify_Loop result;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int temp_end;
 | 
			
		||||
    
 | 
			
		||||
    if (0 <= start && start < end && end <= buffer->size){
 | 
			
		||||
        result.buffer = buffer;
 | 
			
		||||
        result.gaps = buffer->gaps;
 | 
			
		||||
        result.absolute_pos = start;
 | 
			
		||||
        
 | 
			
		||||
        result.chunk_i = buffer_find_chunk(buffer, start);
 | 
			
		||||
        result.chunk_end = buffer_find_chunk(buffer, end);
 | 
			
		||||
        
 | 
			
		||||
        gap = result.gaps + result.chunk_end;
 | 
			
		||||
        end -= gap->start_pos;
 | 
			
		||||
        if (end <= gap->size1) result.end = end;
 | 
			
		||||
        else result.end = end + gap->gap_size;
 | 
			
		||||
        
 | 
			
		||||
        gap = result.gaps + result.chunk_i;
 | 
			
		||||
        start -= gap->start_pos;
 | 
			
		||||
        if (start < gap->size1){
 | 
			
		||||
            result.pos = start;
 | 
			
		||||
            temp_end = gap->size1;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            result.pos = start + gap->gap_size;
 | 
			
		||||
            temp_end = fixed_width_buffer_size;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (result.chunk_i == result.chunk_end && temp_end > result.end) temp_end = result.end;
 | 
			
		||||
        result.size = temp_end - result.pos;
 | 
			
		||||
        result.data = gap->data + result.pos;
 | 
			
		||||
    }
 | 
			
		||||
    else result.buffer = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_stringify_good(Multi_Gap_Buffer_Stringify_Loop *loop){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = (loop->buffer != 0);
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech void
 | 
			
		||||
buffer_stringify_next(Multi_Gap_Buffer_Stringify_Loop *loop){
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int temp_end;
 | 
			
		||||
 | 
			
		||||
    gap = loop->gaps + loop->chunk_i;
 | 
			
		||||
    if (loop->chunk_i == loop->chunk_end && loop->pos + loop->size == loop->end){
 | 
			
		||||
        loop->buffer = 0;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        if (loop->pos < gap->size1){
 | 
			
		||||
            loop->pos = gap->size1 + gap->gap_size;
 | 
			
		||||
            loop->absolute_pos = gap->start_pos + gap->size1;
 | 
			
		||||
            temp_end = fixed_width_buffer_size;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            ++loop->chunk_i;
 | 
			
		||||
            ++gap;
 | 
			
		||||
            loop->pos = 0;
 | 
			
		||||
            loop->absolute_pos = gap->start_pos;
 | 
			
		||||
            temp_end = gap->size1;
 | 
			
		||||
            if (gap->size1 == 0){
 | 
			
		||||
                loop->pos = gap->gap_size;
 | 
			
		||||
                temp_end = fixed_width_buffer_size;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (loop->chunk_i == loop->chunk_end && temp_end > loop->end) temp_end = loop->end;
 | 
			
		||||
        loop->size = temp_end - loop->pos;
 | 
			
		||||
        loop->data = gap->data + loop->pos;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Multi_Gap_Buffer_Backify_Loop{
 | 
			
		||||
    Multi_Gap_Buffer *buffer;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gaps;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int absolute_pos;
 | 
			
		||||
    int size;
 | 
			
		||||
    int chunk_i;
 | 
			
		||||
    int chunk_end;
 | 
			
		||||
    int pos, end;
 | 
			
		||||
} Multi_Gap_Buffer_Backify_Loop;
 | 
			
		||||
 | 
			
		||||
internal_4tech Multi_Gap_Buffer_Backify_Loop
 | 
			
		||||
buffer_backify_loop(Multi_Gap_Buffer *buffer, int start, int end){
 | 
			
		||||
    Multi_Gap_Buffer_Backify_Loop result;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int temp_end, temp_start;
 | 
			
		||||
    
 | 
			
		||||
    ++start;
 | 
			
		||||
    if (0 <= end && end < start && start <= buffer->size){
 | 
			
		||||
        result.buffer = buffer;
 | 
			
		||||
        result.gaps = buffer->gaps;
 | 
			
		||||
        
 | 
			
		||||
        result.chunk_i = buffer_find_chunk(buffer, start);
 | 
			
		||||
        result.chunk_end = buffer_find_chunk(buffer, end);
 | 
			
		||||
        
 | 
			
		||||
        gap = result.gaps + result.chunk_end;
 | 
			
		||||
        end -= gap->start_pos;
 | 
			
		||||
        if (end < gap->size1) result.end = end;
 | 
			
		||||
        else result.end = end + gap->gap_size;
 | 
			
		||||
        
 | 
			
		||||
        gap = result.gaps + result.chunk_i;
 | 
			
		||||
        start -= gap->start_pos;
 | 
			
		||||
        if (start < gap->size1){
 | 
			
		||||
            temp_end = start;
 | 
			
		||||
            temp_start = 0;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            temp_end = start + gap->gap_size;
 | 
			
		||||
            temp_start = gap->size1 + gap->gap_size;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        if (result.chunk_i == result.chunk_end && temp_start < result.end) temp_start = result.end;
 | 
			
		||||
        result.pos = temp_start;
 | 
			
		||||
        result.absolute_pos = temp_start + gap->start_pos;
 | 
			
		||||
        if (temp_start >= gap->size1) result.absolute_pos -= gap->gap_size;
 | 
			
		||||
        result.size = temp_end - temp_start;
 | 
			
		||||
        result.data = gap->data + result.pos;
 | 
			
		||||
    }
 | 
			
		||||
    else result.buffer = 0;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
buffer_backify_good(Multi_Gap_Buffer_Backify_Loop *loop){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = (loop->buffer != 0);
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech void
 | 
			
		||||
buffer_backify_next(Multi_Gap_Buffer_Backify_Loop *loop){
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int temp_end, temp_start;
 | 
			
		||||
    
 | 
			
		||||
    gap = loop->gaps + loop->chunk_i;
 | 
			
		||||
    if (loop->chunk_i == loop->chunk_end && loop->pos == loop->end){
 | 
			
		||||
        loop->buffer = 0;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        if (loop->pos < gap->size1){
 | 
			
		||||
            --gap;
 | 
			
		||||
            --loop->chunk_i;
 | 
			
		||||
            temp_start = gap->size1 + gap->gap_size;
 | 
			
		||||
            temp_end = fixed_width_buffer_size;
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            temp_start = 0;
 | 
			
		||||
            temp_end = gap->size1;
 | 
			
		||||
        }
 | 
			
		||||
        if (loop->chunk_i == loop->chunk_end && temp_start < loop->end) temp_start = loop->end;
 | 
			
		||||
        loop->absolute_pos = temp_start + gap->start_pos;
 | 
			
		||||
        if (temp_start >= gap->size1) loop->absolute_pos -= gap->gap_size;
 | 
			
		||||
        loop->pos = temp_start;
 | 
			
		||||
        loop->size = temp_end - temp_start;
 | 
			
		||||
        loop->data = gap->data + loop->pos;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_replace_range(Multi_Gap_Buffer *buffer, int start, int end, char *str, int len, int *shift_amount_out,
 | 
			
		||||
                     void *scratch, int scratch_size, int *request_amount){
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gaps, *gap, *dgap;
 | 
			
		||||
    char *data;
 | 
			
		||||
    int move_size;
 | 
			
		||||
    int gap_start, gap_end;
 | 
			
		||||
    int result;
 | 
			
		||||
    int size;
 | 
			
		||||
    int local_end;
 | 
			
		||||
    int shift_amount;
 | 
			
		||||
    int required_empty_buffers;
 | 
			
		||||
    int supplanted_gaps;
 | 
			
		||||
    int dpos;
 | 
			
		||||
    int head_size, middle_size, tail_size;
 | 
			
		||||
    int mem_pos, local_start_pos;
 | 
			
		||||
    debug_4tech(int osize);
 | 
			
		||||
    
 | 
			
		||||
    size = buffer_size(buffer);
 | 
			
		||||
    assert_4tech(0 <= start);
 | 
			
		||||
    assert_4tech(start <= end);
 | 
			
		||||
    assert_4tech(end <= size);
 | 
			
		||||
 | 
			
		||||
    *shift_amount_out = (len - (end - start));
 | 
			
		||||
    
 | 
			
		||||
    gaps = buffer->gaps;
 | 
			
		||||
    gap_start = buffer_find_chunk(buffer, start);
 | 
			
		||||
    gap_end = buffer_find_chunk(buffer, end);
 | 
			
		||||
    
 | 
			
		||||
    if (buffer->edit_stage == 0){
 | 
			
		||||
        buffer->size += *shift_amount_out;
 | 
			
		||||
        for (gap = gaps + gap_end + 1;
 | 
			
		||||
             gap < gaps + buffer->chunk_count;
 | 
			
		||||
             ++gap){
 | 
			
		||||
            gap->start_pos += *shift_amount_out;
 | 
			
		||||
        }
 | 
			
		||||
        buffer->edit_stage = 1;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    gap = gaps + gap_start;
 | 
			
		||||
    if (buffer->edit_stage == 1){
 | 
			
		||||
        if (gap_start < gap_end && gap_start+1 < buffer->chunk_count){
 | 
			
		||||
            supplanted_gaps = gap_end - gap_start - 1;
 | 
			
		||||
            if (buffer->chunk_max - buffer->chunk_alloced >= supplanted_gaps){
 | 
			
		||||
                ++gap;
 | 
			
		||||
                memcpy_4tech(gaps + buffer->chunk_alloced, gap, sizeof(*gaps)*supplanted_gaps);
 | 
			
		||||
                memmove_4tech(gap, gaps + gap_end, sizeof(*gaps)*(buffer->chunk_alloced - gap_start - 1));
 | 
			
		||||
                buffer->chunk_count -= (gap_end - gap_start - 1);
 | 
			
		||||
        
 | 
			
		||||
                local_end = end - gap->start_pos;
 | 
			
		||||
        
 | 
			
		||||
                if (local_end >= gap->size1){
 | 
			
		||||
                    gap->size2 -= (local_end - gap->size1);
 | 
			
		||||
                    gap->size1 = 0;
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    memmove_4tech(gap->data, gap->data + local_end, gap->size1 - local_end);
 | 
			
		||||
                    gap->size1 -= local_end;
 | 
			
		||||
                }
 | 
			
		||||
                gap->gap_size = fixed_width_buffer_size - gap->size2 - gap->size1;
 | 
			
		||||
        
 | 
			
		||||
                --gap;
 | 
			
		||||
            }
 | 
			
		||||
            else{
 | 
			
		||||
                buffer->grow_gaps = 1;
 | 
			
		||||
                *request_amount = round_up_4tech(sizeof(*gaps)*(supplanted_gaps+buffer->chunk_max*2), 4<<10);
 | 
			
		||||
                result = 1;
 | 
			
		||||
                goto mugab_replace_range_end;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        buffer->edit_stage = 2;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    start -= gap->start_pos;
 | 
			
		||||
    end -= gap->start_pos;
 | 
			
		||||
    assert_4tech(start >= 0 && end >= 0);
 | 
			
		||||
    if (end > gap->size1 + gap->size2) end = gap->size1 + gap->size2;
 | 
			
		||||
    shift_amount = (len - (end - start));
 | 
			
		||||
    
 | 
			
		||||
    if (shift_amount + gap->size1 + gap->size2 <= fixed_width_buffer_size){
 | 
			
		||||
        data = gap->data;
 | 
			
		||||
        debug_4tech(osize = gap->size1 + gap->size2);
 | 
			
		||||
        if (end < gap->size1){
 | 
			
		||||
            move_size = gap->size1 - end;
 | 
			
		||||
            memmove_4tech(data + gap->size1 + gap->gap_size - move_size, data + end, move_size);
 | 
			
		||||
            gap->size1 -= move_size;
 | 
			
		||||
            gap->size2 += move_size;
 | 
			
		||||
        }
 | 
			
		||||
        if (start > gap->size1){
 | 
			
		||||
            move_size = start - gap->size1;
 | 
			
		||||
            memmove_4tech(data + gap->size1, data + gap->size1 + gap->gap_size, move_size);
 | 
			
		||||
            gap->size1 += move_size;
 | 
			
		||||
            gap->size2 -= move_size;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        memcpy_4tech(data + start, str, len);
 | 
			
		||||
        gap->size2 = fixed_width_buffer_size - (end + gap->gap_size);
 | 
			
		||||
        gap->size1 = start + len;
 | 
			
		||||
        gap->gap_size -= shift_amount;
 | 
			
		||||
        
 | 
			
		||||
        assert_4tech(gap->size1 + gap->size2 == osize + shift_amount);
 | 
			
		||||
        assert_4tech(gap->size1 + gap->gap_size + gap->size2 == fixed_width_buffer_size);
 | 
			
		||||
        
 | 
			
		||||
        result = 0;
 | 
			
		||||
        buffer->edit_stage = 0;
 | 
			
		||||
 | 
			
		||||
        if (gap_start < gap_end){
 | 
			
		||||
            ++gap;
 | 
			
		||||
            gap->start_pos += shift_amount;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        required_empty_buffers = div_ceil_4tech(shift_amount, fixed_width_buffer_half_size);
 | 
			
		||||
        if (buffer->chunk_alloced - buffer->chunk_count >= required_empty_buffers){
 | 
			
		||||
            if (buffer->chunk_max - buffer->chunk_alloced >= required_empty_buffers){
 | 
			
		||||
                memcpy_4tech(gaps + buffer->chunk_alloced, gaps + buffer->chunk_count, sizeof(*gaps)*required_empty_buffers);
 | 
			
		||||
                memmove_4tech(gap + required_empty_buffers + 1, gap + 1, sizeof(*gaps)*(buffer->chunk_count - gap_start - 1));
 | 
			
		||||
                memcpy_4tech(gap + 1, gaps + buffer->chunk_alloced, sizeof(*gaps)*required_empty_buffers);
 | 
			
		||||
                
 | 
			
		||||
                data = gap->data;
 | 
			
		||||
                if (end < gap->size1){
 | 
			
		||||
                    move_size = gap->size1 - end;
 | 
			
		||||
                    memmove_4tech(data + gap->size1 + gap->gap_size - move_size, data + end, move_size);
 | 
			
		||||
                    gap->size1 -= move_size + (end - start);
 | 
			
		||||
                    gap->size2 += move_size;
 | 
			
		||||
                }
 | 
			
		||||
                else if (start > gap->size1){
 | 
			
		||||
                    move_size = start - gap->size1;
 | 
			
		||||
                    memmove_4tech(data + gap->size1, data + gap->size1 + gap->gap_size, move_size);
 | 
			
		||||
                    gap->size1 += move_size;
 | 
			
		||||
                    gap->size2 -= move_size + (end - start);
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    if (end > gap->size1){
 | 
			
		||||
                        gap->size2 -= (end - gap->size1);
 | 
			
		||||
                    }
 | 
			
		||||
                    gap->size1 = start;
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                dgap = gap + required_empty_buffers;
 | 
			
		||||
                dpos = gap->size1 + gap->gap_size;
 | 
			
		||||
                memcpy_4tech(dgap->data + dpos, data + dpos, gap->size2);
 | 
			
		||||
                dgap->size2 = gap->size2;
 | 
			
		||||
                gap->size2 = 0;
 | 
			
		||||
                
 | 
			
		||||
                tail_size = fixed_width_buffer_half_size - dgap->size2;
 | 
			
		||||
                if (tail_size < 0) tail_size = 0;
 | 
			
		||||
 | 
			
		||||
                if (tail_size < len){
 | 
			
		||||
                    middle_size = div_ceil_4tech(len - tail_size, (required_empty_buffers * 2));
 | 
			
		||||
                
 | 
			
		||||
                    head_size = middle_size;
 | 
			
		||||
                
 | 
			
		||||
                    if (head_size + gap->size1 + 256 > fixed_width_buffer_size){
 | 
			
		||||
                        head_size = fixed_width_buffer_size - gap->size1 - 256;
 | 
			
		||||
                        if (head_size < 0) head_size = 0;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (required_empty_buffers-1 > 0){
 | 
			
		||||
                        middle_size = div_ceil_4tech(len - head_size - tail_size, (required_empty_buffers-1)*2);
 | 
			
		||||
                    }
 | 
			
		||||
                    else{
 | 
			
		||||
                        middle_size = 0;
 | 
			
		||||
                        head_size = len - tail_size;
 | 
			
		||||
                        assert_4tech(head_size + gap->size1 <= fixed_width_buffer_size);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                else{
 | 
			
		||||
                    middle_size = 0;
 | 
			
		||||
                    head_size = 0;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                mem_pos = 0;
 | 
			
		||||
                if (head_size > len - mem_pos) head_size = len - mem_pos;
 | 
			
		||||
                
 | 
			
		||||
                gap->size2 = head_size;
 | 
			
		||||
                gap->gap_size = fixed_width_buffer_size - gap->size1 - gap->size2;
 | 
			
		||||
                memcpy_4tech(gap->data + fixed_width_buffer_size - head_size, str + mem_pos, head_size);
 | 
			
		||||
                mem_pos += head_size;
 | 
			
		||||
                local_start_pos = gap->start_pos + gap->size1 + gap->size2;
 | 
			
		||||
                
 | 
			
		||||
                ++gap;
 | 
			
		||||
                for (;gap < dgap; ++gap){
 | 
			
		||||
                    gap->start_pos = local_start_pos;
 | 
			
		||||
                    
 | 
			
		||||
                    if (middle_size > len - mem_pos) middle_size = len - mem_pos;
 | 
			
		||||
                    gap->size1 = middle_size;
 | 
			
		||||
                    memcpy_4tech(gap->data, str + mem_pos, middle_size);
 | 
			
		||||
                    mem_pos += middle_size;
 | 
			
		||||
                    
 | 
			
		||||
                    if (middle_size > len - mem_pos) middle_size = len - mem_pos;
 | 
			
		||||
                    gap->size2 = middle_size;
 | 
			
		||||
                    memcpy_4tech(gap->data + fixed_width_buffer_size - middle_size, str + mem_pos, middle_size);
 | 
			
		||||
                    mem_pos += middle_size;
 | 
			
		||||
                    
 | 
			
		||||
                    gap->gap_size = fixed_width_buffer_size - (gap->size1 + gap->size2);
 | 
			
		||||
                    local_start_pos += gap->size1 + gap->size2;
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                if (tail_size > len - mem_pos) tail_size = len - mem_pos;
 | 
			
		||||
                gap->start_pos = local_start_pos;
 | 
			
		||||
                gap->size1 = tail_size;
 | 
			
		||||
                gap->gap_size = fixed_width_buffer_size - gap->size1 - gap->size2;
 | 
			
		||||
                memcpy_4tech(gap->data, str + mem_pos, tail_size);
 | 
			
		||||
                mem_pos += tail_size;
 | 
			
		||||
                assert_4tech(mem_pos == len);
 | 
			
		||||
                debug_4tech(local_start_pos += gap->size1 + gap->size2);
 | 
			
		||||
                //assert_4tech(local_start_pos == buffer->size);
 | 
			
		||||
 | 
			
		||||
                buffer->chunk_count += required_empty_buffers;
 | 
			
		||||
 | 
			
		||||
                result = 0;
 | 
			
		||||
                buffer->edit_stage = 0;
 | 
			
		||||
            }
 | 
			
		||||
            else{
 | 
			
		||||
                buffer->grow_gaps = 1;
 | 
			
		||||
                *request_amount = round_up_4tech(sizeof(*gaps)*(required_empty_buffers+buffer->chunk_max*2), 4<<10);
 | 
			
		||||
                result = 1;
 | 
			
		||||
                goto mugab_replace_range_end;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            if (buffer->chunk_alloced < buffer->chunk_max){
 | 
			
		||||
                *request_amount = fixed_width_buffer_size;
 | 
			
		||||
                result = 1;
 | 
			
		||||
            }
 | 
			
		||||
            else{
 | 
			
		||||
                buffer->grow_gaps = 1;
 | 
			
		||||
                *request_amount = round_up_4tech(sizeof(*gaps)*(1+buffer->chunk_max*2), 4<<10);
 | 
			
		||||
                result = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
mugab_replace_range_end:
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_mugab_check(Multi_Gap_Buffer *buffer){
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
    int result;
 | 
			
		||||
    int total_size;
 | 
			
		||||
    int i, count;
 | 
			
		||||
    
 | 
			
		||||
    assert_4tech(buffer->chunk_count <= buffer->chunk_alloced);
 | 
			
		||||
    assert_4tech(buffer->chunk_alloced <= buffer->chunk_max);
 | 
			
		||||
    
 | 
			
		||||
    count = buffer->chunk_count;
 | 
			
		||||
    
 | 
			
		||||
    total_size = 0;
 | 
			
		||||
    gap = buffer->gaps;
 | 
			
		||||
    for (i = 0; i < count; ++i, ++gap){
 | 
			
		||||
        assert_4tech(gap->size1 + gap->size2 + gap->gap_size == fixed_width_buffer_size);
 | 
			
		||||
        assert_4tech(gap->start_pos == total_size);
 | 
			
		||||
        total_size += gap->size1 + gap->size2;
 | 
			
		||||
    }
 | 
			
		||||
    assert_4tech(total_size == buffer->size);
 | 
			
		||||
    
 | 
			
		||||
    assert_4tech(buffer->edit_stage == 0);
 | 
			
		||||
    assert_4tech(buffer->grow_gaps == 0);
 | 
			
		||||
    
 | 
			
		||||
    result = 1;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NOTE(allen): This could should be optimized for Multi_Gap_Buffer
 | 
			
		||||
internal_4tech int
 | 
			
		||||
buffer_batch_edit_step(Buffer_Batch_State *state, Multi_Gap_Buffer *buffer, Buffer_Edit *sorted_edits,
 | 
			
		||||
                       char *strings, int edit_count, void *scratch, int scratch_size, int *request_amount){
 | 
			
		||||
    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,
 | 
			
		||||
                                      strings + edit->str_start, edit->len, &shift_amount,
 | 
			
		||||
                                      scratch, scratch_size, request_amount);
 | 
			
		||||
        if (result) break;
 | 
			
		||||
        buffer_mugab_check(buffer);
 | 
			
		||||
        shift_total += shift_amount;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    state->shift_total = shift_total;
 | 
			
		||||
    state->i = i;
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
internal_4tech void*
 | 
			
		||||
buffer_edit_provide_memory(Multi_Gap_Buffer *buffer, void *new_data, int size){
 | 
			
		||||
    void *result;
 | 
			
		||||
    Fixed_Width_Gap_Buffer *gap;
 | 
			
		||||
 | 
			
		||||
    if (buffer->grow_gaps){
 | 
			
		||||
        assert_4tech(size >= buffer->chunk_max*sizeof(*buffer->gaps));
 | 
			
		||||
        
 | 
			
		||||
        result = buffer->gaps;
 | 
			
		||||
        memcpy_4tech(new_data, buffer->gaps, buffer->chunk_alloced*sizeof(*buffer->gaps));
 | 
			
		||||
        buffer->gaps = (Fixed_Width_Gap_Buffer*)new_data;
 | 
			
		||||
        buffer->chunk_max = size / sizeof(*buffer->gaps);
 | 
			
		||||
        buffer->grow_gaps = 0;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    else{
 | 
			
		||||
        assert_4tech(buffer->chunk_max > buffer->chunk_alloced);
 | 
			
		||||
        assert_4tech(size >= fixed_width_buffer_size);
 | 
			
		||||
        
 | 
			
		||||
        gap = &buffer->gaps[buffer->chunk_alloced++];
 | 
			
		||||
        memzero_4tech(*gap);
 | 
			
		||||
        gap->data = (char*)new_data;
 | 
			
		||||
        result = 0;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -1,184 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 08.11.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Buffer experiment testing layer, abstract portion
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
#define Buffer_Init_Type cat_4tech(Buffer_Type, _Init)
 | 
			
		||||
#define Buffer_Stringify_Type cat_4tech(Buffer_Type, _Stringify_Loop)
 | 
			
		||||
#define Buffer_Backify_Type cat_4tech(Buffer_Type, _Backify_Loop)
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
init_buffer(Buffer_Type *buffer, File_Data file, void *scratch, int scratch_size){
 | 
			
		||||
    memzero_4tech(*buffer);
 | 
			
		||||
    Buffer_Init_Type init;
 | 
			
		||||
    for (init = buffer_begin_init(buffer, file.data, file.size);
 | 
			
		||||
         buffer_init_need_more(&init);){
 | 
			
		||||
        int page_size = buffer_init_page_size(&init);
 | 
			
		||||
        void *page = malloc(page_size);
 | 
			
		||||
        buffer_init_provide_page(&init, page, page_size);
 | 
			
		||||
    }
 | 
			
		||||
    debug_4tech(int result =)
 | 
			
		||||
        buffer_end_init(&init, scratch, scratch_size);
 | 
			
		||||
    assert_4tech(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
measure_starts_widths(Buffer_Type *buffer, float *font_widths){
 | 
			
		||||
    if (buffer->line_max == 0){
 | 
			
		||||
        assert_4tech(buffer->line_starts == 0);
 | 
			
		||||
        assert_4tech(buffer->line_widths == 0);
 | 
			
		||||
        assert_4tech(buffer->widths_max == 0);
 | 
			
		||||
        int max = 1 << 10;
 | 
			
		||||
        buffer->line_starts = (int*)malloc(max*sizeof(int));
 | 
			
		||||
        buffer->line_max = max;
 | 
			
		||||
        buffer->line_widths = (float*)malloc(max*sizeof(float));
 | 
			
		||||
        buffer->widths_max = max;
 | 
			
		||||
    }
 | 
			
		||||
    assert_4tech(buffer->line_starts != 0);
 | 
			
		||||
    assert_4tech(buffer->widths_max != 0);
 | 
			
		||||
    assert_4tech(buffer->line_widths != 0);
 | 
			
		||||
 | 
			
		||||
    Buffer_Measure_Starts state;
 | 
			
		||||
    memzero_4tech(state);
 | 
			
		||||
    for (;buffer_measure_starts_widths(&state, buffer, font_widths);){
 | 
			
		||||
        int max = buffer->line_max;
 | 
			
		||||
        int count = state.count;
 | 
			
		||||
        int target = count + 1;
 | 
			
		||||
 | 
			
		||||
        max = target*2;
 | 
			
		||||
        int *new_lines = (int*)malloc(max*sizeof(int));
 | 
			
		||||
        memcpy_4tech(new_lines, buffer->line_starts, count*sizeof(int));
 | 
			
		||||
        free(buffer->line_starts);
 | 
			
		||||
        buffer->line_starts = new_lines;
 | 
			
		||||
        buffer->line_max = max;
 | 
			
		||||
 | 
			
		||||
        float *new_widths = (float*)malloc(max*sizeof(float));
 | 
			
		||||
        memcpy_4tech(new_widths, buffer->line_widths, count*sizeof(int));
 | 
			
		||||
        free(buffer->line_widths);
 | 
			
		||||
        buffer->line_widths = new_widths;
 | 
			
		||||
        buffer->widths_max = max;
 | 
			
		||||
    }
 | 
			
		||||
    buffer->line_count = state.count;
 | 
			
		||||
    buffer->widths_count = state.count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
edit(Buffer_Type *buffer, int start, int end, char *word, int len,
 | 
			
		||||
     float *advance_data, void *scratch, int scratch_size){
 | 
			
		||||
    int shift_amount, request_amount;
 | 
			
		||||
    
 | 
			
		||||
    for (;buffer_replace_range(buffer, start, end, word, len, &shift_amount,
 | 
			
		||||
                               scratch, scratch_size, &request_amount);){
 | 
			
		||||
        void *new_data = 0;
 | 
			
		||||
        if (request_amount > 0) new_data = malloc(request_amount);
 | 
			
		||||
        void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount);
 | 
			
		||||
        if (old_data) free(old_data);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    int line_start = buffer_get_line_index(buffer, start);
 | 
			
		||||
    int line_end = buffer_get_line_index(buffer, end);
 | 
			
		||||
    int replaced_line_count = line_end - line_start;
 | 
			
		||||
    int new_line_count = buffer_count_newlines(buffer, start, start+len);
 | 
			
		||||
    int line_shift =  new_line_count - replaced_line_count;
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        assert_4tech(buffer->line_starts);
 | 
			
		||||
        int count = buffer->line_count;
 | 
			
		||||
        if (count + line_shift > buffer->line_max){
 | 
			
		||||
            int new_max = round_up_4tech(buffer->line_max + line_shift, 1 << 10);
 | 
			
		||||
            int *new_lines = (int*)malloc(sizeof(int)*new_max);
 | 
			
		||||
            memcpy_4tech(new_lines, buffer->line_starts, sizeof(int)*count);
 | 
			
		||||
            free(buffer->line_starts);
 | 
			
		||||
 | 
			
		||||
            buffer->line_starts = new_lines;
 | 
			
		||||
            buffer->line_max = new_max;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        assert_4tech(buffer->line_widths);
 | 
			
		||||
        if (buffer->line_count > buffer->widths_max){
 | 
			
		||||
            int new_max = round_up_4tech(buffer->line_max, 1 << 10);
 | 
			
		||||
            float *new_widths = (float*)malloc(sizeof(float)*new_max);
 | 
			
		||||
            memcpy_4tech(new_widths, buffer->line_widths, sizeof(float)*buffer->widths_count);
 | 
			
		||||
            free(buffer->line_widths);
 | 
			
		||||
            
 | 
			
		||||
            buffer->line_widths = new_widths;
 | 
			
		||||
            buffer->widths_max = new_max;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        buffer_remeasure_widths(buffer, advance_data, line_start, line_end, line_shift);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
insert_bottom(Buffer_Type *buffer, char *word, int len, float *advance_data, void *scratch, int scratch_size){
 | 
			
		||||
    int size;
 | 
			
		||||
    size = buffer_size(buffer);
 | 
			
		||||
    edit(buffer, size, size, word, len, advance_data, scratch, scratch_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
insert_top(Buffer_Type *buffer, char *word, int len, float *advance_data, void *scratch, int scratch_size){
 | 
			
		||||
    edit(buffer, 0, 0, word, len, advance_data, scratch, scratch_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
delete_bottom(Buffer_Type *buffer, int len, float *advance_data, void *scratch, int scratch_size){
 | 
			
		||||
    int size;
 | 
			
		||||
    size = buffer_size(buffer);
 | 
			
		||||
    edit(buffer, size - len, size, 0, 0, advance_data, scratch, scratch_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
delete_top(Buffer_Type *buffer, int len, float *advance_data, void *scratch, int scratch_size){
 | 
			
		||||
    edit(buffer, 0, len, 0, 0, advance_data, scratch, scratch_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
natural_edits(Buffer_Type *buffer, float *advance_data, Replay *replay, int pos, void *scratch, int scratch_size){
 | 
			
		||||
    Edit_Step *steps = replay->replay.edits;
 | 
			
		||||
    char *base_str = replay->replay.strings;
 | 
			
		||||
    int edit_count = replay->replay.count;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < edit_count; ++i){
 | 
			
		||||
        Edit_Step step = steps[i];
 | 
			
		||||
        
 | 
			
		||||
        if (step.child_count == 0 && step.edit.end <= buffer_size(buffer)){
 | 
			
		||||
            edit(buffer, pos + step.edit.start, pos + step.edit.end, base_str + step.edit.str_start, step.edit.len,
 | 
			
		||||
                 advance_data, scratch, scratch_size);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
batch_edit(Buffer_Type *buffer, float *advance_data, Buffer_Edit *batch, char *str_base, int batch_size,
 | 
			
		||||
           void *scratch, int scratch_size){
 | 
			
		||||
    Buffer_Batch_State state;
 | 
			
		||||
    int request_amount;
 | 
			
		||||
    
 | 
			
		||||
    memzero_4tech(state);
 | 
			
		||||
    for (;buffer_batch_edit_step(&state, buffer, batch, str_base, batch_size,
 | 
			
		||||
                                 scratch, scratch_size, &request_amount);){
 | 
			
		||||
        void *new_data = 0;
 | 
			
		||||
        if (request_amount > 0) new_data = malloc(request_amount);
 | 
			
		||||
        void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount);
 | 
			
		||||
        if (old_data) free(old_data);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    measure_starts_widths(buffer, advance_data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
				
			
			@ -1,316 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 11.11.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Converts code file and saved history from 4coder
 | 
			
		||||
 *  into replay file for the test system.
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
#include "shared_test_config.cpp"
 | 
			
		||||
 | 
			
		||||
#include "4coder_shared.cpp"
 | 
			
		||||
#include "4coder_golden_array.cpp"
 | 
			
		||||
 | 
			
		||||
#define Buffer_Type Buffer
 | 
			
		||||
#include "4coder_buffer_abstract.cpp"
 | 
			
		||||
 | 
			
		||||
#include "shared_test_utils.cpp"
 | 
			
		||||
 | 
			
		||||
void do_single_edit(Replay *replay, Buffer *buffer, Edit_Step step, char *str, void *scratch, int scratch_size){
 | 
			
		||||
    {
 | 
			
		||||
        Edit_Stack *stack;
 | 
			
		||||
        char *new_data;
 | 
			
		||||
        Edit_Step inv_step;
 | 
			
		||||
        int size, new_max, new_size;
 | 
			
		||||
    
 | 
			
		||||
        stack = &replay->replay;
 | 
			
		||||
    
 | 
			
		||||
        memzero_4tech(inv_step);
 | 
			
		||||
        size = 0;
 | 
			
		||||
        buffer_invert_edit(buffer, step.edit, &inv_step.edit, (char*)scratch, &size, scratch_size);
 | 
			
		||||
    
 | 
			
		||||
        assert_4tech(stack->count < replay->ed_max);
 | 
			
		||||
        stack = &replay->replay;
 | 
			
		||||
        stack->edits[replay->ed_max - (++stack->count)] = inv_step;
 | 
			
		||||
    
 | 
			
		||||
        new_size = stack->size + size;
 | 
			
		||||
 | 
			
		||||
        if (new_size > replay->str_max){
 | 
			
		||||
            new_max = (replay->str_max + size) * 2;
 | 
			
		||||
            new_data = (char*)malloc(new_max);
 | 
			
		||||
            memcpy_4tech(new_data + new_max - stack->size, stack->strings + replay->str_max - stack->size, stack->size);
 | 
			
		||||
            free(stack->strings);
 | 
			
		||||
            stack->strings = new_data;
 | 
			
		||||
            replay->str_max = new_max;
 | 
			
		||||
        }
 | 
			
		||||
    
 | 
			
		||||
        memcpy_4tech(stack->strings + replay->str_max - new_size, scratch, size);
 | 
			
		||||
        stack->size = new_size;
 | 
			
		||||
 | 
			
		||||
        printf("just wrote: [[ %.*s ]]\n", size, scratch);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    int start = step.edit.start;
 | 
			
		||||
    int end = step.edit.end;
 | 
			
		||||
    int str_start = step.edit.str_start;
 | 
			
		||||
    int len = step.edit.len;
 | 
			
		||||
 | 
			
		||||
    int shift_amount;
 | 
			
		||||
    int request_amount;
 | 
			
		||||
    
 | 
			
		||||
    for (;buffer_replace_range(buffer, start, end, str + str_start, len, &shift_amount,
 | 
			
		||||
                               scratch, scratch_size, &request_amount);){
 | 
			
		||||
        void *new_data = 0;
 | 
			
		||||
        if (request_amount > 0) new_data = malloc(request_amount);
 | 
			
		||||
        void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount);
 | 
			
		||||
        if (old_data) free(old_data);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void do_batch_edit(Replay *replay, Buffer *buffer, Edit_Step step, void *scratch, int scratch_size){
 | 
			
		||||
    {
 | 
			
		||||
        Edit_Stack *stack;
 | 
			
		||||
        Edit_Step inv_step;
 | 
			
		||||
    
 | 
			
		||||
        stack = &replay->replay;
 | 
			
		||||
 | 
			
		||||
        memzero_4tech(inv_step);
 | 
			
		||||
        inv_step.first_child = step.inverse_first_child;
 | 
			
		||||
        inv_step.inverse_first_child = step.first_child;
 | 
			
		||||
        inv_step.child_count = step.inverse_child_count;
 | 
			
		||||
        inv_step.inverse_child_count = step.child_count;
 | 
			
		||||
        
 | 
			
		||||
        assert_4tech(stack->count < replay->ed_max);
 | 
			
		||||
        stack = &replay->replay;
 | 
			
		||||
        stack->edits[replay->ed_max - (++stack->count)] = inv_step;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    char *str;
 | 
			
		||||
    Buffer_Edit *batch;
 | 
			
		||||
    int batch_size;
 | 
			
		||||
    
 | 
			
		||||
    int request_amount;
 | 
			
		||||
    Buffer_Batch_State state;
 | 
			
		||||
 | 
			
		||||
    str = replay->children.strings;
 | 
			
		||||
    batch = replay->children.edits + step.first_child;
 | 
			
		||||
    batch_size = step.child_count;
 | 
			
		||||
    
 | 
			
		||||
    memzero_4tech(state);
 | 
			
		||||
    for (;buffer_batch_edit_step(&state, buffer, batch, str, batch_size,
 | 
			
		||||
                                 scratch, scratch_size, &request_amount);){
 | 
			
		||||
        void *new_data = 0;
 | 
			
		||||
        if (request_amount > 0) new_data = malloc(request_amount);
 | 
			
		||||
        void *old_data = buffer_edit_provide_memory(buffer, new_data, request_amount);
 | 
			
		||||
        if (old_data) free(old_data);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if 0
 | 
			
		||||
 | 
			
		||||
int main(int argc, char **argv){
 | 
			
		||||
    if (argc < 3){
 | 
			
		||||
        printf("usage: hst_to_rply <history> <target-file> <output>\n");
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    File_Data history_file, code_file;
 | 
			
		||||
    history_file = get_file(argv[1]);
 | 
			
		||||
    code_file = get_file(argv[2]);
 | 
			
		||||
 | 
			
		||||
    if (!history_file.data || !code_file.data) exit(1);
 | 
			
		||||
    
 | 
			
		||||
    History history;
 | 
			
		||||
    char *curs = history_file.data;
 | 
			
		||||
    
 | 
			
		||||
    history.undo.count = read_int(&curs);
 | 
			
		||||
    history.redo.count = read_int(&curs);
 | 
			
		||||
    history.history.count = read_int(&curs);
 | 
			
		||||
    history.children.count = read_int(&curs);
 | 
			
		||||
    
 | 
			
		||||
    history.undo.size = read_int(&curs);
 | 
			
		||||
    history.redo.size = read_int(&curs);
 | 
			
		||||
    history.history.size = read_int(&curs);
 | 
			
		||||
    history.children.size = read_int(&curs);
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    history.undo.edits = (Edit_Step*)curs;
 | 
			
		||||
    curs += sizeof(Edit_Step)*history.undo.count;
 | 
			
		||||
    
 | 
			
		||||
    history.redo.edits = (Edit_Step*)curs;
 | 
			
		||||
    curs += sizeof(Edit_Step)*history.redo.count;
 | 
			
		||||
    
 | 
			
		||||
    history.history.edits = (Edit_Step*)curs;
 | 
			
		||||
    curs += sizeof(Edit_Step)*history.history.count;
 | 
			
		||||
    
 | 
			
		||||
    history.children.edits = (Buffer_Edit*)curs;
 | 
			
		||||
    curs += sizeof(Buffer_Edit)*history.children.count;
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    history.undo.strings = (char*)curs;
 | 
			
		||||
    curs += history.undo.size;
 | 
			
		||||
    
 | 
			
		||||
    history.redo.strings = (char*)curs;
 | 
			
		||||
    curs += history.redo.size;
 | 
			
		||||
    
 | 
			
		||||
    history.history.strings = (char*)curs;
 | 
			
		||||
    curs += history.history.size;
 | 
			
		||||
    
 | 
			
		||||
    history.children.strings = (char*)curs;
 | 
			
		||||
    curs += history.children.size;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    void *scratch;
 | 
			
		||||
    int scratch_size;
 | 
			
		||||
    scratch_size = 1 << 20;
 | 
			
		||||
    scratch = malloc(scratch_size);
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    Buffer buffer;
 | 
			
		||||
    Buffer_Init_Type init;
 | 
			
		||||
    
 | 
			
		||||
    memzero_4tech(buffer);
 | 
			
		||||
    memzero_4tech(init);
 | 
			
		||||
    for (init = buffer_begin_init(&buffer, code_file.data, code_file.size);
 | 
			
		||||
         buffer_init_need_more(&init);){
 | 
			
		||||
        int page_size = buffer_init_page_size(&init);
 | 
			
		||||
        page_size = round_up_4tech(page_size, 4 << 10);
 | 
			
		||||
        void *data = malloc(page_size);
 | 
			
		||||
        buffer_init_provide_page(&init, data, page_size);
 | 
			
		||||
    }
 | 
			
		||||
    buffer_end_init(&init, scratch, scratch_size);
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    Replay replay;
 | 
			
		||||
    
 | 
			
		||||
    replay.children = history.children;
 | 
			
		||||
    replay.ed_max = history.history.count;
 | 
			
		||||
    replay.replay.edits = (Edit_Step*)malloc(sizeof(Edit_Step)*replay.ed_max);
 | 
			
		||||
    replay.replay.count = 0;
 | 
			
		||||
    
 | 
			
		||||
    replay.str_max = history.history. size * 4;
 | 
			
		||||
    replay.replay.strings = (char*)malloc(replay.str_max);
 | 
			
		||||
    replay.replay.size = 0;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    for (int i = history.history.count - 1; i >= 0; --i){
 | 
			
		||||
        Edit_Step step = history.history.edits[i];
 | 
			
		||||
        if (step.child_count == 0){
 | 
			
		||||
            do_single_edit(&replay, &buffer, step, history.history.strings, scratch, scratch_size);
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            assert(step.special_type == 1);
 | 
			
		||||
            do_batch_edit(&replay, &buffer, step, scratch, scratch_size);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert(history.history.count == replay.replay.count);
 | 
			
		||||
 | 
			
		||||
    int str_start = 0;
 | 
			
		||||
    for (int i = 0; i < history.history.count; ++i){
 | 
			
		||||
        replay.replay.edits[i].edit.str_start = str_start;
 | 
			
		||||
        str_start += replay.replay.edits[i].edit.len;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    File_Data out_file;
 | 
			
		||||
    out_file.size = 0;
 | 
			
		||||
    out_file.size += replay.replay.count*sizeof(Edit_Step) + sizeof(int);
 | 
			
		||||
    out_file.size += replay.replay.size + sizeof(int);
 | 
			
		||||
    out_file.size += replay.children.count*sizeof(Buffer_Edit) + sizeof(int);
 | 
			
		||||
    out_file.size += replay.children.size + sizeof(int);
 | 
			
		||||
    
 | 
			
		||||
    out_file.data = (char*)malloc(out_file.size);
 | 
			
		||||
    
 | 
			
		||||
    curs = out_file.data;
 | 
			
		||||
    curs = write_int(curs, replay.replay.count);
 | 
			
		||||
    curs = write_int(curs, replay.children.count);
 | 
			
		||||
    
 | 
			
		||||
    curs = write_int(curs, replay.replay.size);
 | 
			
		||||
    curs = write_int(curs, replay.children.size);
 | 
			
		||||
    
 | 
			
		||||
    memcpy(curs, replay.replay.edits, replay.replay.count*sizeof(Edit_Step));
 | 
			
		||||
    curs += replay.replay.count*sizeof(Edit_Step);
 | 
			
		||||
 | 
			
		||||
    memcpy(curs, replay.children.edits, replay.children.count*sizeof(Buffer_Edit));
 | 
			
		||||
    curs += replay.children.count*sizeof(Buffer_Edit);
 | 
			
		||||
    
 | 
			
		||||
    memcpy(curs, replay.replay.strings + replay.str_max - replay.replay.size, replay.replay.size);
 | 
			
		||||
    curs += replay.replay.size;
 | 
			
		||||
    
 | 
			
		||||
    memcpy(curs, replay.children.strings, replay.children.size);
 | 
			
		||||
    curs += replay.children.size;
 | 
			
		||||
 | 
			
		||||
    assert((int)(curs - out_file.data) == out_file.size);
 | 
			
		||||
    
 | 
			
		||||
    save_file(argv[3], out_file);
 | 
			
		||||
    
 | 
			
		||||
    return(0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
int main(int argc, char **argv){
 | 
			
		||||
    if (argc < 2){
 | 
			
		||||
        printf("usage: <replay-file>\n");
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    File_Data file = get_file(argv[1]);
 | 
			
		||||
    //char *curs = file.data;
 | 
			
		||||
    
 | 
			
		||||
    Replay replay;
 | 
			
		||||
    prepare_replay(file, &replay);
 | 
			
		||||
#if 0
 | 
			
		||||
    replay.replay.count = read_int(&curs);
 | 
			
		||||
    replay.children.count = read_int(&curs);
 | 
			
		||||
    replay.replay.size = read_int(&curs);
 | 
			
		||||
    replay.children.size = read_int(&curs);
 | 
			
		||||
    
 | 
			
		||||
    replay.replay.edits = (Edit_Step*)curs;
 | 
			
		||||
    curs += sizeof(Edit_Step)*replay.replay.count;
 | 
			
		||||
    
 | 
			
		||||
    replay.children.edits = (Buffer_Edit*)curs;
 | 
			
		||||
    curs += sizeof(Buffer_Edit)*replay.children.count;
 | 
			
		||||
    
 | 
			
		||||
    replay.replay.strings = curs;
 | 
			
		||||
    curs += replay.replay.size;
 | 
			
		||||
    
 | 
			
		||||
    replay.children.strings = curs;
 | 
			
		||||
    curs += replay.children.size;
 | 
			
		||||
 | 
			
		||||
    assert_4tech((int)(curs - file.data) == file.size);
 | 
			
		||||
#endif
 | 
			
		||||
    
 | 
			
		||||
    for (int i = 0; i < replay.replay.count; ++i){
 | 
			
		||||
        Edit_Step step = replay.replay.edits[i];
 | 
			
		||||
        if (step.first_child == 0){
 | 
			
		||||
            if (step.edit.len > 0 && step.edit.str_start >= 0 && step.edit.str_start < replay.replay.size){
 | 
			
		||||
                printf("replace [%d,%d] with %.*s\n", step.edit.start, step.edit.end,
 | 
			
		||||
                       step.edit.len, replay.replay.strings + step.edit.str_start);
 | 
			
		||||
            }
 | 
			
		||||
            else if (step.edit.len > 0){
 | 
			
		||||
                printf("str_start out of bounds!\n");
 | 
			
		||||
            }
 | 
			
		||||
            else{
 | 
			
		||||
                printf("replace [%d,%d] with ~~empty string~~\n", step.edit.start, step.edit.end);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            printf("batch edit\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    printf("string section:\n%.*s\n", replay.replay.size, replay.replay.strings);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1,58 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 11.11.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Code shared between history_to_replay.cpp and 4coder_test_main.cpp
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define inline_4tech inline
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
CEIL32(float x){
 | 
			
		||||
    int extra;
 | 
			
		||||
    extra = ((x!=(int)(x) && x>0)?1:0);
 | 
			
		||||
    extra += (int)(x);
 | 
			
		||||
    return(extra);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech int
 | 
			
		||||
DIVCEIL32(int n, int d) {
 | 
			
		||||
    int q = (n/d);
 | 
			
		||||
    q += (q*d < n);
 | 
			
		||||
    return(q);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline_4tech unsigned int
 | 
			
		||||
ROUNDPOT32(unsigned int v){
 | 
			
		||||
    v--;
 | 
			
		||||
    v |= v >> 1;
 | 
			
		||||
    v |= v >> 2;
 | 
			
		||||
    v |= v >> 4;
 | 
			
		||||
    v |= v >> 8;
 | 
			
		||||
    v |= v >> 16;
 | 
			
		||||
    v++;
 | 
			
		||||
    return(v);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO(allen): this should actually be compiler specific, it doesn't
 | 
			
		||||
// have anything to do with platform
 | 
			
		||||
#if defined(__linux__)
 | 
			
		||||
#define memzero_4tech(x) memset_4tech(&(x), 0, sizeof(x))
 | 
			
		||||
#else
 | 
			
		||||
#define memzero_4tech(x) (x = {})
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1,154 +0,0 @@
 | 
			
		|||
/* 
 | 
			
		||||
 * Mr. 4th Dimention - Allen Webster
 | 
			
		||||
 *  Four Tech
 | 
			
		||||
 *
 | 
			
		||||
 * public domain -- no warranty is offered or implied; use this code at your own risk
 | 
			
		||||
 * 
 | 
			
		||||
 * 11.11.2015
 | 
			
		||||
 * 
 | 
			
		||||
 * Code shared between history_to_replay.cpp and 4coder_test_main.cpp
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
// TOP
 | 
			
		||||
 | 
			
		||||
typedef struct File_Data{
 | 
			
		||||
    char *data;
 | 
			
		||||
    int size;
 | 
			
		||||
} File_Data;
 | 
			
		||||
 | 
			
		||||
File_Data get_file(const char *filename){
 | 
			
		||||
    FILE *file;
 | 
			
		||||
    File_Data result;
 | 
			
		||||
 | 
			
		||||
    memzero_4tech(result);
 | 
			
		||||
    
 | 
			
		||||
    file = fopen(filename, "rb");
 | 
			
		||||
    if (!file){
 | 
			
		||||
        printf("error: could not find file %s\n", filename);
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        fseek(file, 0, SEEK_END);
 | 
			
		||||
        result.size = ftell(file);
 | 
			
		||||
        fseek(file, 0, SEEK_SET);
 | 
			
		||||
 | 
			
		||||
        if (result.size == 0){        
 | 
			
		||||
            printf("error: file %s was empty\n", filename);
 | 
			
		||||
        }
 | 
			
		||||
        else{
 | 
			
		||||
            result.data = (char*)malloc(result.size);
 | 
			
		||||
            fread(result.data, result.size, 1, file);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fclose(file);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void save_file(const char *fname, File_Data file){
 | 
			
		||||
    FILE *f = fopen(fname, "wb");
 | 
			
		||||
    if (f){
 | 
			
		||||
        fwrite(file.data, 1, file.size, f);
 | 
			
		||||
        fclose(f);
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        printf("error: could not open %s\n", fname);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void free_file(File_Data file){
 | 
			
		||||
    free(file.data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct Edit_Step{
 | 
			
		||||
    int type;
 | 
			
		||||
    union{
 | 
			
		||||
        struct{
 | 
			
		||||
            int can_merge;
 | 
			
		||||
            Buffer_Edit edit;
 | 
			
		||||
            int pre_pos;
 | 
			
		||||
            int post_pos;
 | 
			
		||||
            int next_block, prev_block;
 | 
			
		||||
        };
 | 
			
		||||
        struct{
 | 
			
		||||
            int first_child;
 | 
			
		||||
            int inverse_first_child;
 | 
			
		||||
            int inverse_child_count;
 | 
			
		||||
            int special_type;
 | 
			
		||||
        };
 | 
			
		||||
    };
 | 
			
		||||
    int child_count;
 | 
			
		||||
} Edit_Step;
 | 
			
		||||
 | 
			
		||||
typedef struct Edit_Stack{
 | 
			
		||||
    char *strings;
 | 
			
		||||
    int size;
 | 
			
		||||
    
 | 
			
		||||
    Edit_Step *edits;
 | 
			
		||||
    int count;
 | 
			
		||||
} Edit_Stack;
 | 
			
		||||
 | 
			
		||||
typedef struct Small_Edit_Stack{
 | 
			
		||||
    char *strings;
 | 
			
		||||
    int size;
 | 
			
		||||
    
 | 
			
		||||
    Buffer_Edit *edits;
 | 
			
		||||
    int count;
 | 
			
		||||
} Small_Edit_Stack;
 | 
			
		||||
 | 
			
		||||
typedef struct History{
 | 
			
		||||
    Edit_Stack undo;
 | 
			
		||||
    Edit_Stack redo;
 | 
			
		||||
    Edit_Stack history;
 | 
			
		||||
    Small_Edit_Stack children;
 | 
			
		||||
} History;
 | 
			
		||||
 | 
			
		||||
typedef struct Replay{
 | 
			
		||||
    Edit_Stack replay;
 | 
			
		||||
    Small_Edit_Stack children;
 | 
			
		||||
 | 
			
		||||
    int str_max;
 | 
			
		||||
    int ed_max;
 | 
			
		||||
} Replay;
 | 
			
		||||
 | 
			
		||||
int read_int(char **curs){
 | 
			
		||||
    int result;
 | 
			
		||||
    result = *(int*)(*curs);
 | 
			
		||||
    *curs += 4;
 | 
			
		||||
    return(result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char* write_int(char *curs, int x){
 | 
			
		||||
    *(int*)(curs) = x;
 | 
			
		||||
    curs += 4;
 | 
			
		||||
    return(curs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void prepare_replay(File_Data file, Replay *replay){
 | 
			
		||||
    char *curs = file.data;
 | 
			
		||||
    
 | 
			
		||||
    replay->replay.count = read_int(&curs);
 | 
			
		||||
    replay->children.count = read_int(&curs);
 | 
			
		||||
    replay->replay.size = read_int(&curs);
 | 
			
		||||
    replay->children.size = read_int(&curs);
 | 
			
		||||
    
 | 
			
		||||
    replay->replay.edits = (Edit_Step*)curs;
 | 
			
		||||
    curs += sizeof(Edit_Step)*replay->replay.count;
 | 
			
		||||
    
 | 
			
		||||
    replay->children.edits = (Buffer_Edit*)curs;
 | 
			
		||||
    curs += sizeof(Buffer_Edit)*replay->children.count;
 | 
			
		||||
    
 | 
			
		||||
    replay->replay.strings = curs;
 | 
			
		||||
    curs += replay->replay.size;
 | 
			
		||||
    
 | 
			
		||||
    replay->children.strings = curs;
 | 
			
		||||
    curs += replay->children.size;
 | 
			
		||||
 | 
			
		||||
    assert_4tech((int)(curs - file.data) == file.size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BOTTOM
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue