gh-110289: C API: Add PyUnicode_EqualToUTF8() and PyUnicode_EqualToUTF8AndSize() functions (GH-110297)

This commit is contained in:
Serhiy Storchaka 2023-10-11 16:41:58 +03:00 committed by GitHub
parent d1f7fae424
commit eb50cd37ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 280 additions and 0 deletions

View file

@ -1429,6 +1429,48 @@ unicode_comparewithasciistring(PyObject *self, PyObject *args)
return PyLong_FromLong(result);
}
/* Test PyUnicode_EqualToUTF8() */
static PyObject *
unicode_equaltoutf8(PyObject *self, PyObject *args)
{
PyObject *left;
const char *right = NULL;
Py_ssize_t right_len;
int result;
if (!PyArg_ParseTuple(args, "Oz#", &left, &right, &right_len)) {
return NULL;
}
NULLABLE(left);
result = PyUnicode_EqualToUTF8(left, right);
assert(!PyErr_Occurred());
return PyLong_FromLong(result);
}
/* Test PyUnicode_EqualToUTF8AndSize() */
static PyObject *
unicode_equaltoutf8andsize(PyObject *self, PyObject *args)
{
PyObject *left;
const char *right = NULL;
Py_ssize_t right_len;
Py_ssize_t size = -100;
int result;
if (!PyArg_ParseTuple(args, "Oz#|n", &left, &right, &right_len, &size)) {
return NULL;
}
NULLABLE(left);
if (size == -100) {
size = right_len;
}
result = PyUnicode_EqualToUTF8AndSize(left, right, size);
assert(!PyErr_Occurred());
return PyLong_FromLong(result);
}
/* Test PyUnicode_RichCompare() */
static PyObject *
unicode_richcompare(PyObject *self, PyObject *args)
@ -2044,6 +2086,8 @@ static PyMethodDef TestMethods[] = {
{"unicode_replace", unicode_replace, METH_VARARGS},
{"unicode_compare", unicode_compare, METH_VARARGS},
{"unicode_comparewithasciistring",unicode_comparewithasciistring,METH_VARARGS},
{"unicode_equaltoutf8", unicode_equaltoutf8, METH_VARARGS},
{"unicode_equaltoutf8andsize",unicode_equaltoutf8andsize, METH_VARARGS},
{"unicode_richcompare", unicode_richcompare, METH_VARARGS},
{"unicode_format", unicode_format, METH_VARARGS},
{"unicode_contains", unicode_contains, METH_VARARGS},