Skip to content
Myr

Merge branch 'textedit-undo_char_position_type' of https://github.com/ocornut/stb into working

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

authored by Sean Barrett · committed by Sean Barrett · parents 9bb4bc1, 94e418b · 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{
780 unmodified lines
1094 // if the 0th undo state has characters, clean those up1095 if (state->undo_rec[0].char_storage >= 0) {1096 int n = state->undo_rec[0].insert_length, i;1097 // delete n characters from all other records1098 state->undo_char_point = state->undo_char_point - (short) n; // vsnet051098 state->undo_char_point -= n;1099 STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));1100 for (i=0; i < state->undo_point; ++i)1101 if (state->undo_rec[i].char_storage >= 0)1102 state->undo_rec[i].char_storage = state->undo_rec[i].char_storage - (short) n; // vsnet05 // @OPTIMIZE: get rid of char_storage and infer it1102 state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it1103 }1104 --state->undo_point;1105 STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));1106 }
11 unmodified lines
1118 // if the k'th undo state has characters, clean those up1119 if (state->undo_rec[k].char_storage >= 0) {1120 int n = state->undo_rec[k].insert_length, i;1121 // move the remaining redo character data to the end of the buffer1122 state->redo_char_point = state->redo_char_point + (short) n; // vsnet051122 state->redo_char_point += n;1123 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)));1124 // adjust the position of all the other records to account for above memmove1125 for (i=state->redo_point; i < k; ++i)1126 if (state->undo_rec[i].char_storage >= 0)1127 state->undo_rec[i].char_storage += (short) n; // vsnet051127 state->undo_rec[i].char_storage += n;1128 }1129 // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'1130 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])));1131 // now move redo_point to point to the new one
31 unmodified lines
1163 if (r == NULL)1164 return NULL;11651166 r->where = pos;1167 r->insert_length = (short) insert_len;1168 r->delete_length = (short) delete_len;1167 r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;1168 r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;11691170 if (insert_len == 0) {1171 r->char_storage = -1;1172 return NULL;1173 } else {1174 r->char_storage = state->undo_char_point;1175 state->undo_char_point = state->undo_char_point + (short) insert_len;1175 state->undo_char_point += insert_len;1176 return &state->undo_char[r->char_storage];1177 }1178}1179
40 unmodified lines
1220 }1221 r = &s->undo_rec[s->redo_point-1];12221223 r->char_storage = s->redo_char_point - u.delete_length;1224 s->redo_char_point = s->redo_char_point - (short) u.delete_length;1224 s->redo_char_point = s->redo_char_point - u.delete_length;12251226 // now save the characters1227 for (i=0; i < u.delete_length; ++i)1228 s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
175 unmodified lines