Source code for str.split?

天涯浪子 提交于 2019-11-29 15:23:50

inspect.getsource(str.split) is not written to handle code written in the implementation language (C here). str.split is builtin, i.e written in C.

The source code for the implementation of str.split is broken up in two parts based on if a sep argument is supplied.

The first function, for when no sep argument is supplied and split removes white space characters, is split_whitespace. How it is implemented is pretty straight-forward; the main bulk is located in the while loop that removes leading whitespace, searches the remaining string characters if any white space exists and splits on it. I've added some comments for illustrating this:

i = j = 0;
while (maxcount-- > 0) {
    /* Increment counter past all leading whitespace in 
       the string. */
    while (i < str_len && STRINGLIB_ISSPACE(str[i]))
        i++;
    /* if string only contains whitespace, break. */
    if (i == str_len) break;

    /* After leading white space, increment counter 
       while the character is not a whitespace. 
       If this ends before i == str_len, it points to 
       a white space character. */
    j = i; i++;
    while (i < str_len && !STRINGLIB_ISSPACE(str[i]))
        i++;
#ifndef STRINGLIB_MUTABLE
    /* Case where no split should be done, return the string. */
    if (j == 0 && i == str_len && STRINGLIB_CHECK_EXACT(str_obj)) {
        /* No whitespace in str_obj, so just use it as list[0] */
        Py_INCREF(str_obj);
        PyList_SET_ITEM(list, 0, (PyObject *)str_obj);
        count++;
        break;
    }
#endif
    /* Make the split based on the incremented counters. */
    SPLIT_ADD(str, j, i);
}

Similarly, split_char is the case where a character is supplied as sep. Its implementation is again pretty straight-forward, examine it a bit after seeing split_whitespace; you won't find it too difficult.

There's also the split function for handling cases where the separator is more than one characters long. This is implemented by searching for the characters in the string and splitting accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!