Refactor some code into a style I like

This commit is contained in:
Kovid Goyal 2018-11-21 10:54:25 +05:30
parent a91b3b9487
commit 7ed268ea2f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -314,36 +314,31 @@ pagerhist_as_text(HistoryBuf *self, PyObject *callback) {
if (!ph) Py_RETURN_NONE; if (!ph) Py_RETURN_NONE;
if (ph->rewrap_needed) pagerhist_rewrap(ph, self->xnum); if (ph->rewrap_needed) pagerhist_rewrap(ph, self->xnum);
PyObject *nl = PyUnicode_FromString("\n");
if (!nl) return NULL;
for (int i = 0; i < 3; i++) { #define CALLBACK { \
switch(i) { if (t == NULL) goto end; \
case 0: ret = PyObject_CallFunctionObjArgs(callback, t, NULL); \
num = (ph->bufend ? ph->bufend : ph->end) - ph->start; Py_DECREF(t); \
buf = ph->buffer + ph->start; if (ret == NULL) goto end; \
t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, num); Py_DECREF(ret); \
break; }
case 1:
if (!ph->bufend) continue; num = (ph->bufend ? ph->bufend : ph->end) - ph->start;
buf = ph->buffer + ph->start;
t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, num);
CALLBACK;
if (ph->bufend) {
num = ph->end; buf = ph->buffer; num = ph->end; buf = ph->buffer;
t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, num); t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, num);
break; CALLBACK;
case 2:
{ Line l = {.xnum=self->xnum}; get_line(self, 0, &l); if (l.continued) continue; }
t = nl;
Py_INCREF(t);
break;
} }
if (t == NULL) goto end; Line l = {.xnum=self->xnum}; get_line(self, 0, &l);
ret = PyObject_CallFunctionObjArgs(callback, t, NULL); if (!l.continued) {
Py_DECREF(t); t = PyUnicode_FromString("\n");
if (ret == NULL) goto end; CALLBACK;
Py_DECREF(ret); }
} #undef CALLBACK
end: end:
Py_DECREF(nl);
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }