diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index 0397f12a8260b0..a07034e6e95088 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -436,17 +436,19 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op) if (PyUnicode_IS_ASCII(op)) { return 0x7fU; } - + static const Py_UCS4 max_char_values[] = { + 0, /* PyUnicode_WCHAR_KIND */ + 0xffU, /* PyUnicode_1BYTE_KIND */ + 0xffffU, /* PyUnicode_2BYTE_KIND */ + 0, + 0x10ffffU, /* PyUnicode_4BYTE_KIND */ + }; unsigned int kind = PyUnicode_KIND(op); - if (kind == PyUnicode_1BYTE_KIND) { - return 0xffU; - } - if (kind == PyUnicode_2BYTE_KIND) { - return 0xffffU; - } - assert(kind == PyUnicode_4BYTE_KIND); - return 0x10ffffU; + const Py_UCS4 value = max_char_values[kind]; + assert(value); + return value; } + #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 # define PyUnicode_MAX_CHAR_VALUE(op) \ PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))