python文件中的with_python 的startswith和endswith怎么实现的啊,找到了py文件,但是里面居然...

展开全部

为了回答你这个问题我专门把python源码(github python/cpython)下载来搜了下。

首先可以e69da5e6ba9062616964757a686964616f31333365656530确定是C语言实现的,所以在IDE只能看到声明。

然后搜索:find . -type f  -name '*.c' |xargs grep -s 'startswith'

可以看到

./Objects/bytesobject.c:bytes_startswith(PyBytesObject *self, PyObject *args)static PyObject *

bytes_startswith(PyBytesObject *self, PyObject *args)

{

return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), args);

}PyObject *

_Py_bytes_startswith(const char *str, Py_ssize_t len, PyObject *args)

{

return _Py_bytes_tailmatch(str, len, "startswith", args, -1);

}

/* Matches the end (direction >= 0) or start (direction 

* against substr, using the start and end arguments. Returns

* -1 on error, 0 if not found and 1 if found.

*/

static int

tailmatch(const char *str, Py_ssize_t len, PyObject *substr,

Py_ssize_t start, Py_ssize_t end, int direction)

{

Py_buffer sub_view = {NULL, NULL};

const char *sub;

Py_ssize_t slen;

if (PyBytes_Check(substr)) {

sub = PyBytes_AS_STRING(substr);

slen = PyBytes_GET_SIZE(substr);

}

else {

if (PyObject_GetBuffer(substr, &sub_view, PyBUF_SIMPLE) != 0)

return -1;

sub = sub_view.buf;

slen = sub_view.len;

}

ADJUST_INDICES(start, end, len);

if (direction 

/* startswith */

if (start + slen > len)

goto notfound;

} else {

/* endswith */

if (end - start  len)

goto notfound;

if (end - slen > start)

start = end - slen;

}

if (end - start 

goto notfound;

if (memcmp(str + start, sub, slen) != 0)

goto notfound;

PyBuffer_Release(&sub_view);

return 1;

notfound:

PyBuffer_Release(&sub_view);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值