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

This commit is contained in:
Sean Barrett 2019-02-07 07:28:41 -08:00
commit 5428c40870

View File

@ -86,8 +86,8 @@
// moderate sizes. The undo system does no memory allocations, so // moderate sizes. The undo system does no memory allocations, so
// it grows STB_TexteditState by the worst-case storage which is (in bytes): // it grows STB_TexteditState by the worst-case storage which is (in bytes):
// //
// [4 + sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT // [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT
// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT // + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT
// //
// //
// Implementation mode: // Implementation mode:
@ -110,7 +110,7 @@
// Symbols that must be the same in header-file and implementation mode: // Symbols that must be the same in header-file and implementation mode:
// //
// STB_TEXTEDIT_CHARTYPE the character type // STB_TEXTEDIT_CHARTYPE the character type
// STB_TEXTEDIT_POSITIONTYPE small type that a valid cursor position // STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position
// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow
// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer
// //
@ -295,9 +295,9 @@ typedef struct
{ {
// private data // private data
STB_TEXTEDIT_POSITIONTYPE where; STB_TEXTEDIT_POSITIONTYPE where;
short insert_length; STB_TEXTEDIT_POSITIONTYPE insert_length;
short delete_length; STB_TEXTEDIT_POSITIONTYPE delete_length;
short char_storage; int char_storage;
} StbUndoRecord; } StbUndoRecord;
typedef struct typedef struct
@ -306,7 +306,7 @@ typedef struct
StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]; StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];
STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]; STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];
short undo_point, redo_point; short undo_point, redo_point;
short undo_char_point, redo_char_point; int undo_char_point, redo_char_point;
} StbUndoState; } StbUndoState;
typedef struct typedef struct
@ -1095,11 +1095,11 @@ static void stb_textedit_discard_undo(StbUndoState *state)
if (state->undo_rec[0].char_storage >= 0) { if (state->undo_rec[0].char_storage >= 0) {
int n = state->undo_rec[0].insert_length, i; int n = state->undo_rec[0].insert_length, i;
// delete n characters from all other records // delete n characters from all other records
state->undo_char_point = state->undo_char_point - (short) n; // vsnet05 state->undo_char_point -= n;
STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE))); STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));
for (i=0; i < state->undo_point; ++i) for (i=0; i < state->undo_point; ++i)
if (state->undo_rec[i].char_storage >= 0) if (state->undo_rec[i].char_storage >= 0)
state->undo_rec[i].char_storage = state->undo_rec[i].char_storage - (short) n; // vsnet05 // @OPTIMIZE: get rid of char_storage and infer it state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it
} }
--state->undo_point; --state->undo_point;
STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));
@ -1119,12 +1119,12 @@ static void stb_textedit_discard_redo(StbUndoState *state)
if (state->undo_rec[k].char_storage >= 0) { if (state->undo_rec[k].char_storage >= 0) {
int n = state->undo_rec[k].insert_length, i; int n = state->undo_rec[k].insert_length, i;
// move the remaining redo character data to the end of the buffer // move the remaining redo character data to the end of the buffer
state->redo_char_point = state->redo_char_point + (short) n; // vsnet05 state->redo_char_point += n;
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))); 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)));
// adjust the position of all the other records to account for above memmove // adjust the position of all the other records to account for above memmove
for (i=state->redo_point; i < k; ++i) for (i=state->redo_point; i < k; ++i)
if (state->undo_rec[i].char_storage >= 0) if (state->undo_rec[i].char_storage >= 0)
state->undo_rec[i].char_storage += (short) n; // vsnet05 state->undo_rec[i].char_storage += n;
} }
// now move all the redo records towards the end of the buffer; the first one is at 'redo_point' // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'
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]))); 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])));
@ -1164,15 +1164,15 @@ static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos,
return NULL; return NULL;
r->where = pos; r->where = pos;
r->insert_length = (short) insert_len; r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;
r->delete_length = (short) delete_len; r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;
if (insert_len == 0) { if (insert_len == 0) {
r->char_storage = -1; r->char_storage = -1;
return NULL; return NULL;
} else { } else {
r->char_storage = state->undo_char_point; r->char_storage = state->undo_char_point;
state->undo_char_point = state->undo_char_point + (short) insert_len; state->undo_char_point += insert_len;
return &state->undo_char[r->char_storage]; return &state->undo_char[r->char_storage];
} }
} }
@ -1221,7 +1221,7 @@ static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
r = &s->undo_rec[s->redo_point-1]; r = &s->undo_rec[s->redo_point-1];
r->char_storage = s->redo_char_point - u.delete_length; r->char_storage = s->redo_char_point - u.delete_length;
s->redo_char_point = s->redo_char_point - (short) u.delete_length; s->redo_char_point = s->redo_char_point - u.delete_length;
// now save the characters // now save the characters
for (i=0; i < u.delete_length; ++i) for (i=0; i < u.delete_length; ++i)