str.split 的源代码? [英] Source code for str.split?

查看:102
本文介绍了str.split 的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看 str.split() 在 Python 中是如何实现的这是我尝试过的:

I would like to see how str.split() is implemented in Python Here's what I tried:

> inspect.getsource(str.split)

TypeError: <method 'split' of 'str' objects> is not a module, 
class, method, function, traceback, frame, or code object

在 StackOverflow 上复制另一个示例不起作用:最大公约数的代码在 Python 中

Copying the other example on StackOverflow has not work: Code for Greatest Common Divisor in Python

推荐答案

inspect.getsource(str.split) 不是为了处理以实现语言 (C代码>这里).str.splitbuiltin,即用 C 编写.

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.

str.split 实现的源代码根据是否提供 sep 参数分为两部分.

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

第一个函数是 split_whitespace.它的实施方式非常简单.主要块位于 while 循环中,该循环删除前导空格,如果存在任何空格并在其上拆分,则搜索剩余的字符串字符.我添加了一些注释来说明这一点:

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);
}

同样,split_char 是将字符作为 sep 提供的情况.它的实现再次非常简单,在看到 split_whitespace 后稍微检查一下;你不会觉得太难.

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.

还有 split 函数 用于处理分隔符长度超过一个字符的情况.这是通过搜索字符串中的字符并进行相应拆分来实现的.

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.

这篇关于str.split 的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆