LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 152|回复: 0

python 方法和槽位

[复制链接]
发表于 2024-1-29 00:40:18 | 显示全部楼层 |阅读模式


方法与槽位函数
以下 API 可以处理输入的 Unicode 对象和字符串(在描述中我们称其为字符串)并返回适当的 Unicode 对象或整数值。

如果发生异常它们都将返回 NULL 或 -1。

PyObject *PyUnicode_Concat(PyObject *left, PyObject *right)
返回值:新的引用。 属于 稳定 ABI.
拼接两个字符串得到一个新的 Unicode 字符串。

PyObject *PyUnicode_Split(PyObject *unicode, PyObject *sep, Py_ssize_t maxsplit)
返回值:新的引用。 属于 稳定 ABI.
拆分一个字符串得到一个 Unicode 字符串的列表。 如果 sep 为 NULL,则将根据空格来拆分所有子字符串。 否则,将根据指定的分隔符来拆分。 最多拆分数为 maxsplit。 如为负值,则没有限制。 分隔符不包括在结果列表中。

PyObject *PyUnicode_Splitlines(PyObject *unicode, int keepends)
返回值:新的引用。 属于 稳定 ABI.
根据分行符来拆分 Unicode 字符串,返回一个 Unicode 字符串的列表。 CRLF 将被视为一个分行符。 如果 keepends 为 0,则行分隔符将不包括在结果字符串中。

PyObject *PyUnicode_Join(PyObject *separator, PyObject *seq)
返回值:新的引用。 属于 稳定 ABI.
使用给定的 separator 合并一个字符串列表并返回结果 Unicode 字符串。

Py_ssize_t PyUnicode_Tailmatch(PyObject *unicode, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
属于 稳定 ABI.
如果 substr 在给定的端点 (direction == -1 表示前缀匹配, direction == 1 表示后缀匹配) 与 unicode[start:end] 相匹配则返回 1,否则返回 0。 如果发生错误则返回 -1。

Py_ssize_t PyUnicode_Find(PyObject *unicode, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
属于 稳定 ABI.
返回使用给定的 direction (direction == 1 表示前向搜索,direction == -1 表示后向搜索) 时 substr 在 unicode[start:end] 中首次出现的位置。 返回值为首个匹配的索引号;值为 -1 表示未找到匹配,-2 则表示发生了错误并设置了异常。

Py_ssize_t PyUnicode_FindChar(PyObject *unicode, Py_UCS4 ch, Py_ssize_t start, Py_ssize_t end, int direction)
属于 稳定 ABI 自 3.7 版开始.
返回使用给定的 direction (direction == 1 表示前向搜索,direction == -1 表示后向搜索) 时字符 ch 在 unicode[start:end] 中首次出现的位置。 返回值为首个匹配的索引号;值为 -1 表示未找到匹配,-2 则表示发生错误并设置了异常。

在 3.3 版本加入.

在 3.7 版本发生变更: 现在 start 和 end 被调整为与 unicode[start:end] 类似的行为。

Py_ssize_t PyUnicode_Count(PyObject *unicode, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
属于 稳定 ABI.
返回 substr 在 unicode[start:end] 中不重叠出现的次数。 如果发生错误则返回 -1。

PyObject *PyUnicode_Replace(PyObject *unicode, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
返回值:新的引用。 属于 稳定 ABI.
将 unicode 中 substr 替换为 replstr 至多 maxcount 次并返回结果 Unicode 对象。 maxcount == -1 表示全部替换。

int PyUnicode_Compare(PyObject *left, PyObject *right)
属于 稳定 ABI.
比较两个字符串并返回 -1, 0, 1 分别表示小于、等于和大于。

此函数执行失败时返回 -1,因此应当调用 PyErr_Occurred() 来检查错误。

int PyUnicode_CompareWithASCIIString(PyObject *unicode, const char *string)
属于 稳定 ABI.
将 Unicode 对象 unicode 与 string 进行比较并返回 -1, 0, 1 分别表示小于、等于和大于。 最好只传入 ASCII 编码的字符串,但如果输入字符串包含非 ASCII 字符则此函数会将其按 ISO-8859-1 编码格式来解读。

此函数不会引发异常。

PyObject *PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
返回值:新的引用。 属于 稳定 ABI.
对两个 Unicode 字符串执行富比较并返回以下值之一:

NULL 用于引发了异常的情况

Py_True 或 Py_False 用于成功完成比较的情况

Py_NotImplemented 用于类型组合未知的情况

可能的 op 值有 Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, 和 Py_LE。

PyObject *PyUnicode_Format(PyObject *format, PyObject *args)
返回值:新的引用。 属于 稳定 ABI.
根据 format 和 args 返回一个新的字符串对象;这等同于 format % args。

int PyUnicode_Contains(PyObject *unicode, PyObject *substr)
属于 稳定 ABI.
检查 substr 是否包含在 unicode 中并相应返回真值或假值。

substr 必须强制转为一个单元素 Unicode 字符串。 如果发生错误则返回 -1。

void PyUnicode_InternInPlace(PyObject **p_unicode)
属于 稳定 ABI.
原地内部化参数 *p_unicode。 该参数必须是一个指向 Python Unicode 字符串对象的指针变量的地址。 如果已存在与 *p_unicode 相同的内部化字符串,则将其设为 *p_unicode (释放对旧字符串的引用并新建一个指向内部化字符串对象的 strong reference),否则将保持 *p_unicode 不变并将其内部化 (新建一个 strong reference)。 (澄清说明:虽然这里大量提及了引用,但请将此函数视为引用无关的;当且仅当你在调用之前就已拥有该对象时你才会在调用之后也拥有它。)

PyObject *PyUnicode_InternFromString(const char *str)
返回值:新的引用。 属于 稳定 ABI.
PyUnicode_FromString() 和 PyUnicode_InternInPlace() 的组合操作,返回一个已内部化的新 Unicode 字符串对象,或一个指向具有相同值的原有内部化字符串对象的新的(“拥有的”)引用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表