Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,36 @@ 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)

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
(maximum three), and return the number of characters stored.
Passing a ``NULL`` buffer returns the buffer size needed.

.. versionadded:: next


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

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
(maximum three), and return the number of characters stored.
Passing a ``NULL`` buffer returns the buffer size needed.

.. versionadded:: next


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

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
(maximum three), and return the number of characters stored.
Passing a ``NULL`` buffer returns the buffer size needed.

.. versionadded:: next


These APIs can be used to work with surrogates:

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

PyAPI_FUNC(int) PyUnicode_ToLower(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res /* Output buffer */
);

PyAPI_FUNC(int) PyUnicode_ToUpper(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res /* Output buffer */
);

PyAPI_FUNC(int) PyUnicode_ToTitle(
Py_UCS4 ch, /* Unicode character */
Py_UCS4 *res /* Output buffer */
);

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

Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +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
42 changes: 29 additions & 13 deletions Objects/unicodectype.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,51 +198,67 @@
return ch + ctype->lower;
}

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

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->lower & 0xFFFF;
int n = ctype->lower >> 24;
int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
for (i = 0; i < n; i++) {
if (res != NULL) {
res[i] = _PyUnicode_ExtendedCase[index + i];
}
}
return n;
}
res[0] = ch + ctype->lower;

if (res != NULL) {
res[0] = ch + ctype->lower;
}
return 1;
}

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

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->title & 0xFFFF;
int n = ctype->title >> 24;
int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
for (i = 0; i < n; i++) {
if (res != NULL) {
res[i] = _PyUnicode_ExtendedCase[index + i];
}
}
return n;
}
res[0] = ch + ctype->title;
if (res != NULL) {
res[0] = ch + ctype->title;
}
return 1;
}

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

if (ctype->flags & EXTENDED_CASE_MASK) {
int index = ctype->upper & 0xFFFF;
int n = ctype->upper >> 24;
int i;
for (i = 0; i < n; i++)
res[i] = _PyUnicode_ExtendedCase[index + i];
for (i = 0; i < n; i++) {
if (res != NULL) {
res[i] = _PyUnicode_ExtendedCase[index + i];
}
}
return n;
}
res[0] = ch + ctype->upper;
if (res != NULL) {
res[0] = ch + ctype->upper;
}
return 1;
}

Expand All @@ -258,7 +274,7 @@
res[i] = _PyUnicode_ExtendedCase[index + i];
return n;
}
return _PyUnicode_ToLowerFull(ch, res);
return PyUnicode_ToLowerFull(ch, res);

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check warning on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'PyUnicode_ToLowerFull' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'PyUnicode_ToLowerFull' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'PyUnicode_ToLowerFull' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check warning on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'PyUnicode_ToLowerFull' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]

Check failure on line 277 in Objects/unicodectype.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘PyUnicode_ToLowerFull’; did you mean ‘PyUnicode_ToLower’? [-Werror=implicit-function-declaration]
}

int _PyUnicode_IsCased(Py_UCS4 ch)
Expand Down
10 changes: 5 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10046,7 +10046,7 @@ lower_ucs4(int kind, const void *data, Py_ssize_t length, Py_ssize_t i,
mapped[0] = handle_capital_sigma(kind, data, length, i);
return 1;
}
return _PyUnicode_ToLowerFull(c, mapped);
return PyUnicode_ToLower(c, mapped);
}

static Py_ssize_t
Expand All @@ -10057,7 +10057,7 @@ do_capitalize(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UC
Py_UCS4 c, mapped[3];

c = PyUnicode_READ(kind, data, 0);
n_res = _PyUnicode_ToTitleFull(c, mapped);
n_res = PyUnicode_ToTitle(c, mapped);
for (j = 0; j < n_res; j++) {
*maxchar = Py_MAX(*maxchar, mapped[j]);
res[k++] = mapped[j];
Expand All @@ -10084,7 +10084,7 @@ do_swapcase(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4
n_res = lower_ucs4(kind, data, length, i, c, mapped);
}
else if (Py_UNICODE_ISLOWER(c)) {
n_res = _PyUnicode_ToUpperFull(c, mapped);
n_res = PyUnicode_ToUpper(c, mapped);
}
else {
n_res = 1;
Expand All @@ -10110,7 +10110,7 @@ do_upper_or_lower(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res,
if (lower)
n_res = lower_ucs4(kind, data, length, i, c, mapped);
else
n_res = _PyUnicode_ToUpperFull(c, mapped);
n_res = PyUnicode_ToUpper(c, mapped);
for (j = 0; j < n_res; j++) {
*maxchar = Py_MAX(*maxchar, mapped[j]);
res[k++] = mapped[j];
Expand Down Expand Up @@ -10163,7 +10163,7 @@ do_title(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *m
if (previous_is_cased)
n_res = lower_ucs4(kind, data, length, i, c, mapped);
else
n_res = _PyUnicode_ToTitleFull(c, mapped);
n_res = PyUnicode_ToTitle(c, mapped);

for (j = 0; j < n_res; j++) {
*maxchar = Py_MAX(*maxchar, mapped[j]);
Expand Down
Loading