Skip to content

gh-76535: Add C API functions for changing case of a single codepoint #117117

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

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ the Python configuration.

Return ``1`` or ``0`` depending on whether *ch* is a titlecase character.

.. c:function:: int Py_UNICODE_ISCASED(Py_UCS4 ch)

Return ``1`` or ``0`` depending on whether *ch* is a cased character.

.. c:function:: int Py_UNICODE_ISCASEIGNORABLE(Py_UCS4 ch)

Return ``1`` or ``0`` depending on whether *ch* is a case-ignorable character.

Comment on lines +226 to +233
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the versionadded label :)


.. c:function:: int Py_UNICODE_ISLINEBREAK(Py_UCS4 ch)

Expand Down Expand Up @@ -322,6 +330,26 @@ These APIs can be used to work with surrogates:
surrogate pair. *high* must be in the range [0xD800; 0xDBFF] and *low* must
be in the range [0xDC00; 0xDFFF].

.. 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 *size* characters, and return the number of characters stored.
If the buffer is not big enough, return -1.

.. 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 *size* characters, and return the number of characters stored.
If the buffer is not big enough, return -1.

.. 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 *size* characters, and return the number of characters stored.
If the buffer is not big enough, return -1.

Creating and accessing Unicode strings
""""""""""""""""""""""""""""""""""""""
Expand Down
10 changes: 10 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
Py_UCS4 ch /* Unicode character */
);

PyAPI_FUNC(int) _PyUnicode_IsCased(
Py_UCS4 ch /* Unicode character */
);

PyAPI_FUNC(int) _PyUnicode_IsCaseIgnorable(
Py_UCS4 ch /* Unicode character */
);

PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
const Py_UCS4 ch /* Unicode character */
);
Expand Down Expand Up @@ -671,6 +679,8 @@ static inline int Py_UNICODE_ISSPACE(Py_UCS4 ch) {
#define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
#define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
#define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
#define Py_UNICODE_ISCASED(ch) _PyUnicode_IsCased(ch)
#define Py_UNICODE_ISCASEIGNORABLE(ch) _PyUnicode_IsCaseIgnorable(ch)
#define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)

#define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ 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);

/* --- Unicode API -------------------------------------------------------- */

Expand Down
24 changes: 24 additions & 0 deletions Include/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,30 @@ PyAPI_FUNC(int) PyUnicode_Contains(

PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s);

/* Lowercases character and adds result to buffer */

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToLower(
Py_UCS4 ch,
Py_UCS4 *buffer,
Py_ssize_t size
);

/* Uppercases character and adds result to buffer */

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToUpper(
Py_UCS4 ch,
Py_UCS4 *buffer,
Py_ssize_t size
);

/* Titlecases character and adds result to buffer */

PyAPI_FUNC(Py_ssize_t) PyUnicode_ToTitle(
Py_UCS4 ch,
Py_UCS4 *buffer,
Py_ssize_t size
);

/* === Characters Type APIs =============================================== */

#ifndef Py_LIMITED_API
Expand Down
30 changes: 30 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9616,6 +9616,36 @@ case_operation(PyObject *self,
return res;
}

Py_ssize_t
PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
{
Py_ssize_t n = _PyUnicode_ToLowerFull(ch, buffer);
if (n > size) {
return -1;
}
return n;
}

Py_ssize_t
PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
{
Py_ssize_t n = _PyUnicode_ToUpperFull(ch, buffer);
if (n > size) {
return -1;
}
return n;
}

Py_ssize_t
PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *buffer, Py_ssize_t size)
{
Py_ssize_t n = _PyUnicode_ToTitleFull(ch, buffer);
if (n > size) {
return -1;
}
return n;
}

PyObject *
PyUnicode_Join(PyObject *separator, PyObject *seq)
{
Expand Down