Browse Source

InputText: minor tidying up of selection search loop (no need to imply it runs in single line mode)

pull/2739/merge
ocornut 2 months ago
parent
commit
aef07aea27
  1. 23
      imgui_widgets.cpp

23
imgui_widgets.cpp

@ -5100,33 +5100,30 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{ {
// Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions. // Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions.
const char* searches_input_ptr[2] = { NULL, NULL };
int searches_result_line_no[2] = { -1000, -1000 }; int searches_result_line_no[2] = { -1000, -1000 };
int searches_remaining = 0; const char* searches_input_ptr[2] = { NULL, NULL };
if (render_cursor) if (render_cursor)
{ {
searches_input_ptr[0] = text_begin + state->Stb->cursor; searches_input_ptr[0] = text_begin + state->Stb->cursor;
searches_result_line_no[0] = -1; searches_result_line_no[0] = -1;
searches_remaining++;
} }
if (render_selection) if (render_selection)
{ {
searches_input_ptr[1] = text_begin + ImMin(state->Stb->select_start, state->Stb->select_end); searches_input_ptr[1] = text_begin + ImMin(state->Stb->select_start, state->Stb->select_end);
searches_result_line_no[1] = -1; searches_result_line_no[1] = -1;
searches_remaining++;
} }
// Iterate all lines to find our line numbers // Count lines and find line number for cursor and selection ends
// In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter. int line_count = 1;
searches_remaining += is_multiline ? 1 : 0; if (is_multiline)
int line_count = 0;
for (const char* s = text_begin; (s = (const char*)memchr(s, '\n', (size_t)(text_end - s))) != NULL; s++)
{ {
line_count++; for (const char* s = text_begin; (s = (const char*)memchr(s, '\n', (size_t)(text_end - s))) != NULL; s++)
if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; if (--searches_remaining <= 0) break; } {
if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; if (--searches_remaining <= 0) break; } if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; }
if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; }
line_count++;
}
} }
line_count++;
if (searches_result_line_no[0] == -1) if (searches_result_line_no[0] == -1)
searches_result_line_no[0] = line_count; searches_result_line_no[0] = line_count;
if (searches_result_line_no[1] == -1) if (searches_result_line_no[1] == -1)

Loading…
Cancel
Save