⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 str_util.c

📁 Urwid is a Python library for making text console applications. It has many features including fluid
💻 C
📖 第 1 页 / 共 2 页
字号:
    }    if (str[pos] < 0x80)  return 0;        for (i=pos-1; i>=line_start; i--)        if (str[i] < 0x80)  break;        if ((pos-i) & 1)  return 1;    else  return 2;}static PyObject * within_double_byte(PyObject *self, PyObject *args){    const unsigned char *str;    int line_start, pos;    int ret;        if (!PyArg_ParseTuple(args, "sii", &str, &line_start, &pos))        return NULL;    ret = Py_WithinDoubleByte(str, line_start, pos);    return Py_BuildValue("i", ret);}//======================================================================char is_wide_char_doc[] ="is_wide_char(string/unicode text, int offs) -> bool iswide\n\n\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){    const unsigned char *str;    Py_UNICODE *ustr;    int ret[2], str_len;            if (PyUnicode_Check(text))  //text_py is unicode string    {        ustr = PyUnicode_AS_UNICODE(text);        return (Py_GetWidth((long int)ustr[offs]) == 2);    }    if ( text->ob_type != Py_BuildValue("s","")->ob_type ) {        PyErr_SetString(PyExc_TypeError,            "is_wide_char: Argument \"text\" is not a string.");        return -1;    }        str = (const unsigned char *)PyString_AsString(text);    str_len = (int) PyString_Size(text);    if (byte_encoding == ENC_UTF8)    {        Py_DecodeOne(str, str_len, offs, ret);        return (Py_GetWidth(ret[0]) == 2);    }    if (byte_encoding == ENC_WIDE)        return (Py_WithinDoubleByte(str, offs, offs) == 1);    return 0;}static PyObject * is_wide_char(PyObject *self, PyObject *args){    PyObject *text;    int offs;    int ret;        if (!PyArg_ParseTuple(args, "Oi", &text, &offs))        return NULL;    ret = Py_IsWideChar(text, offs);    if ( ret == -1) // error        return NULL;    return Py_BuildValue("O", to_bool(ret));}//======================================================================char move_prev_char_doc[] ="move_prev_char(string/unicode text, int start_offs, int end_offs) -> int pos\n\n\Return the position of the character before end_offs.\n\n\text -- string or unicode text\n\start_offs -- start offset\n\end_offs -- end offset";static int Py_MovePrevChar(PyObject *text, int start_offs,                           int end_offs){    int position;    unsigned char *str;        if (PyUnicode_Check(text))  //text_py is unicode string        return end_offs-1;    else        str = (unsigned char *)PyString_AsString(text);        if (byte_encoding == ENC_UTF8) //encoding is utf8    {        position = end_offs - 1;        while ((str[position]&0xc0) == 0x80)            position -=1;        return position;    }    else if ((byte_encoding == ENC_WIDE) &&             (Py_WithinDoubleByte(str, start_offs, end_offs-1) == 2))        return end_offs-2;    else        return end_offs-1;}static PyObject * move_prev_char(PyObject *self, PyObject *args){    PyObject *text;    int start_offs, end_offs;    int ret;    if (!PyArg_ParseTuple(args, "Oii", &text, &start_offs, &end_offs))        return NULL;     ret = Py_MovePrevChar(text, start_offs, end_offs);    return Py_BuildValue("i", ret);}//======================================================================char move_next_char_doc[] ="move_next_char(string/unicode text, int start_offs, int end_offs) -> int pos\n\n\Return the position of the character after start_offs.\n\n\text -- string or unicode text\n\start_offs -- start offset\n\end_offs -- end offset";static int Py_MoveNextChar(PyObject *text, int start_offs,                           int end_offs){    int position;    unsigned char * str;    if (PyUnicode_Check(text))  //text_py is unicode string        return start_offs+1;    else        str = (unsigned char *)PyString_AsString(text);        if (byte_encoding == ENC_UTF8) //encoding is utf8    {        position = start_offs + 1;        while ((position < end_offs) && ((str[position]&0xc0) == 0x80))            position +=1;         return position;    }    else if ((byte_encoding == ENC_WIDE) &&             (Py_WithinDoubleByte(str, start_offs, start_offs) == 1))        return start_offs+2;    else        return start_offs+1;}static PyObject * move_next_char(PyObject *self, PyObject *args){    PyObject *text;    int start_offs, end_offs;    int ret;    if (!PyArg_ParseTuple(args, "Oii", &text, &start_offs, &end_offs))        return NULL;     ret = Py_MoveNextChar(text, start_offs, end_offs);    return Py_BuildValue("i", ret);}//======================================================================char calc_width_doc[] ="calc_width(string/unicode text, int start_off, int end_offs) -> int width\n\n\Return the screen column width of text between start_offs and end_offs.\n\n\text -- string or unicode text\n\start_offs -- start offset\n\end_offs -- end offset";static int Py_CalcWidth(PyObject *text, int start_offs, int end_offs){    unsigned char * str;    int i, screencols, ret[2], str_len;    Py_UNICODE *ustr;    if (PyUnicode_Check(text))  //text_py is unicode string    {        ustr = PyUnicode_AS_UNICODE(text);        screencols = 0;         for(i=start_offs; i<end_offs; i++)             screencols += Py_GetWidth(ustr[i]);        return screencols;    }    if (!PyString_Check(text))    {        PyErr_SetString(PyExc_TypeError, "Neither unicode nor string.");        return -1;    }    str = (unsigned char *)PyString_AsString(text);    str_len = (int) PyString_Size(text);    if (byte_encoding == ENC_UTF8)    {        i = start_offs;        screencols = 0;        while (i<end_offs) {            Py_DecodeOne(str, str_len, i, ret);            screencols += Py_GetWidth(ret[0]);            i = ret[1];        }        return screencols;    }    return end_offs - start_offs; // "wide" and "narrow"    }static PyObject * calc_width(PyObject *self, PyObject *args){    PyObject *text;    int start_offs, end_offs;    int ret;    if (!PyArg_ParseTuple(args, "Oii", &text, &start_offs, &end_offs))        return NULL;     ret = Py_CalcWidth(text, start_offs, end_offs);    if (ret==-1) //an error occured        return NULL;                return Py_BuildValue("i", ret);}//======================================================================char calc_text_pos_doc[] ="calc_text_pos(string/unicode text, int start_offs, int end_offs, int pref_col)\n\-> (int pos, int actual_col)\n\n\Calculate the closest position to the screen column pref_col in text\n\where start_offs is the offset into text assumed to be screen column 0\n\and end_offs is the end of the range to search.\n\n\Returns (position, actual_col).\n\n\text -- string or unicode text\n\start_offs -- start offset\n\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){    unsigned char * str;    int i, screencols, dummy[2], str_len, width;    Py_UNICODE *ustr;    if (PyUnicode_Check(text))  //text_py is unicode string    {        ustr = PyUnicode_AS_UNICODE(text);        screencols = 0;         for(i=start_offs; i<end_offs; i++)        {            width = Py_GetWidth(ustr[i]);                        if (width+screencols > pref_col)            {                ret[0] = i;                ret[1] = screencols;                return 0;            }            screencols += width;        }                ret[0] = i;        ret[1] = screencols;        return 0;    }    if (!PyString_Check(text))    {        PyErr_SetString(PyExc_TypeError, "Neither unicode nor string.");        return -1;    }    str = (unsigned char *)PyString_AsString(text);    str_len = (int) PyString_Size(text);        if (byte_encoding == ENC_UTF8)    {        i = start_offs;        screencols = 0;        while (i<end_offs)        {            Py_DecodeOne(str, str_len, i, dummy);            width = Py_GetWidth(dummy[0]);            if (width+screencols > pref_col)            {                ret[0] = i;                ret[1] = screencols;                return 0;            }            i = dummy[1];            screencols += width;        }                ret[0] = i;        ret[1] = screencols;        return 0;    }    // "wide" and "narrow"    i = start_offs + pref_col;    if (i>= end_offs)    {        ret[0] = end_offs;        ret[1] = end_offs - start_offs;        return 0;    }    if (byte_encoding == ENC_WIDE)        if (Py_WithinDoubleByte(str, start_offs, i)==2)            i -= 1;    ret[0] = i;    ret[1] = i - start_offs;    return 0;}static PyObject * calc_text_pos(PyObject *self, PyObject *args){    PyObject *text;    int start_offs, end_offs, pref_col;    int ret[2], err;    if (!PyArg_ParseTuple(args, "Oiii", &text, &start_offs, &end_offs,                          &pref_col))        return NULL;     err = Py_CalcTextPos(text, start_offs, end_offs, pref_col, ret);    if (err==-1) //an error occured        return NULL;                          return Py_BuildValue("(ii)", ret[0], ret[1]);}//======================================================================static PyMethodDef Str_UtilMethods[] = {    {"get_byte_encoding", get_byte_encoding, METH_VARARGS,     get_byte_encoding_doc},    {"set_byte_encoding", set_byte_encoding, METH_VARARGS,     set_byte_encoding_doc},    {"get_width", get_width, METH_VARARGS, get_width_doc},    {"decode_one", decode_one, METH_VARARGS, decode_one_doc},    {"decode_one_right", decode_one_right, METH_VARARGS, decode_one_right_doc},    {"within_double_byte", within_double_byte, METH_VARARGS,     within_double_byte_doc},    {"is_wide_char", is_wide_char, METH_VARARGS, is_wide_char_doc},    {"move_prev_char", move_prev_char, METH_VARARGS, move_prev_char_doc},    {"move_next_char", move_next_char, METH_VARARGS, move_next_char_doc},    {"calc_width", calc_width, METH_VARARGS, calc_width_doc},    {"calc_text_pos", calc_text_pos, METH_VARARGS, calc_text_pos_doc},    {NULL, NULL, 0, NULL}        // Sentinel };PyMODINIT_FUNC initstr_util(void){    Py_InitModule("str_util", Str_UtilMethods);}int main(int argc, char *argv[]){    //Pass argv[0] to the Python interpreter:    Py_SetProgramName(argv[0]);    //Initialize the Python interpreter.     Py_Initialize();    //Add a static module:    initstr_util();    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -