Skip to content

gh-76535: Make PyUnicode_ToLowerFull and friends public #136176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,51 @@ These APIs can be used for fast direct character conversions:
possible. This function does not raise exceptions.


.. c:function:: Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

Convert *ch* to lower case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be lower cased
(e.g. a maximum of two character for Unicode 16.0), and
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

Convert *ch* to upper case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be upper cased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

Convert *ch* to title case, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be title cased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next


.. c:function:: Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)

Foldcase *ch*, store result in *buffer*, which should be
able to hold as many characters needed for *ch* to be foldcased
(e.g. a maximum of three character for Unicode 16.0), and
return the number of characters stored. If at some point a buffer overflow
is detected, an :exc:`ValueError` is raised and ``-1`` is returned.

.. versionadded:: next



These APIs can be used to work with surrogates:

.. c:function:: int Py_UNICODE_IS_SURROGATE(Py_UCS4 ch)
Expand Down
25 changes: 25 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,31 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
Py_UCS4 ch /* Unicode character */
);

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToLower(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToUpper(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToTitle(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
Py_ssize_t size /* Buffer size */
);

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToFolded(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res, /* Output buffer */
Py_ssize_t size /* Buffer size */
);


// Helper array used by Py_UNICODE_ISSPACE().
PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];

Expand Down
4 changes: 0 additions & 4 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ extern "C" {

extern int _PyUnicode_IsXidStart(Py_UCS4 ch);
extern int _PyUnicode_IsXidContinue(Py_UCS4 ch);
extern int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res);
extern int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res);
extern int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res);
extern int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res);
extern int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch);
extern int _PyUnicode_IsCased(Py_UCS4 ch);

Expand Down
55 changes: 55 additions & 0 deletions Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,61 @@ def test_GET_CACHED_HASH(self):
# impl detail: ASCII string hashes are equal to bytes ones
self.assertEqual(unicode_GET_CACHED_HASH(obj), hash(content_bytes))

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_tolower(self):
import string
from _testcapi import unicode_tolower

for i, c in enumerate(string.ascii_uppercase):
with self.subTest(c):
self.assertEqual(unicode_tolower(c), string.ascii_lowercase[i])

# Test unicode character
self.assertEqual(unicode_tolower("Č"), "č")
self.assertEqual(unicode_tolower("Σ"), "σ")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_toupper(self):
import string
from _testcapi import unicode_toupper

for i, c in enumerate(string.ascii_lowercase):
with self.subTest(c):
self.assertEqual(unicode_toupper(c), string.ascii_uppercase[i])

# Test unicode character
self.assertEqual(unicode_toupper("č"), "Č")
self.assertEqual(unicode_toupper("ß"), "SS")
self.assertEqual(unicode_toupper("ΐ"), "Ϊ́")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_totitle(self):
from _testcapi import unicode_totitle

self.assertEqual(unicode_totitle("t"), "T")

# Test unicode character
self.assertEqual(unicode_totitle("ł"), "Ł")
self.assertEqual(unicode_totitle("ß"), "Ss")
self.assertEqual(unicode_totitle("ΐ"), "Ϊ́")

@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_tofolded(self):
from _testcapi import unicode_tofolded

self.assertEqual(unicode_tofolded("T"), "t")

# Test unicode character
self.assertEqual(unicode_tofolded("Ł"), "ł")
self.assertEqual(unicode_tofolded("Σ"), "σ")

# Test case-ignorable character
self.assertEqual(unicode_tofolded("👍"), "👍")


class PyUnicodeWriterTest(unittest.TestCase):
def create_writer(self, size):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :c:func:`PyUnicode_ToLower`, :c:func:`PyUnicode_ToUpper`, :c:func:`PyUnicode_ToTitle` and :c:func:`PyUnicode_ToFolded` public.
58 changes: 58 additions & 0 deletions Modules/_testcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,60 @@ unicode_copycharacters(PyObject *self, PyObject *args)
return Py_BuildValue("(Nn)", to_copy, copied);
}

static PyObject *
unicode_case_operation(PyObject *str, Py_ssize_t (*function)(Py_UCS4, Py_UCS4 *, Py_ssize_t))
{
if (!PyUnicode_Check(str)) {
PyErr_Format(PyExc_TypeError, "expect str type, got %T", str);
return NULL;
}

if (PyUnicode_GET_LENGTH(str) != 1) {
PyErr_SetString(PyExc_ValueError, "expecting 1-character strings only");
return NULL;
}

Py_UCS4 c = PyUnicode_READ_CHAR(str, 0);

Py_UCS4 buf[3];
Py_ssize_t chars = function(c, buf, Py_ARRAY_LENGTH(buf));
if (chars < 0) {
return NULL;
}

return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buf, chars);
}

/* Test PyUnicode_ToLower() */
static PyObject *
unicode_tolower(PyObject *self, PyObject *arg)
{
return unicode_case_operation(arg, PyUnicode_ToLower);
}

/* Test PyUnicode_ToUpper() */
static PyObject *
unicode_toupper(PyObject *self, PyObject *arg)
{
return unicode_case_operation(arg, PyUnicode_ToUpper);
}


/* Test PyUnicode_ToLower() */
static PyObject *
unicode_totitle(PyObject *self, PyObject *arg)
{
return unicode_case_operation(arg, PyUnicode_ToTitle);
}

/* Test PyUnicode_ToLower() */
static PyObject *
unicode_tofolded(PyObject *self, PyObject *arg)
{
return unicode_case_operation(arg, PyUnicode_ToFolded);
}


static PyObject*
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
{
Expand Down Expand Up @@ -577,6 +631,10 @@ static PyMethodDef TestMethods[] = {
{"unicode_asutf8", unicode_asutf8, METH_VARARGS},
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
{"unicode_GET_CACHED_HASH", unicode_GET_CACHED_HASH, METH_O},
{"unicode_tolower", unicode_tolower, METH_O},
{"unicode_toupper", unicode_toupper, METH_O},
{"unicode_totitle", unicode_totitle, METH_O},
{"unicode_tofolded", unicode_tofolded, METH_O},
{NULL},
};

Expand Down
46 changes: 41 additions & 5 deletions Objects/unicodectype.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,67 +198,103 @@ Py_UCS4 _PyUnicode_ToLowercase(Py_UCS4 ch)
return ch + ctype->lower;
}

int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res)
Py_ssize_t PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->lower & 0xFFFF;
int n = ctype->lower >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
return n;
}

if (size < 1) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->lower;
return 1;
}

int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res)
Py_ssize_t PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->title & 0xFFFF;
int n = ctype->title >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
return n;
}

if (size < 1) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->title;
return 1;
}

int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res)
Py_ssize_t PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->upper & 0xFFFF;
int n = ctype->upper >> 24;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
return n;
}

if (size < 1) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}
res[0] = ch + ctype->upper;
return 1;
}

int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res)
Py_ssize_t PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, Py_ssize_t size)
{
const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);

if (ctype->flags & EXTENDED_CASE_MASK && (ctype->lower >> 20) & 7) {
int index = (ctype->lower & 0xFFFF) + (ctype->lower >> 24);
int n = (ctype->lower >> 20) & 7;
if (n > size) {
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
return -1;
}

int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
return n;
}
return _PyUnicode_ToLowerFull(ch, res);

return PyUnicode_ToLower(ch, res, size);
}

int _PyUnicode_IsCased(Py_UCS4 ch)
Expand Down
Loading
Loading