展开全部
为了回答你这个问题我专门把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;
}