diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/str_util.c | 133 |
1 files changed, 72 insertions, 61 deletions
diff --git a/source/str_util.c b/source/str_util.c index 90b665f..76dc1ec 100644 --- a/source/str_util.c +++ b/source/str_util.c @@ -176,8 +176,8 @@ text -- string text\n\ pos -- position in text"; -static void Py_DecodeOne(const unsigned char *text, int text_len, int pos, - int *ret) +static void Py_DecodeOne(const unsigned char *text, Py_ssize_t text_len, + Py_ssize_t pos, Py_ssize_t *ret) { int dummy; @@ -309,17 +309,17 @@ static PyObject * decode_one(PyObject *self, PyObject *args) { PyObject *py_text; - int pos, text_len; + Py_ssize_t pos, text_len; char *text; - int ret[2]; + Py_ssize_t ret[2]; - if (!PyArg_ParseTuple(args, "Oi", &py_text, &pos)) + if (!PyArg_ParseTuple(args, "On", &py_text, &pos)) return NULL; - PyString_AsStringAndSize(py_text, &text, &text_len); + PyBytes_AsStringAndSize(py_text, &text, &text_len); Py_DecodeOne((unsigned char *)text, text_len, pos, ret); - return Py_BuildValue("(i, i)", ret[0], ret[1]); + return Py_BuildValue("(n, n)", ret[0], ret[1]); } @@ -333,10 +333,10 @@ text -- text string \n\ pos -- position in text"; -static void Py_DecodeOneRight(const unsigned char *text, int text_len, int pos, - int *ret) +static void Py_DecodeOneRight(const unsigned char *text, Py_ssize_t text_len, + Py_ssize_t pos, Py_ssize_t *ret) { - int subret[2]; + Py_ssize_t subret[2]; while (pos >= 0) { @@ -364,14 +364,14 @@ static PyObject * decode_one_right(PyObject *self, PyObject *args) PyObject *py_text; - int pos, text_len; + Py_ssize_t pos, text_len; char *text; - int ret[2] = {'?',0}; + Py_ssize_t ret[2] = {'?',0}; - if (!PyArg_ParseTuple(args, "Oi", &py_text, &pos)) + if (!PyArg_ParseTuple(args, "On", &py_text, &pos)) return NULL; - PyString_AsStringAndSize(py_text, &text, &text_len); + PyBytes_AsStringAndSize(py_text, &text, &text_len); Py_DecodeOneRight((const unsigned char *)text, text_len, pos, ret); return Py_BuildValue("(i, i)", ret[0], ret[1]); @@ -391,10 +391,10 @@ Return values:\n\ 2 -- pos is on the 2nd half of a dbe char"; -static int Py_WithinDoubleByte(const unsigned char *str, int line_start, - int pos) +static int Py_WithinDoubleByte(const unsigned char *str, Py_ssize_t line_start, + Py_ssize_t pos) { - int i; + Py_ssize_t i; if ((str[pos] >= 0x40) && (str[pos] < 0x7f)) { @@ -421,14 +421,14 @@ static int Py_WithinDoubleByte(const unsigned char *str, int line_start, static PyObject * within_double_byte(PyObject *self, PyObject *args) { const unsigned char *str; - int line_start, pos; - int ret; + Py_ssize_t line_start, pos; + Py_ssize_t ret; - if (!PyArg_ParseTuple(args, "sii", &str, &line_start, &pos)) + if (!PyArg_ParseTuple(args, "snn", &str, &line_start, &pos)) return NULL; ret = Py_WithinDoubleByte(str, line_start, pos); - return Py_BuildValue("i", ret); + return Py_BuildValue("n", ret); } @@ -439,11 +439,11 @@ Test if the character at offs within text is wide.\n\n\ text -- string or unicode text\n\ offs -- offset"; -static int Py_IsWideChar(PyObject *text, int offs) +static int Py_IsWideChar(PyObject *text, Py_ssize_t offs) { const unsigned char *str; Py_UNICODE *ustr; - int ret[2], str_len; + Py_ssize_t ret[2], str_len; if (PyUnicode_Check(text)) //text_py is unicode string { @@ -458,8 +458,8 @@ static int Py_IsWideChar(PyObject *text, int offs) return -1; } - str = (const unsigned char *)PyString_AsString(text); - str_len = (int) PyString_Size(text); + str = (const unsigned char *)PyBytes_AsString(text); + str_len = (int) PyBytes_Size(text); if (byte_encoding == ENC_UTF8) { @@ -477,10 +477,10 @@ static int Py_IsWideChar(PyObject *text, int offs) static PyObject * is_wide_char(PyObject *self, PyObject *args) { PyObject *text; - int offs; + Py_ssize_t offs; int ret; - if (!PyArg_ParseTuple(args, "Oi", &text, &offs)) + if (!PyArg_ParseTuple(args, "On", &text, &offs)) return NULL; ret = Py_IsWideChar(text, offs); @@ -501,16 +501,16 @@ start_offs -- start offset\n\ end_offs -- end offset"; -static int Py_MovePrevChar(PyObject *text, int start_offs, - int end_offs) +static Py_ssize_t Py_MovePrevChar(PyObject *text, Py_ssize_t start_offs, + Py_ssize_t end_offs) { - int position; + Py_ssize_t position; unsigned char *str; if (PyUnicode_Check(text)) //text_py is unicode string return end_offs-1; else - str = (unsigned char *)PyString_AsString(text); + str = (unsigned char *)PyBytes_AsString(text); if (byte_encoding == ENC_UTF8) //encoding is utf8 { @@ -530,14 +530,14 @@ static int Py_MovePrevChar(PyObject *text, int start_offs, static PyObject * move_prev_char(PyObject *self, PyObject *args) { PyObject *text; - int start_offs, end_offs; - int ret; + Py_ssize_t start_offs, end_offs; + Py_ssize_t ret; - if (!PyArg_ParseTuple(args, "Oii", &text, &start_offs, &end_offs)) + if (!PyArg_ParseTuple(args, "Onn", &text, &start_offs, &end_offs)) return NULL; ret = Py_MovePrevChar(text, start_offs, end_offs); - return Py_BuildValue("i", ret); + return Py_BuildValue("n", ret); } @@ -550,16 +550,16 @@ start_offs -- start offset\n\ end_offs -- end offset"; -static int Py_MoveNextChar(PyObject *text, int start_offs, - int end_offs) +static Py_ssize_t Py_MoveNextChar(PyObject *text, Py_ssize_t start_offs, + Py_ssize_t end_offs) { - int position; + Py_ssize_t position; unsigned char * str; if (PyUnicode_Check(text)) //text_py is unicode string return start_offs+1; else - str = (unsigned char *)PyString_AsString(text); + str = (unsigned char *)PyBytes_AsString(text); if (byte_encoding == ENC_UTF8) //encoding is utf8 { @@ -580,14 +580,14 @@ static int Py_MoveNextChar(PyObject *text, int start_offs, static PyObject * move_next_char(PyObject *self, PyObject *args) { PyObject *text; - int start_offs, end_offs; - int ret; + Py_ssize_t start_offs, end_offs; + Py_ssize_t ret; - if (!PyArg_ParseTuple(args, "Oii", &text, &start_offs, &end_offs)) + if (!PyArg_ParseTuple(args, "Onn", &text, &start_offs, &end_offs)) return NULL; ret = Py_MoveNextChar(text, start_offs, end_offs); - return Py_BuildValue("i", ret); + return Py_BuildValue("n", ret); } @@ -600,10 +600,12 @@ start_offs -- start offset\n\ end_offs -- end offset"; -static int Py_CalcWidth(PyObject *text, int start_offs, int end_offs) +static Py_ssize_t Py_CalcWidth(PyObject *text, Py_ssize_t start_offs, + Py_ssize_t end_offs) { unsigned char * str; - int i, screencols, ret[2], str_len; + Py_ssize_t i, ret[2], str_len; + int screencols; Py_UNICODE *ustr; if (PyUnicode_Check(text)) //text_py is unicode string @@ -617,14 +619,14 @@ static int Py_CalcWidth(PyObject *text, int start_offs, int end_offs) return screencols; } - if (!PyString_Check(text)) + if (!PyBytes_Check(text)) { PyErr_SetString(PyExc_TypeError, "Neither unicode nor string."); return -1; } - str = (unsigned char *)PyString_AsString(text); - str_len = (int) PyString_Size(text); + str = (unsigned char *)PyBytes_AsString(text); + str_len = PyBytes_Size(text); if (byte_encoding == ENC_UTF8) { @@ -675,11 +677,12 @@ end_offs -- end offset\n\ pref_col -- preferred column"; -static int Py_CalcTextPos(PyObject *text, int start_offs, int end_offs, - int pref_col, int *ret) +static int Py_CalcTextPos(PyObject *text, Py_ssize_t start_offs, + Py_ssize_t end_offs, int pref_col, Py_ssize_t *ret) { unsigned char * str; - int i, screencols, dummy[2], str_len, width; + Py_ssize_t i, dummy[2], str_len; + int screencols, width; Py_UNICODE *ustr; if (PyUnicode_Check(text)) //text_py is unicode string @@ -706,14 +709,14 @@ static int Py_CalcTextPos(PyObject *text, int start_offs, int end_offs, return 0; } - if (!PyString_Check(text)) + if (!PyBytes_Check(text)) { PyErr_SetString(PyExc_TypeError, "Neither unicode nor string."); return -1; } - str = (unsigned char *)PyString_AsString(text); - str_len = (int) PyString_Size(text); + str = (unsigned char *)PyBytes_AsString(text); + str_len = PyBytes_Size(text); if (byte_encoding == ENC_UTF8) { @@ -764,10 +767,10 @@ static int Py_CalcTextPos(PyObject *text, int start_offs, int end_offs, static PyObject * calc_text_pos(PyObject *self, PyObject *args) { PyObject *text; - int start_offs, end_offs, pref_col; - int ret[2], err; + Py_ssize_t start_offs, end_offs, ret[2]; + int pref_col, err; - if (!PyArg_ParseTuple(args, "Oiii", &text, &start_offs, &end_offs, + if (!PyArg_ParseTuple(args, "Onni", &text, &start_offs, &end_offs, &pref_col)) return NULL; @@ -775,7 +778,7 @@ static PyObject * calc_text_pos(PyObject *self, PyObject *args) if (err==-1) //an error occured return NULL; - return Py_BuildValue("(ii)", ret[0], ret[1]); + return Py_BuildValue("(nn)", ret[0], ret[1]); } @@ -799,14 +802,21 @@ static PyMethodDef Str_UtilMethods[] = { {NULL, NULL, 0, NULL} // Sentinel }; +static struct PyModuleDef Str_UtilModule = { + PyModuleDef_HEAD_INIT, + "str_util", + NULL, + -1, + Str_UtilMethods +}; -PyMODINIT_FUNC initstr_util(void) +PyMODINIT_FUNC PyInit_str_util(void) { - Py_InitModule("str_util", Str_UtilMethods); + return PyModule_Create(&Str_UtilModule); } - +/* int main(int argc, char *argv[]) { //Pass argv[0] to the Python interpreter: @@ -820,3 +830,4 @@ int main(int argc, char *argv[]) return 0; } +*/ |