Simplify SavepointBuffer implementation
This commit is contained in:
parent
91a68f0089
commit
46ec0d4e9c
@ -235,8 +235,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Savepoint buf[SAVEPOINTS_SZ];
|
Savepoint buf[SAVEPOINTS_SZ];
|
||||||
Savepoint *start_of_data;
|
index_type start_of_data, count;
|
||||||
Savepoint *end_of_data;
|
|
||||||
} SavepointBuffer;
|
} SavepointBuffer;
|
||||||
|
|
||||||
#define PARSER_BUF_SZ 8192
|
#define PARSER_BUF_SZ 8192
|
||||||
@ -296,7 +295,6 @@ uint16_t* translation_table(char);
|
|||||||
uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte);
|
uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte);
|
||||||
Savepoint* savepoints_pop(SavepointBuffer *);
|
Savepoint* savepoints_pop(SavepointBuffer *);
|
||||||
Savepoint* savepoints_push(SavepointBuffer *);
|
Savepoint* savepoints_push(SavepointBuffer *);
|
||||||
void savepoints_init(SavepointBuffer *);
|
|
||||||
void cursor_reset(Cursor*);
|
void cursor_reset(Cursor*);
|
||||||
Cursor* cursor_copy(Cursor*);
|
Cursor* cursor_copy(Cursor*);
|
||||||
void cursor_copy_to(Cursor *src, Cursor *dest);
|
void cursor_copy_to(Cursor *src, Cursor *dest);
|
||||||
|
|||||||
@ -7,26 +7,15 @@
|
|||||||
|
|
||||||
#include "data-types.h"
|
#include "data-types.h"
|
||||||
|
|
||||||
#define ADVANCE(x) \
|
|
||||||
self->x = (self->x >= self->buf + SAVEPOINTS_SZ - 1) ? self->buf : self->x + 1;
|
|
||||||
|
|
||||||
#define RETREAT(x) \
|
|
||||||
self->x = self->x == self->buf ? self->buf + SAVEPOINTS_SZ - 1 : self->x - 1;
|
|
||||||
|
|
||||||
Savepoint* savepoints_push(SavepointBuffer *self) {
|
Savepoint* savepoints_push(SavepointBuffer *self) {
|
||||||
Savepoint *ans = self->end_of_data;
|
Savepoint *ans = self->buf + ((self->start_of_data + self->count) % SAVEPOINTS_SZ);
|
||||||
ADVANCE(end_of_data);
|
if (self->count == SAVEPOINTS_SZ) self->start_of_data = (self->start_of_data + 1) % SAVEPOINTS_SZ;
|
||||||
if (self->end_of_data == self->start_of_data) ADVANCE(start_of_data);
|
else self->count++;
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
Savepoint* savepoints_pop(SavepointBuffer *self) {
|
Savepoint* savepoints_pop(SavepointBuffer *self) {
|
||||||
if (self->start_of_data == self->end_of_data) return NULL;
|
if (self->count == 0) return NULL;
|
||||||
RETREAT(end_of_data);
|
self->count--;
|
||||||
return self->end_of_data;
|
return self->buf + ((self->start_of_data + self->count) % SAVEPOINTS_SZ);
|
||||||
}
|
|
||||||
|
|
||||||
void savepoints_init(SavepointBuffer *self) {
|
|
||||||
self->end_of_data = self->buf;
|
|
||||||
self->start_of_data = self->buf;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,7 +39,6 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
|
|||||||
if (self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || self->change_tracker == NULL || self->tabstops == NULL) {
|
if (self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || self->change_tracker == NULL || self->tabstops == NULL) {
|
||||||
Py_CLEAR(self); return NULL;
|
Py_CLEAR(self); return NULL;
|
||||||
}
|
}
|
||||||
savepoints_init(&self->main_savepoints); savepoints_init(&self->alt_savepoints);
|
|
||||||
}
|
}
|
||||||
return (PyObject*) self;
|
return (PyObject*) self;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user