Use a null to represent a blank rather than a space

This has performance benefits when clearing (can use a single
memset). Also allows detecting trailing whitespace on lines correctly.
This commit is contained in:
Kovid Goyal 2017-09-09 10:25:03 +05:30
parent 382daacb73
commit bc97cfa024
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 79 additions and 60 deletions

View File

@ -38,7 +38,7 @@ typedef uint16_t sprite_index;
#define SGR_PROTOCOL 2
#define URXVT_PROTOCOL 3
#define BLANK_CHAR 32
#define BLANK_CHAR 0
#define CHAR_MASK 0xFFFFFF
#define ATTRS_SHIFT 24
#define ATTRS_MASK_WITHOUT_WIDTH 0xFC000000

View File

@ -19,12 +19,13 @@ clear_chars_to(LineBuf* linebuf, index_type y, char_type ch) {
clear_chars_in_line(lineptr(linebuf, y), linebuf->xnum, ch);
}
void linebuf_clear(LineBuf *self, char_type ch) {
void
linebuf_clear(LineBuf *self, char_type ch) {
memset(self->buf, 0, self->xnum * self->ynum * sizeof(Cell));
memset(self->continued_map, 0, self->ynum * sizeof(bool));
for (index_type i = 0; i < self->ynum; i++) {
clear_chars_to(self, i, ch);
self->line_map[i] = i;
for (index_type i = 0; i < self->ynum; i++) self->line_map[i] = i;
if (ch != 0) {
for (index_type i = 0; i < self->ynum; i++) clear_chars_to(self, i, ch);
}
}
@ -69,7 +70,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
self->line->xnum = xnum;
for(index_type i = 0; i < ynum; i++) {
self->line_map[i] = i;
clear_chars_to(self, i, BLANK_CHAR);
if (BLANK_CHAR != 0) clear_chars_to(self, i, BLANK_CHAR);
}
}
}
@ -151,7 +152,7 @@ allocate_line_storage(Line *line, bool initialize) {
if (initialize) {
line->cells = PyMem_Calloc(line->xnum, sizeof(Cell));
if (line->cells == NULL) { PyErr_NoMemory(); return false; }
clear_chars_in_line(line->cells, line->xnum, BLANK_CHAR);
if (BLANK_CHAR != 0) clear_chars_in_line(line->cells, line->xnum, BLANK_CHAR);
} else {
line->cells = PyMem_Malloc(line->xnum * sizeof(Cell));
if (line->cells == NULL) { PyErr_NoMemory(); return false; }
@ -199,7 +200,7 @@ copy_line_to(LineBuf *self, PyObject *args) {
static inline void
clear_line_(Line *l, index_type xnum) {
memset(l->cells, 0, xnum * sizeof(Cell));
clear_chars_in_line(l->cells, xnum, BLANK_CHAR);
if (BLANK_CHAR != 0) clear_chars_in_line(l->cells, xnum, BLANK_CHAR);
}
void linebuf_clear_line(LineBuf *self, index_type y) {

View File

@ -62,14 +62,22 @@ static PyObject *
as_unicode(Line* self) {
Py_ssize_t n = 0;
Py_UCS4 *buf = PyMem_Malloc(3 * self->xnum * sizeof(Py_UCS4));
if (buf == NULL) {
PyErr_NoMemory();
return NULL;
if (buf == NULL) return PyErr_NoMemory();
index_type xlimit = self->xnum;
if (BLANK_CHAR == 0) {
while (xlimit != 0) {
if ((self->cells[xlimit - 1].ch & CHAR_MASK) != BLANK_CHAR) break;
xlimit--;
}
for(index_type i = 0; i < self->xnum; i++) {
char_type attrs = self->cells[i].ch >> ATTRS_SHIFT;
if ((attrs & WIDTH_MASK) < 1) continue;
buf[n++] = self->cells[i].ch & CHAR_MASK;
}
char_type previous_width = 0;
for(index_type i = 0; i < xlimit; i++) {
char_type ch = self->cells[i].ch & CHAR_MASK;
if (ch == 0) {
if (previous_width == 2) { previous_width = 0; continue; };
ch = ' ';
}
buf[n++] = ch;
char_type cc = self->cells[i].cc;
Py_UCS4 cc1 = cc & CC_MASK, cc2;
if (cc1) {
@ -77,6 +85,7 @@ as_unicode(Line* self) {
cc2 = cc >> 16;
if (cc2) buf[n++] = cc2;
}
previous_width = (self->cells[i].ch >> ATTRS_SHIFT) & WIDTH_MASK;
}
PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, n);
PyMem_Free(buf);
@ -136,7 +145,7 @@ line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen) {
index_type limit = self->xnum, i=0;
int r;
if (!self->continued) { // Trim trailing spaces
if (!self->continued) { // Trim trailing blanks
for(r = self->xnum - 1; r >= 0; r--) {
if ((self->cells[r].ch & CHAR_MASK) != BLANK_CHAR) break;
}
@ -144,11 +153,15 @@ line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen) {
}
bool bold = false, italic = false, reverse = false, strike = false;
uint32_t fg = 0, bg = 0, decoration_fg = 0, decoration = 0;
char_type previous_width = 0;
WRITE_SGR(0);
for (index_type pos=0; pos < limit; pos++) {
char_type attrs = self->cells[pos].ch >> ATTRS_SHIFT, ch = self->cells[pos].ch & CHAR_MASK;
if (ch == 0 || (attrs & WIDTH_MASK) < 1) continue;
if (ch == 0) {
if (previous_width == 2) { previous_width = 0; continue; }
ch = ' ';
}
CHECK_BOOL(bold, BOLD_SHIFT, 1, 22);
CHECK_BOOL(italic, ITALIC_SHIFT, 3, 23);
CHECK_BOOL(reverse, REVERSE_SHIFT, 7, 27);
@ -175,6 +188,7 @@ line_as_ansi(Line *self, Py_UCS4 *buf, index_type buflen) {
cc1 = cc >> 16;
if (cc1) { WRITE_CH(cc1); }
}
previous_width = attrs & WIDTH_MASK;
}
return i;
#undef CHECK_BOOL
@ -305,7 +319,7 @@ cursor_from(Line* self, PyObject *args) {
void
line_clear_text(Line *self, unsigned int at, unsigned int num, int ch) {
const char_type repl = ((char_type)ch & CHAR_MASK) | (1 << ATTRS_SHIFT);
const char_type repl = ((char_type)ch & CHAR_MASK) | ( (ch ? 1 : 0) << ATTRS_SHIFT);
#define PREFIX \
for (index_type i = at; i < MIN(self->xnum, at + num); i++) { \
self->cells[i].ch = (self->cells[i].ch & ATTRS_MASK_WITHOUT_WIDTH) | repl; \
@ -321,7 +335,7 @@ line_clear_text(Line *self, unsigned int at, unsigned int num, int ch) {
static PyObject*
clear_text(Line* self, PyObject *args) {
#define clear_text_doc "clear_text(at, num, ch=' ') -> Clear characters in the specified range, preserving formatting."
#define clear_text_doc "clear_text(at, num, ch=BLANK_CHAR) -> Clear characters in the specified range, preserving formatting."
unsigned int at, num;
int ch = BLANK_CHAR;
if (!PyArg_ParseTuple(args, "II|C", &at, &num, &ch)) return NULL;
@ -369,7 +383,7 @@ void line_right_shift(Line *self, unsigned int at, unsigned int num) {
// Check if a wide character was split at the right edge
char_type w = (self->cells[self->xnum - 1].ch >> ATTRS_SHIFT) & WIDTH_MASK;
if (w != 1) {
self->cells[self->xnum - 1].ch = (1 << ATTRS_SHIFT) | BLANK_CHAR;
self->cells[self->xnum - 1].ch = ((BLANK_CHAR ? 1 : 0) << ATTRS_SHIFT) | BLANK_CHAR;
clear_sprite_position(self->cells[self->xnum - 1]);
}
}

View File

@ -58,7 +58,7 @@ rewrap_inner(BufType *src, BufType *dest, const index_type src_limit, HistoryBuf
src_line_is_continued = is_src_line_continued(src_y);
src_x_limit = src->xnum;
if (!src_line_is_continued) {
// Trim trailing white-space since there is a hard line break at the end of this line
// Trim trailing blanks since there is a hard line break at the end of this line
while(src_x_limit && (src->line->cells[src_x_limit - 1].ch & CHAR_MASK) == BLANK_CHAR) src_x_limit--;
}

View File

@ -34,7 +34,7 @@ class TestDataTypes(BaseTest):
new.copy_old(old)
self.ae(new.line(0), old.line(1))
new.clear()
self.ae(str(new.line(0)), ' ' * new.xnum)
self.ae(str(new.line(0)), '')
old.set_attribute(REVERSE, False)
for y in range(old.ynum):
for x in range(old.xnum):
@ -140,21 +140,22 @@ class TestDataTypes(BaseTest):
lb = LineBuf(2, 3)
for y in range(lb.ynum):
line = lb.line(y)
self.ae(str(line), ' ' * lb.xnum)
self.ae(str(line), '')
for x in range(lb.xnum):
self.ae(line[x], ' ')
self.ae(line[x], '\0')
with self.assertRaises(IndexError):
lb.line(lb.ynum)
with self.assertRaises(IndexError):
lb.line(0)[lb.xnum]
l = lb.line(0)
l.set_text(' ', 0, len(' '), C())
l.add_combining_char(0, '1')
self.ae(l[0], ' 1')
l.add_combining_char(0, '2')
self.ae(l[0], ' 12')
l.add_combining_char(0, '3')
self.ae(l[0], ' 13')
self.ae(l[1], ' ')
self.ae(l[1], '\0')
self.ae(str(l), ' 13')
t = 'Testing with simple text'
lb = LineBuf(2, len(t))
@ -257,7 +258,7 @@ class TestDataTypes(BaseTest):
def test_rewrap_wider(self):
' New buffer wider '
lb = create_lbuf('0123 ', '56789')
lb2 = self.line_comparison_rewrap(lb, '0123 5', '6789 ', ' ' * 6)
lb2 = self.line_comparison_rewrap(lb, '0123 5', '6789', '')
self.assertContinued(lb2, False, True)
lb = create_lbuf('12', 'abc')
@ -269,6 +270,9 @@ class TestDataTypes(BaseTest):
lb = create_lbuf('123', 'abcde')
lb2 = self.line_comparison_rewrap(lb, '123', 'abc', 'de')
self.assertContinued(lb2, False, False, True)
lb = create_lbuf('123 ', 'abcde')
lb2 = self.line_comparison_rewrap(lb, '123', ' a', 'bcd', 'e')
self.assertContinued(lb2, False, True, True, True)
@skipIf('ANCIENT_WCWIDTH' in os.environ, 'wcwidth() is too old')
def test_utils(self):

View File

@ -23,7 +23,7 @@ class TestScreen(BaseTest):
self.assertTrue(s.linebuf.is_continued(2))
self.ae(str(s.line(0)), 'a' * 5)
self.ae(str(s.line(1)), 'b' * 5)
self.ae(str(s.line(2)), 'b' * 2 + ' ' * 3)
self.ae(str(s.line(2)), 'b' * 2)
self.ae(s.cursor.x, 2), self.ae(s.cursor.y, 2)
s.draw('c' * 15)
self.ae(str(s.line(0)), 'b' * 5)
@ -203,13 +203,13 @@ class TestScreen(BaseTest):
s = self.create_screen()
s.draw('12345' * 5)
s.index()
self.ae(str(s.line(4)), ' ' * 5)
self.ae(str(s.line(4)), '')
for i in range(4):
self.ae(str(s.line(i)), '12345')
s.draw('12345' * 5)
s.cursor_up(5)
s.reverse_index()
self.ae(str(s.line(0)), ' ' * 5)
self.ae(str(s.line(0)), '')
for i in range(1, 5):
self.ae(str(s.line(i)), '12345')
@ -219,7 +219,7 @@ class TestScreen(BaseTest):
s.resize(3, 10)
self.ae(str(s.line(0)), '0'*5 + '1'*5)
self.ae(str(s.line(1)), '2'*5 + '3'*5)
self.ae(str(s.line(2)), '4'*5 + ' '*5)
self.ae(str(s.line(2)), '4'*5)
s.resize(5, 1)
self.ae(str(s.line(0)), '4')
hb = s.historybuf