Skip to content

gh-108767: Convert Py_ISLOWER() macro to static inline function #108770

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
36 changes: 27 additions & 9 deletions Include/cpython/pyctype.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,37 @@ PyAPI_DATA(const unsigned int) _Py_ctype_table[256];
/* Unlike their C counterparts, the following macros are not meant to
* handle an int with any of the values [EOF, 0-UCHAR_MAX]. The argument
* must be a signed/unsigned char. */
#define Py_ISLOWER(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_LOWER)
#define Py_ISUPPER(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_UPPER)
#define Py_ISALPHA(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALPHA)
#define Py_ISDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_DIGIT)
#define Py_ISXDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_XDIGIT)
#define Py_ISALNUM(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALNUM)
#define Py_ISSPACE(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_SPACE)
static inline int Py_ISLOWER(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_LOWER);
}
static inline int Py_ISUPPER(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_UPPER);
}
static inline int Py_ISALPHA(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALPHA);
}
static inline int Py_ISDIGIT(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_DIGIT);
}
static inline int Py_ISXDIGIT(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_XDIGIT);
}
static inline int Py_ISALNUM(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALNUM);
}
static inline int Py_ISSPACE(int c) {
return (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_SPACE);
}

PyAPI_DATA(const unsigned char) _Py_ctype_tolower[256];
PyAPI_DATA(const unsigned char) _Py_ctype_toupper[256];

#define Py_TOLOWER(c) (_Py_ctype_tolower[Py_CHARMASK(c)])
#define Py_TOUPPER(c) (_Py_ctype_toupper[Py_CHARMASK(c)])
static inline int Py_TOLOWER(int c) {
return _Py_ctype_tolower[Py_CHARMASK(c)];
}
static inline int Py_TOUPPER(int c) {
return _Py_ctype_toupper[Py_CHARMASK(c)];
}

#ifdef __cplusplus
}
Expand Down
4 changes: 3 additions & 1 deletion Include/pymacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
#define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member)

/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
static inline unsigned char Py_CHARMASK(int c) {
return (unsigned char)(c & 0xff);
}

/* Assert a build-time dependency, as an expression.

Expand Down
2 changes: 1 addition & 1 deletion Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static unsigned int sre_toupper(unsigned int ch) {

static unsigned int sre_lower_ascii(unsigned int ch)
{
return ((ch) < 128 ? Py_TOLOWER(ch) : ch);
return ((ch) < 128 ? (unsigned int)Py_TOLOWER((unsigned char)ch) : ch);
}

/* locale-specific character predicates */
Expand Down
10 changes: 6 additions & 4 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,11 +1337,13 @@ bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
}

if (table_chars == NULL) {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(i);
for (int ch = 0; ch < 256; ch++) {
trans_table[ch] = Py_CHARMASK(ch);
}
} else {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(table_chars[i]);
for (int ch = 0; ch < 256; ch++) {
trans_table[ch] = Py_CHARMASK(table_chars[ch]);
}
}

for (i = 0; i < vdel.len; i++)
Expand Down
10 changes: 6 additions & 4 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2128,11 +2128,13 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
}

if (table_chars == NULL) {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(i);
for (int ch = 0; ch < 256; ch++) {
trans_table[ch] = Py_CHARMASK(ch);
}
} else {
for (i = 0; i < 256; i++)
trans_table[i] = Py_CHARMASK(table_chars[i]);
for (int ch = 0; ch < 256; ch++) {
trans_table[ch] = Py_CHARMASK(table_chars[ch]);
}
}
PyBuffer_Release(&table_view);

Expand Down