optimization of row operations

master
Alessandro Mauri 5 years ago
parent 814aa89262
commit f09a9e807d
  1. 102
      ste.c

102
ste.c

@ -75,7 +75,7 @@ static void rowDeleteChar (row *rw, int select, int pos);
static void rowCpy (row *to, row *from); static void rowCpy (row *to, row *from);
static void rowAddRow (int pos); static void rowAddRow (int pos);
static void rowFree (row *rw); static void rowFree (row *rw);
static void rowAddString (row *rw, char *s, int len, int pos); static void rowAppendString (row *rw, char *s, int len);
static void rowDeleteRow (int pos); static void rowDeleteRow (int pos);
/* Terminal operations */ /* Terminal operations */
@ -160,6 +160,8 @@ int main (int argc, char *argv[])
default: default:
if (c == KEY_STAB) c = '\t'; if (c == KEY_STAB) c = '\t';
rowAddChar(&rows.rw[t.cur.y], c, t.cur.x); rowAddChar(&rows.rw[t.cur.y], c, t.cur.x);
t.cur.x++;
break;
} }
if (decimalSize(rows.rownum) - irow) updateInfo(); if (decimalSize(rows.rownum) - irow) updateInfo();
} }
@ -404,52 +406,40 @@ void rowInit (void)
rows.rownum = 0; rows.rownum = 0;
} }
void rowAddChar (row *rw, char c, int pos) // WIP void rowAddChar (row *rw, char c, int pos)
{ {
// Error checking (allow tab) /* Check if char is valid */
if (!c || (iscntrl(c) && c != '\t')) return; if (!c || (iscntrl(c) && c != '\t')) return;
int i = 0; /* extend the string */
char *s = rw->chars;
// reallocate mem and inc size
rw->chars = malloc(rw->size + 2);
if (rw->chars == NULL) termDie("malloc in rowAddchar");
rw->size++; rw->size++;
char *t = realloc(rw->chars, rw->size + 1);
if (t == NULL) termDie("realloc in rowAddchar");
rw->chars = t;
// copy bf cursor /* make space for the new char */
for (i = 0; i < pos; i++) { memcpy(&rw->chars[pos + 1], &rw->chars[pos], (rw->size + 1) - (pos + 1));
rw->chars[i] = s[i];
}
// add at cursor
rw->chars[pos++] = c;
//copy after cursor /* add the new char */
for (i = pos; i < rw->size + 1; i++) { rw->chars[pos] = c;
rw->chars[i] = s[i - 1];
}
free(s);
updateRender(rw); updateRender(rw);
t.cur.x++;
} }
void rowDeleteChar (row *rw, int select, int pos) // WIP void rowDeleteChar (row *rw, int select, int pos) // WIP
{ {
//Do not delete NULL char /* Check if the character is valid */
if (rw->chars[pos - 1] == '\0' && pos) return; if (rw->chars[pos - 1] == '\0' && pos) return;
if (!pos && !select) return; if (!pos && !select) return;
if (rw->chars[pos] == '\0' && select) return; if (rw->chars[pos] == '\0' && select) return;
// Backspace /* delete char before the position */
if (!select) { if (!select) {
for (int i = pos; i < rw->size + 1; i++) for (int i = pos; i < rw->size + 1; i++)
rw->chars[i - 1] = rw->chars[i]; rw->chars[i - 1] = rw->chars[i];
t.cur.x--; t.cur.x--;
// Delete /* delete char at position */
} else { } else {
for (int i = pos; i < rw->size + 1; i++) for (int i = pos; i < rw->size + 1; i++)
@ -533,21 +523,17 @@ void rowCpy (row *to, row *from) // WIP
updateRender(to); updateRender(to);
} }
void rowAddString (row *rw, char *s, int len, int pos) void rowAppendString (row *rw, char *s, int len)
{ {
/* reallocate the row to accomodate for the added string */
char *temp = realloc(rw->chars, rw->size + len + 1); char *temp = realloc(rw->chars, rw->size + len + 1);
if (temp == NULL) termDie("realloc in rowAddString"); if (temp == NULL) termDie("realloc in rowAppendString");
rw->chars = temp; rw->chars = temp;
if (pos == -1 || pos == rw->size) {
memcpy(&rw->chars[rw->size], s, len); memcpy(&rw->chars[rw->size], s, len);
rw->size += len; rw->size += len;
rw->chars[rw->size] = '\0'; rw->chars[rw->size] = '\0';
} else {
memcpy(&rw->chars[rw->size], &rw->chars[rw->size - len], len);
memcpy(&rw->chars[rw->size - len], s, len);
rw->size += len;
rw->chars[rw->size] = '\0';
}
updateRender(rw); updateRender(rw);
} }
@ -617,7 +603,40 @@ void cursorMove (int a)
void curUpdateRender () void curUpdateRender ()
{ {
// y /*
Whole file (memory)
_____________________________
|(0, 0) |
| |
| off_y |
| +--------------+ |
| | | |
| | | |
| | | off_x |
| off_x| | + |
| | | dim_x |
| | | |
| | | |
| | | |
| +--------------+ |
| off_y + dim.y |
| |
| |
| |
|_____________________________| rows.rownum
The inner sqaure represents the render area
and it is delimited by:
left: x -> t.cur.off_x
right: x -> t.cur.off_x + (t.dim.x - 1)
upper: y -> t.cur.off_y
lower: y -> t.cur.off_y + (t.dim.y - 1)
The rows are drawn top to botom, starting
from the left most char of the render area
until row end or when the characters hit
the right bound.
*/
if (t.cur.y >= t.cur.off_y && t.cur.y < t.cur.off_y + t.dim.y) { if (t.cur.y >= t.cur.off_y && t.cur.y < t.cur.off_y + t.dim.y) {
t.cur.r_y = t.cur.y - t.cur.off_y; t.cur.r_y = t.cur.y - t.cur.off_y;
@ -686,7 +705,7 @@ void handleDel (int select)
if (!select) { if (!select) {
if (t.cur.x <= 0 && t.cur.y > 0) { if (t.cur.x <= 0 && t.cur.y > 0) {
t.cur.x = rows.rw[t.cur.y - 1].size; t.cur.x = rows.rw[t.cur.y - 1].size;
rowAddString(&rows.rw[t.cur.y - 1], rows.rw[t.cur.y].chars, rows.rw[t.cur.y].size, -1); rowAppendString(&rows.rw[t.cur.y - 1], rows.rw[t.cur.y].chars, rows.rw[t.cur.y].size);
rowDeleteRow(t.cur.y); rowDeleteRow(t.cur.y);
t.cur.y--; t.cur.y--;
} else { } else {
@ -694,7 +713,7 @@ void handleDel (int select)
} }
} else { } else {
if (t.cur.x >= rows.rw[t.cur.y].size) { if (t.cur.x >= rows.rw[t.cur.y].size) {
rowAddString(&rows.rw[t.cur.y], rows.rw[t.cur.y + 1].chars, rows.rw[t.cur.y + 1].size, -1); rowAppendString(&rows.rw[t.cur.y], rows.rw[t.cur.y + 1].chars, rows.rw[t.cur.y + 1].size);
rowDeleteRow(t.cur.y + 1); rowDeleteRow(t.cur.y + 1);
} else { } else {
rowDeleteChar(&rows.rw[t.cur.y], 1, t.cur.x); rowDeleteChar(&rows.rw[t.cur.y], 1, t.cur.x);
@ -710,6 +729,7 @@ void updateInfo (void)
t.dim.x -= t.pad + 1; t.dim.x -= t.pad + 1;
} }
/* Check for utf-8 char type */
int isUtf (int c) { int isUtf (int c) {
return (c >= 0x80 || c < 0 ? 1 : 0); return (c >= 0x80 || c < 0 ? 1 : 0);
} }

Loading…
Cancel
Save