Skip to content
Myr

stb_textedit: fixed undo/redo after pasting large amount of text (over 32 kb). Redo will still fail when undo buffers are exhausted, but text won't be corrupted. Fix #620.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Omar · committed by Omar · parent e6afb9c · Browse files · 1 file+15 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−15 removed

  • other:+15 added−15 removed

1 file+15 added−15 removed

modified:stb_textedit.h+15 added-15 removed
84 unmodified lines
85// If you don't define these, they are set to permissive types and86// moderate sizes. The undo system does no memory allocations, so87// it grows STB_TexteditState by the worst-case storage which is (in bytes):88//89// [4 + sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT90// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT89// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT90// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT91//92//93// Implementation mode:94//
14 unmodified lines
109//110// Symbols that must be the same in header-file and implementation mode:111//112// STB_TEXTEDIT_CHARTYPE the character type113// STB_TEXTEDIT_POSITIONTYPE small type that a valid cursor position113// STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position114// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow115// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer116//117// Symbols you must define for implementation mode:
176 unmodified lines
294typedef struct295{296 // private data297 STB_TEXTEDIT_POSITIONTYPE where;298 short insert_length;299 short delete_length;300 short char_storage;298 STB_TEXTEDIT_POSITIONTYPE insert_length;299 STB_TEXTEDIT_POSITIONTYPE delete_length;300 int char_storage;301} StbUndoRecord;302303typedef struct304{305 // private data306 StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];307 STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];308 short undo_point, redo_point;309 short undo_char_point, redo_char_point;309 int undo_char_point, redo_char_point;310} StbUndoState;311312typedef struct313{
781 unmodified lines
1095 // if the 0th undo state has characters, clean those up1096 if (state->undo_rec[0].char_storage >= 0) {1097 int n = state->undo_rec[0].insert_length, i;1098 // delete n characters from all other records1099 state->undo_char_point = state->undo_char_point - (short) n; // vsnet051099 state->undo_char_point -= n;1100 STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));1101 for (i=0; i < state->undo_point; ++i)1102 if (state->undo_rec[i].char_storage >= 0)1103 state->undo_rec[i].char_storage = state->undo_rec[i].char_storage - (short) n; // vsnet05 // @OPTIMIZE: get rid of char_storage and infer it1103 state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it1104 }1105 --state->undo_point;1106 STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));1107 }
11 unmodified lines
1119 // if the k'th undo state has characters, clean those up1120 if (state->undo_rec[k].char_storage >= 0) {1121 int n = state->undo_rec[k].insert_length, i;1122 // move the remaining redo character data to the end of the buffer1123 state->redo_char_point = state->redo_char_point + (short) n; // vsnet051123 state->redo_char_point += n;1124 STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)));1125 // adjust the position of all the other records to account for above memmove1126 for (i=state->redo_point; i < k; ++i)1127 if (state->undo_rec[i].char_storage >= 0)1128 state->undo_rec[i].char_storage += (short) n; // vsnet051128 state->undo_rec[i].char_storage += n;1129 }1130 // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'1131 STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, (size_t) ((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point)*sizeof(state->undo_rec[0])));1132 // now move redo_point to point to the new one
31 unmodified lines
1164 if (r == NULL)1165 return NULL;11661167 r->where = pos;1168 r->insert_length = (short) insert_len;1169 r->delete_length = (short) delete_len;1168 r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;1169 r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;11701171 if (insert_len == 0) {1172 r->char_storage = -1;1173 return NULL;1174 } else {1175 r->char_storage = state->undo_char_point;1176 state->undo_char_point = state->undo_char_point + (short) insert_len;1176 state->undo_char_point += insert_len;1177 return &state->undo_char[r->char_storage];1178 }1179}1180
40 unmodified lines
1221 }1222 r = &s->undo_rec[s->redo_point-1];12231224 r->char_storage = s->redo_char_point - u.delete_length;1225 s->redo_char_point = s->redo_char_point - (short) u.delete_length;1225 s->redo_char_point = s->redo_char_point - u.delete_length;12261227 // now save the characters1228 for (i=0; i < u.delete_length; ++i)1229 s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
175 unmodified lines