原生c代码
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);
Python中路径分割split的源代码
实现效果:
传入路径,eg. C:\Users\Actions\PycharmProjects\myAlgorithm
返回
head=C:\Users\Actions\PycharmProjects, tail=myAlgorithm
分析
程序的大致思路
1.先判断传入的路径的合法性
2.提取路径中的分隔符/
3.提取路径中倒数第一个分隔符的位置,构建head<带分隔符的路径名>和tail<文件名>
4.去除head两边的分隔符<一是为了判断head是否为空,二是为了判断head不会全部由分隔符构成的特殊情况>
5.返回head和tail