From ab12f73e67b04fc7de04d2216895bfceafc97b5d Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Tue, 11 Jun 2019 16:16:27 -0700 Subject: [PATCH] Seek string in the core --- 4coder_base_commands.cpp | 4 +- 4coder_generated/app_functions.h | 7 + 4coder_generated/command_metadata.h | 20 +-- 4coder_helper.cpp | 34 +---- 4coder_seek.cpp | 196 +++++++--------------------- 4ed_api_implementation.cpp | 177 +++++++++++++++++++------ 4ed_buffer.cpp | 45 +++++++ 4ed_buffer.h | 6 + 8 files changed, 265 insertions(+), 224 deletions(-) diff --git a/4coder_base_commands.cpp b/4coder_base_commands.cpp index 1cc97fe7..7686f591 100644 --- a/4coder_base_commands.cpp +++ b/4coder_base_commands.cpp @@ -1496,7 +1496,7 @@ delete_file_base(Application_Links *app, String_Const_u8 file_name, Buffer_ID bu string_list_pushf(scratch, &list, "\"%.*s\"", string_expand(file_name)); String_Const_u8 cmd = string_list_flatten(scratch, list, StringFill_NullTerminate); exec_system_command(app, 0, buffer_identifier(0), path, cmd, 0); - kill_buffer(app, buffer_identifier(buffer_id), 0, BufferKill_AlwaysKill); + kill_buffer(app, buffer_identifier(buffer_id), 0, BufferKill_AlwaysKill); } CUSTOM_COMMAND_SIG(delete_file_query) @@ -2124,7 +2124,7 @@ CUSTOM_DOC("Advances backward through the undo history in the buffer containing buffer_history_get_current_state_index(app, buffer, &index); if (index > 0){ Record_Info record = {}; - buffer_history_get_record_info(app, buffer, index, &record); + buffer_history_get_record_info(app, buffer, index, &record); if (record.edit_number == highest_edit_number){ did_match = true; new_edit_position = record_get_new_cursor_position_undo(app, buffer, index, record); diff --git a/4coder_generated/app_functions.h b/4coder_generated/app_functions.h index 6d9cac64..16b15d0e 100644 --- a/4coder_generated/app_functions.h +++ b/4coder_generated/app_functions.h @@ -20,6 +20,7 @@ struct Application_Links; #define BUFFER_READ_RANGE_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id, i32 start, i32 one_past_last, char *out) #define BUFFER_REPLACE_RANGE_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id, Range range, String_Const_u8 string) #define BUFFER_BATCH_EDIT_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id, char *str, Buffer_Edit *edits, i32 edit_count) +#define BUFFER_SEEK_STRING_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer, String_Const_u8 needle, Scan_Direction direction, i32 start_pos, i32 *pos_out, b32 *case_sensitive_out) #define BUFFER_SEEK_CHARACTER_CLASS_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id, Character_Predicate *predicate, Scan_Direction direction, i32 start_pos, i32 *pos_out) #define BUFFER_COMPUTE_CURSOR_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id, Buffer_Seek seek, Partial_Cursor *cursor_out) #define BUFFER_EXISTS_SIG(n) b32 n(Application_Links *app, Buffer_ID buffer_id) @@ -204,6 +205,7 @@ typedef GET_BUFFER_BY_FILE_NAME_SIG(Get_Buffer_By_File_Name_Function); typedef BUFFER_READ_RANGE_SIG(Buffer_Read_Range_Function); typedef BUFFER_REPLACE_RANGE_SIG(Buffer_Replace_Range_Function); typedef BUFFER_BATCH_EDIT_SIG(Buffer_Batch_Edit_Function); +typedef BUFFER_SEEK_STRING_SIG(Buffer_Seek_String_Function); typedef BUFFER_SEEK_CHARACTER_CLASS_SIG(Buffer_Seek_Character_Class_Function); typedef BUFFER_COMPUTE_CURSOR_SIG(Buffer_Compute_Cursor_Function); typedef BUFFER_EXISTS_SIG(Buffer_Exists_Function); @@ -390,6 +392,7 @@ Get_Buffer_By_File_Name_Function *get_buffer_by_file_name; Buffer_Read_Range_Function *buffer_read_range; Buffer_Replace_Range_Function *buffer_replace_range; Buffer_Batch_Edit_Function *buffer_batch_edit; +Buffer_Seek_String_Function *buffer_seek_string; Buffer_Seek_Character_Class_Function *buffer_seek_character_class; Buffer_Compute_Cursor_Function *buffer_compute_cursor; Buffer_Exists_Function *buffer_exists; @@ -575,6 +578,7 @@ Get_Buffer_By_File_Name_Function *get_buffer_by_file_name_; Buffer_Read_Range_Function *buffer_read_range_; Buffer_Replace_Range_Function *buffer_replace_range_; Buffer_Batch_Edit_Function *buffer_batch_edit_; +Buffer_Seek_String_Function *buffer_seek_string_; Buffer_Seek_Character_Class_Function *buffer_seek_character_class_; Buffer_Compute_Cursor_Function *buffer_compute_cursor_; Buffer_Exists_Function *buffer_exists_; @@ -768,6 +772,7 @@ app_links->get_buffer_by_file_name_ = Get_Buffer_By_File_Name;\ app_links->buffer_read_range_ = Buffer_Read_Range;\ app_links->buffer_replace_range_ = Buffer_Replace_Range;\ app_links->buffer_batch_edit_ = Buffer_Batch_Edit;\ +app_links->buffer_seek_string_ = Buffer_Seek_String;\ app_links->buffer_seek_character_class_ = Buffer_Seek_Character_Class;\ app_links->buffer_compute_cursor_ = Buffer_Compute_Cursor;\ app_links->buffer_exists_ = Buffer_Exists;\ @@ -953,6 +958,7 @@ static b32 get_buffer_by_file_name(Application_Links *app, String_Const_u8 file_ static b32 buffer_read_range(Application_Links *app, Buffer_ID buffer_id, i32 start, i32 one_past_last, char *out){return(app->buffer_read_range(app, buffer_id, start, one_past_last, out));} static b32 buffer_replace_range(Application_Links *app, Buffer_ID buffer_id, Range range, String_Const_u8 string){return(app->buffer_replace_range(app, buffer_id, range, string));} static b32 buffer_batch_edit(Application_Links *app, Buffer_ID buffer_id, char *str, Buffer_Edit *edits, i32 edit_count){return(app->buffer_batch_edit(app, buffer_id, str, edits, edit_count));} +static b32 buffer_seek_string(Application_Links *app, Buffer_ID buffer, String_Const_u8 needle, Scan_Direction direction, i32 start_pos, i32 *pos_out, b32 *case_sensitive_out){return(app->buffer_seek_string(app, buffer, needle, direction, start_pos, pos_out, case_sensitive_out));} static b32 buffer_seek_character_class(Application_Links *app, Buffer_ID buffer_id, Character_Predicate *predicate, Scan_Direction direction, i32 start_pos, i32 *pos_out){return(app->buffer_seek_character_class(app, buffer_id, predicate, direction, start_pos, pos_out));} static b32 buffer_compute_cursor(Application_Links *app, Buffer_ID buffer_id, Buffer_Seek seek, Partial_Cursor *cursor_out){return(app->buffer_compute_cursor(app, buffer_id, seek, cursor_out));} static b32 buffer_exists(Application_Links *app, Buffer_ID buffer_id){return(app->buffer_exists(app, buffer_id));} @@ -1138,6 +1144,7 @@ static b32 get_buffer_by_file_name(Application_Links *app, String_Const_u8 file_ static b32 buffer_read_range(Application_Links *app, Buffer_ID buffer_id, i32 start, i32 one_past_last, char *out){return(app->buffer_read_range_(app, buffer_id, start, one_past_last, out));} static b32 buffer_replace_range(Application_Links *app, Buffer_ID buffer_id, Range range, String_Const_u8 string){return(app->buffer_replace_range_(app, buffer_id, range, string));} static b32 buffer_batch_edit(Application_Links *app, Buffer_ID buffer_id, char *str, Buffer_Edit *edits, i32 edit_count){return(app->buffer_batch_edit_(app, buffer_id, str, edits, edit_count));} +static b32 buffer_seek_string(Application_Links *app, Buffer_ID buffer, String_Const_u8 needle, Scan_Direction direction, i32 start_pos, i32 *pos_out, b32 *case_sensitive_out){return(app->buffer_seek_string_(app, buffer, needle, direction, start_pos, pos_out, case_sensitive_out));} static b32 buffer_seek_character_class(Application_Links *app, Buffer_ID buffer_id, Character_Predicate *predicate, Scan_Direction direction, i32 start_pos, i32 *pos_out){return(app->buffer_seek_character_class_(app, buffer_id, predicate, direction, start_pos, pos_out));} static b32 buffer_compute_cursor(Application_Links *app, Buffer_ID buffer_id, Buffer_Seek seek, Partial_Cursor *cursor_out){return(app->buffer_compute_cursor_(app, buffer_id, seek, cursor_out));} static b32 buffer_exists(Application_Links *app, Buffer_ID buffer_id){return(app->buffer_exists_(app, buffer_id));} diff --git a/4coder_generated/command_metadata.h b/4coder_generated/command_metadata.h index c0df6c94..56336e0d 100644 --- a/4coder_generated/command_metadata.h +++ b/4coder_generated/command_metadata.h @@ -256,12 +256,12 @@ int32_t line_number; }; static Command_Metadata fcoder_metacmd_table[234] = { { PROC_LINKS(replace_all_occurrences, 0), "replace_all_occurrences", 23, "Queries the user for two strings, and replaces all occurrences of the first string with the second string in all open buffers.", 126, "w:\\4ed\\code\\4coder_experiments.cpp", 34, 806 }, -{ PROC_LINKS(seek_beginning_of_textual_line, 0), "seek_beginning_of_textual_line", 30, "Seeks the cursor to the beginning of the line across all text.", 62, "w:\\4ed\\code\\4coder_seek.cpp", 27, 235 }, -{ PROC_LINKS(seek_end_of_textual_line, 0), "seek_end_of_textual_line", 24, "Seeks the cursor to the end of the line across all text.", 56, "w:\\4ed\\code\\4coder_seek.cpp", 27, 241 }, -{ PROC_LINKS(seek_beginning_of_line, 0), "seek_beginning_of_line", 22, "Seeks the cursor to the beginning of the visual line.", 53, "w:\\4ed\\code\\4coder_seek.cpp", 27, 247 }, -{ PROC_LINKS(seek_end_of_line, 0), "seek_end_of_line", 16, "Seeks the cursor to the end of the visual line.", 47, "w:\\4ed\\code\\4coder_seek.cpp", 27, 253 }, -{ PROC_LINKS(goto_beginning_of_file, 0), "goto_beginning_of_file", 22, "Sets the cursor to the beginning of the file.", 45, "w:\\4ed\\code\\4coder_seek.cpp", 27, 259 }, -{ PROC_LINKS(goto_end_of_file, 0), "goto_end_of_file", 16, "Sets the cursor to the end of the file.", 39, "w:\\4ed\\code\\4coder_seek.cpp", 27, 268 }, +{ PROC_LINKS(seek_beginning_of_textual_line, 0), "seek_beginning_of_textual_line", 30, "Seeks the cursor to the beginning of the line across all text.", 62, "w:\\4ed\\code\\4coder_seek.cpp", 27, 303 }, +{ PROC_LINKS(seek_end_of_textual_line, 0), "seek_end_of_textual_line", 24, "Seeks the cursor to the end of the line across all text.", 56, "w:\\4ed\\code\\4coder_seek.cpp", 27, 309 }, +{ PROC_LINKS(seek_beginning_of_line, 0), "seek_beginning_of_line", 22, "Seeks the cursor to the beginning of the visual line.", 53, "w:\\4ed\\code\\4coder_seek.cpp", 27, 315 }, +{ PROC_LINKS(seek_end_of_line, 0), "seek_end_of_line", 16, "Seeks the cursor to the end of the visual line.", 47, "w:\\4ed\\code\\4coder_seek.cpp", 27, 321 }, +{ PROC_LINKS(goto_beginning_of_file, 0), "goto_beginning_of_file", 22, "Sets the cursor to the beginning of the file.", 45, "w:\\4ed\\code\\4coder_seek.cpp", 27, 327 }, +{ PROC_LINKS(goto_end_of_file, 0), "goto_end_of_file", 16, "Sets the cursor to the end of the file.", 39, "w:\\4ed\\code\\4coder_seek.cpp", 27, 336 }, { PROC_LINKS(change_active_panel, 0), "change_active_panel", 19, "Change the currently active panel, moving to the panel with the next highest view_id.", 85, "w:\\4ed\\code\\4coder_default_framework.cpp", 40, 206 }, { PROC_LINKS(change_active_panel_backwards, 0), "change_active_panel_backwards", 29, "Change the currently active panel, moving to the panel with the next lowest view_id.", 84, "w:\\4ed\\code\\4coder_default_framework.cpp", 40, 217 }, { PROC_LINKS(open_panel_vsplit, 0), "open_panel_vsplit", 17, "Create a new panel by vertically splitting the active panel.", 60, "w:\\4ed\\code\\4coder_default_framework.cpp", 40, 228 }, @@ -394,10 +394,10 @@ static Command_Metadata fcoder_metacmd_table[234] = { { PROC_LINKS(interactive_new, 0), "interactive_new", 15, "Interactively creates a new file.", 33, "w:\\4ed\\code\\4coder_lists.cpp", 28, 885 }, { PROC_LINKS(interactive_open, 0), "interactive_open", 16, "Interactively opens a file.", 27, "w:\\4ed\\code\\4coder_lists.cpp", 28, 919 }, { PROC_LINKS(command_lister, 0), "command_lister", 14, "Opens an interactive list of all registered commands.", 53, "w:\\4ed\\code\\4coder_lists.cpp", 28, 1004 }, -{ PROC_LINKS(auto_tab_whole_file, 0), "auto_tab_whole_file", 19, "Audo-indents the entire current buffer.", 39, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 608 }, -{ PROC_LINKS(auto_tab_line_at_cursor, 0), "auto_tab_line_at_cursor", 23, "Auto-indents the line on which the cursor sits.", 47, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 621 }, -{ PROC_LINKS(auto_tab_range, 0), "auto_tab_range", 14, "Auto-indents the range between the cursor and the mark.", 55, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 635 }, -{ PROC_LINKS(write_and_auto_tab, 0), "write_and_auto_tab", 18, "Inserts a character and auto-indents the line on which the cursor sits.", 71, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 648 }, +{ PROC_LINKS(auto_tab_whole_file, 0), "auto_tab_whole_file", 19, "Audo-indents the entire current buffer.", 39, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 606 }, +{ PROC_LINKS(auto_tab_line_at_cursor, 0), "auto_tab_line_at_cursor", 23, "Auto-indents the line on which the cursor sits.", 47, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 619 }, +{ PROC_LINKS(auto_tab_range, 0), "auto_tab_range", 14, "Auto-indents the range between the cursor and the mark.", 55, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 633 }, +{ PROC_LINKS(write_and_auto_tab, 0), "write_and_auto_tab", 18, "Inserts a character and auto-indents the line on which the cursor sits.", 71, "w:\\4ed\\code\\4coder_auto_indent.cpp", 34, 646 }, { PROC_LINKS(list_all_locations, 0), "list_all_locations", 18, "Queries the user for a string and lists all exact case-sensitive matches found in all open buffers.", 99, "w:\\4ed\\code\\4coder_search.cpp", 29, 723 }, { PROC_LINKS(list_all_substring_locations, 0), "list_all_substring_locations", 28, "Queries the user for a string and lists all case-sensitive substring matches found in all open buffers.", 103, "w:\\4ed\\code\\4coder_search.cpp", 29, 730 }, { PROC_LINKS(list_all_locations_case_insensitive, 0), "list_all_locations_case_insensitive", 35, "Queries the user for a string and lists all exact case-insensitive matches found in all open buffers.", 101, "w:\\4ed\\code\\4coder_search.cpp", 29, 737 }, diff --git a/4coder_helper.cpp b/4coder_helper.cpp index a099f721..55853100 100644 --- a/4coder_helper.cpp +++ b/4coder_helper.cpp @@ -1178,24 +1178,6 @@ get_pos_of_blank_line_grouped(Application_Links *app, Buffer_ID buffer, Scan_Dir //////////////////////////////// -static i32 -round_down(i32 x, i32 b){ - i32 r = 0; - if (x >= 0){ - r = x - (x % b); - } - return(r); -} - -static i32 -round_up(i32 x, i32 b){ - i32 r = 0; - if (x >= 0){ - r = x + b - (x % b); - } - return(r); -} - static void exec_command(Application_Links *app, Custom_Command_Function *func){ func(app); @@ -1534,8 +1516,8 @@ init_stream_chunk(Stream_Chunk *chunk, Application_Links *app, Buffer_ID buffer_ chunk->buffer_id = buffer_id; chunk->base_data = data; chunk->data_size = size; - chunk->start = round_down(pos, size); - chunk->end = round_up(pos, size); + chunk->start = round_down_i32(pos, size); + chunk->end = round_up_i32(pos, size); if (chunk->max_end > buffer_size || chunk->max_end == 0){ chunk->max_end = buffer_size; @@ -1643,16 +1625,12 @@ init_stream_tokens(Stream_Tokens_DEP *stream, Application_Links *app, Buffer_ID stream->buffer_id = buffer_id; stream->base_tokens = data; stream->count = count; - stream->start = round_down(pos, count); - stream->end = round_up(pos, count); + stream->start = round_down_i32(pos, count); + stream->end = round_up_i32(pos, count); stream->token_count = token_count; - if (stream->start < 0){ - stream->start = 0; - } - if (stream->end > stream->token_count){ - stream->end = stream->token_count; - } + stream->start = clamp_bot(0, stream->start); + stream->end = clamp_top(stream->end, stream->token_count); buffer_read_tokens(app, buffer_id, stream->start, stream->end, stream->base_tokens); stream->tokens = stream->base_tokens - stream->start; diff --git a/4coder_seek.cpp b/4coder_seek.cpp index 9cef143a..b29bf9b2 100644 --- a/4coder_seek.cpp +++ b/4coder_seek.cpp @@ -17,164 +17,68 @@ buffer_seek_delimiter_backward(Application_Links *app, Buffer_ID buffer, i32 pos } static void -buffer_seek_string_forward(Application_Links *app, Buffer_ID buffer_id, i32 pos, i32 end, String_Const_u8 needle_string, i32 *result){ - i32 buffer_size = 0; - buffer_get_size(app, buffer_id, &buffer_size); - if (buffer_size > end){ - *result = buffer_size; +buffer_seek_string_forward(Application_Links *app, Buffer_ID buffer, i32 pos, i32 end, String_Const_u8 needle, i32 *result){ + if (end == 0){ + buffer_get_size(app, buffer, &end); + } + b32 case_sensitive = false; + b32 finding_matches = false; + do{ + finding_matches = + buffer_seek_string(app, buffer, needle, Scan_Forward, pos, &pos, &case_sensitive); + } while(!case_sensitive && pos < end && finding_matches); + if (pos < end && finding_matches){ + *result = pos; } else{ - *result = end; - } - - Scratch_Block scratch(app); - if (0 < needle_string.size){ - if (buffer_exists(app, buffer_id)){ - u8 first_char = string_get_character(needle_string, 0); - - u8 *read_buffer = push_array(scratch, u8, needle_string.size); - String_Const_u8 read_str = SCu8(read_buffer, needle_string.size); - - char chunk[1024]; - Stream_Chunk stream = {}; - stream.max_end = end; - - if (init_stream_chunk(&stream, app, buffer_id, pos, chunk, sizeof(chunk))){ - b32 still_looping = true; - do{ - for(; pos < stream.end; ++pos){ - u8 at_pos = stream.data[pos]; - if (at_pos == first_char){ - buffer_read_range(app, buffer_id, pos, (i32)(pos + needle_string.size), (char*)read_buffer); - if (string_match(needle_string, read_str)){ - *result = pos; - goto finished; - } - } - } - still_looping = forward_stream_chunk(&stream); - }while (still_looping); - } - } - - if (end == 0){ - *result = buffer_size; - } - else{ - *result = end; - } - - finished:; + buffer_get_size(app, buffer, result); } } static void -buffer_seek_string_backward(Application_Links *app, Buffer_ID buffer_id, i32 pos, i32 min, String_Const_u8 needle_string, i32 *result){ - Scratch_Block scratch(app); - *result = min - 1; - if (0 < needle_string.size && buffer_exists(app, buffer_id)){ - u8 first_char = string_get_character(needle_string, 0); - - u8 *read_buffer = push_array(scratch, u8, needle_string.size); - String_Const_u8 read_str = SCu8(read_buffer, needle_string.size); - - char chunk[1024]; - Stream_Chunk stream = {}; - stream.min_start = min; - - if (init_stream_chunk(&stream, app, buffer_id, pos, chunk, sizeof(chunk))){ - b32 still_looping = true; - do{ - for(; pos >= stream.start; --pos){ - u8 at_pos = stream.data[pos]; - if (at_pos == first_char){ - buffer_read_range(app, buffer_id, pos, (i32)(pos + needle_string.size), (char*)read_buffer); - if (string_match(needle_string, read_str)){ - *result = pos; - goto finished; - } - } - } - still_looping = backward_stream_chunk(&stream); - }while (still_looping); - } - finished:; - } -} - -static void -buffer_seek_string_insensitive_forward(Application_Links *app, Buffer_ID buffer_id, i32 pos, i32 end, String_Const_u8 needle_string, i32 *result){ - i32 buffer_size = 0; - buffer_get_size(app, buffer_id, &buffer_size); - if (buffer_size > end){ - *result = buffer_size; +buffer_seek_string_backward(Application_Links *app, Buffer_ID buffer, i32 pos, i32 min, String_Const_u8 needle, i32 *result){ + b32 case_sensitive = false; + b32 finding_matches = false; + do{ + finding_matches = + buffer_seek_string(app, buffer, needle, Scan_Backward, pos, &pos, &case_sensitive); + } while(!case_sensitive && pos >= min && finding_matches); + if (pos >= min && finding_matches){ + *result = pos; } else{ - *result = end; - } - - Scratch_Block scratch(app); - if (0 < needle_string.size && buffer_exists(app, buffer_id)){ - u8 first_char = character_to_upper(string_get_character(needle_string, 0)); - - u8 *read_buffer = push_array(scratch, u8, needle_string.size); - String_Const_u8 read_str = SCu8(read_buffer, needle_string.size); - - char chunk[1024]; - Stream_Chunk stream = {}; - stream.max_end = end; - - if (init_stream_chunk(&stream, app, buffer_id, pos, chunk, sizeof(chunk))){ - b32 still_looping = true; - do{ - for(; pos < stream.end; ++pos){ - u8 at_pos = character_to_upper(stream.data[pos]); - if (at_pos == first_char){ - buffer_read_range(app, buffer_id, pos, (i32)(pos + needle_string.size), (char*)read_buffer); - if (string_match_insensitive(needle_string, read_str)){ - *result = pos; - goto finished; - } - } - } - still_looping = forward_stream_chunk(&stream); - }while (still_looping); - } - finished:; + *result = -1; } } static void -buffer_seek_string_insensitive_backward(Application_Links *app, Buffer_ID buffer_id, i32 pos, i32 min, String_Const_u8 needle_string, i32 *result){ - Scratch_Block scratch(app); - *result = min - 1; - if (0 < needle_string.size && buffer_exists(app, buffer_id)){ - u8 first_char = character_to_upper(string_get_character(needle_string, 0)); - - u8 *read_buffer = push_array(scratch, u8, needle_string.size); - String_Const_u8 read_str = SCu8(read_buffer, needle_string.size); - - char chunk[1024]; - Stream_Chunk stream = {}; - stream.min_start = min; - - if (init_stream_chunk(&stream, app, buffer_id, pos, chunk, sizeof(chunk))){ - b32 still_looping = true; - do{ - for(; pos >= stream.start; --pos){ - u8 at_pos = character_to_upper(stream.data[pos]); - if (at_pos == first_char){ - buffer_read_range(app, buffer_id, pos, (i32)(pos + needle_string.size), (char*)read_buffer); - if (string_match_insensitive(needle_string, read_str)){ - *result = pos; - goto finished; - } - } - } - still_looping = backward_stream_chunk(&stream); - }while (still_looping); - } - finished:; +buffer_seek_string_insensitive_forward(Application_Links *app, Buffer_ID buffer, i32 pos, i32 end, String_Const_u8 needle, i32 *result){ + if (end == 0){ + buffer_get_size(app, buffer, &end); + } + b32 finding_matches = false; + b32 case_sensitive = false; + finding_matches = + buffer_seek_string(app, buffer, needle, Scan_Forward, pos, &pos, &case_sensitive); + if (pos < end && finding_matches){ + *result = pos; + } + else{ + buffer_get_size(app, buffer, result); + } +} + +static void +buffer_seek_string_insensitive_backward(Application_Links *app, Buffer_ID buffer, i32 pos, i32 min, String_Const_u8 needle, i32 *result){ + b32 finding_matches = false; + b32 case_sensitive = false; + finding_matches = + buffer_seek_string(app, buffer, needle, Scan_Backward, pos, &pos, &case_sensitive); + if (pos >= min && finding_matches){ + *result = pos; + } + else{ + *result = -1; } } diff --git a/4ed_api_implementation.cpp b/4ed_api_implementation.cpp index dd4c0837..b2e58e3f 100644 --- a/4ed_api_implementation.cpp +++ b/4ed_api_implementation.cpp @@ -420,6 +420,132 @@ DOC_SEE(Buffer_Batch_Edit_Type) return(result); } +internal b32 +chunked_match(String_Const_u8_Array chunks, String_Const_u8 needle, + i32 chunk_index, i32 chunk_pos, b32 *case_sensitive_out){ + b32 result = false; + if (chunk_pos + (imem)(needle.size) <= (imem)(chunks.vals[chunk_index].size)){ + String_Const_u8 s = SCu8(chunks.vals[chunk_index].str + chunk_pos, needle.size); + if (string_match(needle, s)){ + *case_sensitive_out = true; + result = true; + } + else if (string_match_insensitive(needle, s)){ + *case_sensitive_out = false; + result = true; + } + } + else{ + String_Const_u8 c = chunks.vals[chunk_index]; + u8 *str = c.str; + String_Const_u8 s = SCu8(str + chunk_pos, str + c.size); + String_Const_u8 p = string_prefix(needle, s.size); + if (string_match(p, s)){ + result = chunked_match(chunks, string_skip(needle, s.size), + chunk_index + 1, 0, case_sensitive_out); + } + else if (string_match_insensitive(p, s)){ + result = chunked_match(chunks, string_skip(needle, s.size), + chunk_index + 1, 0, case_sensitive_out); + *case_sensitive_out = false; + } + } + return(result); +} + +API_EXPORT b32 +Buffer_Seek_String(Application_Links *app, Buffer_ID buffer, String_Const_u8 needle, Scan_Direction direction, i32 start_pos, i32 *pos_out, b32 *case_sensitive_out){ + Models *models = (Models*)app->cmd_context; + Editing_File *file = imp_get_file(models, buffer); + b32 result = false; + if (api_check_buffer(file)){ + if (needle.size == 0){ + *pos_out = start_pos; + result = true; + } + else{ + Gap_Buffer *gap_buffer = &file->state.buffer; + i32 size = buffer_size(gap_buffer); + if (size >= (imem)needle.size){ + u8 first_character = character_to_upper(needle.str[0]); + + i32 last_point = size - (i32)(needle.size) + 1; + String_Const_u8 chunks_space[2]; + Cursor chunks_cursor = make_cursor(chunks_space, sizeof(chunks_space)); + String_Const_u8_Array chunks = buffer_get_chunks(&chunks_cursor, gap_buffer, BufferGetChunk_Basic); + start_pos = clamp(-1, start_pos, last_point + 1); + Buffer_Chunk_Position pos = buffer_get_chunk_position(chunks, size, start_pos); + if (direction == Scan_Forward){ + for (; pos.chunk_index < chunks.count; pos.chunk_index += 1, pos.chunk_pos = 0){ + for (;;){ + pos.real_pos += 1; + if (pos.real_pos > last_point){ + goto done_forward; + } + pos.chunk_pos += 1; + if (pos.chunk_pos >= chunks.vals[pos.chunk_index].size){ + break; + } + + // TODO(allen): "point skip array" for speedup + u8 v = chunks.vals[pos.chunk_index].str[pos.chunk_pos]; + if (character_to_upper(v) == first_character){ + if (chunked_match(chunks, needle, pos.chunk_index, pos.chunk_pos, case_sensitive_out)){ + *pos_out = pos.real_pos; + result = true; + goto done_forward; + } + } + } + } + done_forward:; + if (!result){ + *pos_out = size; + } + } + else{ + for (;;){ + for (;;){ + pos.real_pos -= 1; + if (pos.real_pos < 0){ + goto done_backward; + } + pos.chunk_pos -= 1; + if (pos.chunk_pos < 0){ + break; + } + // TODO(allen): "point skip array" for speedup + u8 v = chunks.vals[pos.chunk_index].str[pos.chunk_pos]; + if (character_to_upper(v) == first_character){ + if (chunked_match(chunks, needle, pos.chunk_index, pos.chunk_pos, case_sensitive_out)){ + *pos_out = pos.real_pos; + result = true; + goto done_forward; + } + } + } + pos.chunk_index -= 1; + if (pos.chunk_index > 0){ + pos.chunk_pos = (i32)chunks.vals[pos.chunk_index].size - 1; + } + else{ + break; + } + } + done_backward:; + if (!result){ + *pos_out = 0; + } + } + } + else{ + *pos_out = 0; + } + } + } + return(result); +} + #define character_predicate_check_character(p, c) (((p).b[(c)/8] & (1 << ((c)%8))) != 0) API_EXPORT b32 @@ -433,50 +559,25 @@ Buffer_Seek_Character_Class(Application_Links *app, Buffer_ID buffer_id, Charact Gap_Buffer *gap_buffer = &file->state.buffer; String_Const_u8_Array chunks = buffer_get_chunks(&chunks_cursor, gap_buffer, BufferGetChunk_Basic); - i32 size = buffer_size(gap_buffer); - start_pos = clamp(-1, start_pos, size); if (chunks.count > 0){ - i32 real_pos = start_pos; - i32 chunk_index = 0; - i32 chunk_pos = start_pos; - if (start_pos != size){ - for (;(imem)(chunks.vals[chunk_index].size) <= chunk_pos;){ - Assert(chunk_index < chunks.count); - chunk_pos -= (i32)chunks.vals[chunk_index].size; - chunk_index += 1; - } - } - else{ - chunk_index = chunks.count - 1; - chunk_pos = (i32)chunks.vals[chunk_index].size; - } + i32 size = buffer_size(gap_buffer); + start_pos = clamp(-1, start_pos, size); + Buffer_Chunk_Position pos = buffer_get_chunk_position(chunks, size, start_pos); + // TODO(allen): rewrite as loop-in-loop for better iteration performance for (;;){ - real_pos += direction; - chunk_pos += direction; - if (chunk_pos < 0){ - if (chunk_index == 0){ - *pos_out = 0; - break; - } - else{ - chunk_index -= 1; - chunk_pos = (i32)chunks.vals[chunk_index].size - 1; - } + i32 past_end = buffer_chunk_position_iterate(chunks, &pos, direction); + if (past_end == -1){ + *pos_out = 0; + break; } - else if (chunk_pos >= (imem)(chunks.vals[chunk_index].size)){ - chunk_index += 1; - if (chunk_index == chunks.count){ - *pos_out = size; - break; - } - else{ - chunk_pos = 0; - } + else if (past_end == 1){ + *pos_out = size; + break; } - u8 v = chunks.vals[chunk_index].str[chunk_pos]; + u8 v = chunks.vals[pos.chunk_index].str[pos.chunk_pos]; if (character_predicate_check_character(*predicate, v)){ result = true; - *pos_out = real_pos; + *pos_out = pos.real_pos; break; } } diff --git a/4ed_buffer.cpp b/4ed_buffer.cpp index 2e1c43b0..8793b391 100644 --- a/4ed_buffer.cpp +++ b/4ed_buffer.cpp @@ -1869,5 +1869,50 @@ buffer_render_data(Buffer_Render_State *S_ptr, Buffer_Render_Params params, f32 #undef DrReturn #undef DrCase +internal Buffer_Chunk_Position +buffer_get_chunk_position(String_Const_u8_Array chunks, i32 buffer_size, i32 real_pos){ + Buffer_Chunk_Position pos = {}; + pos.real_pos = real_pos; + pos.chunk_pos = real_pos; + if (pos.real_pos != buffer_size){ + for (;(imem)(chunks.vals[pos.chunk_index].size) <= pos.chunk_pos;){ + Assert(pos.chunk_index < chunks.count); + pos.chunk_pos -= (i32)chunks.vals[pos.chunk_index].size; + pos.chunk_index += 1; + } + } + else{ + pos.chunk_index = chunks.count - 1; + pos.chunk_pos = (i32)chunks.vals[pos.chunk_index].size; + } + return(pos); +} + +internal i32 +buffer_chunk_position_iterate(String_Const_u8_Array chunks, Buffer_Chunk_Position *pos, Scan_Direction direction){ + i32 past_end = 0; + pos->real_pos += direction; + pos->chunk_pos += direction; + if (pos->chunk_pos < 0){ + if (pos->chunk_index == 0){ + past_end = -1; + } + else{ + pos->chunk_index -= 1; + pos->chunk_pos = (i32)chunks.vals[pos->chunk_index].size - 1; + } + } + else if (pos->chunk_pos >= (imem)(chunks.vals[pos->chunk_index].size)){ + pos->chunk_index += 1; + if (pos->chunk_index == chunks.count){ + past_end = 1; + } + else{ + pos->chunk_pos = 0; + } + } + return(past_end); +} + // BOTTOM diff --git a/4ed_buffer.h b/4ed_buffer.h index db7003dc..fd0ef537 100644 --- a/4ed_buffer.h +++ b/4ed_buffer.h @@ -53,6 +53,12 @@ struct Gap_Buffer_Stream{ }; global Gap_Buffer_Stream null_buffer_stream = {}; +struct Buffer_Chunk_Position{ + i32 real_pos; + i32 chunk_pos; + i32 chunk_index; +}; + struct Buffer_Batch_State{ i32 i; i32 shift_total;