Find the shortest single replacement we can make to get from old_buf to new_buf Note that this doesn't directly alter state->TextA, state->TextLen. They are expected to be made valid separately. FIXME: Ideally we should transition toward (1) making InsertChars()/DeleteChars() update undo-stack (2) discourage (and keep reconcile) or obsolete (and remove reconcile) accessing buffer directly.
| 4467 | // Note that this doesn't directly alter state->TextA, state->TextLen. They are expected to be made valid separately. |
| 4468 | // FIXME: Ideally we should transition toward (1) making InsertChars()/DeleteChars() update undo-stack (2) discourage (and keep reconcile) or obsolete (and remove reconcile) accessing buffer directly. |
| 4469 | static void InputTextReconcileUndoState(ImGuiInputTextState* state, const char* old_buf, int old_length, const char* new_buf, int new_length) |
| 4470 | { |
| 4471 | const int shorter_length = ImMin(old_length, new_length); |
| 4472 | int first_diff; |
| 4473 | for (first_diff = 0; first_diff < shorter_length; first_diff++) |
| 4474 | if (old_buf[first_diff] != new_buf[first_diff]) |
| 4475 | break; |
| 4476 | if (first_diff == old_length && first_diff == new_length) |
| 4477 | return; |
| 4478 | |
| 4479 | int old_last_diff = old_length - 1; |
| 4480 | int new_last_diff = new_length - 1; |
| 4481 | for (; old_last_diff >= first_diff && new_last_diff >= first_diff; old_last_diff--, new_last_diff--) |
| 4482 | if (old_buf[old_last_diff] != new_buf[new_last_diff]) |
| 4483 | break; |
| 4484 | |
| 4485 | const int insert_len = new_last_diff - first_diff + 1; |
| 4486 | const int delete_len = old_last_diff - first_diff + 1; |
| 4487 | if (insert_len > 0 || delete_len > 0) |
| 4488 | if (IMSTB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb->undostate, first_diff, delete_len, insert_len)) |
| 4489 | for (int i = 0; i < delete_len; i++) |
| 4490 | p[i] = old_buf[first_diff + i]; |
| 4491 | } |
| 4492 | |
| 4493 | // As InputText() retain textual data and we currently provide a path for user to not retain it (via local variables) |
| 4494 | // we need some form of hook to reapply data back to user buffer on deactivation frame. (#4714) |
no test coverage detected