repr for Line

This commit is contained in:
Kovid Goyal 2016-11-01 18:43:55 +05:30
parent f3944e6289
commit c069d40ffe
2 changed files with 25 additions and 1 deletions

View File

@ -47,6 +47,29 @@ text_at(Line* self, PyObject *x) {
return ans; return ans;
} }
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;
}
for(index_type i = 0; i < self->xnum; i++) {
char_type ch = self->chars[i] & CHAR_MASK;
char_type cc = self->combining_chars[i];
buf[n++] = ch & CHAR_MASK;
Py_UCS4 cc1 = cc & 0xFFFF, cc2;
if (cc1) {
buf[n++] = cc1;
cc2 = cc >> 16;
if (cc2) buf[n++] = cc2;
}
}
PyObject *ans = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, n);
PyMem_Free(buf);
return ans;
}
// Boilerplate {{{ // Boilerplate {{{
static PyMethodDef methods[] = { static PyMethodDef methods[] = {
@ -66,7 +89,7 @@ PyTypeObject Line_Type = {
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_reserved */ 0, /* tp_reserved */
0, /* tp_repr */ (reprfunc)as_unicode, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */

View File

@ -17,6 +17,7 @@ def test_line_buf(self):
lb = LineBuf(2, 3) lb = LineBuf(2, 3)
for y in range(2): for y in range(2):
line = lb.line(y) line = lb.line(y)
self.ae(str(line), ' '*3)
for x in range(3): for x in range(3):
self.ae(line.text_at(x), ' ') self.ae(line.text_at(x), ' ')
with self.assertRaises(ValueError): with self.assertRaises(ValueError):