Skip to content

Commit 128d624

Browse files
authored
gh-89653: PEP 670: Convert unicodeobject.h macros to functions (#91773)
Convert unicodeobject.h macros to static inline functions: * PyUnicode_MAX_CHAR_VALUE() * PyUnicode_READ() * PyUnicode_READY() * PyUnicode_READ_CHAR() * PyUnicode_WRITE() Move PyUnicode_READY() after _PyUnicode_Ready(), since it uses _PyUnicode_Ready(). Static inline functions are wrapped by macros which casts arguments with _PyObject_CAST() and casts 'kind' arguments to "unsigned int" to prevent introducing new compiler warnings when passing "const PyObject*".
1 parent f8dc618 commit 128d624

File tree

1 file changed

+74
-57
lines changed

1 file changed

+74
-57
lines changed

Include/cpython/unicodeobject.h

+74-57
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
4646

4747
#define Py_UNICODE_ISALNUM(ch) \
48-
(Py_UNICODE_ISALPHA(ch) || \
48+
(Py_UNICODE_ISALPHA(ch) || \
4949
Py_UNICODE_ISDECIMAL(ch) || \
5050
Py_UNICODE_ISDIGIT(ch) || \
5151
Py_UNICODE_ISNUMERIC(ch))
@@ -379,80 +379,84 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
379379
}
380380
#define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
381381

382-
/* In the access macros below, "kind" may be evaluated more than once.
383-
All other macro parameters are evaluated exactly once, so it is safe
384-
to put side effects into them (such as increasing the index). */
385-
386-
/* Write into the canonical representation, this macro does not do any sanity
382+
/* Write into the canonical representation, this function does not do any sanity
387383
checks and is intended for usage in loops. The caller should cache the
388-
kind and data pointers obtained from other macro calls.
384+
kind and data pointers obtained from other function calls.
389385
index is the index in the string (starts at 0) and value is the new
390386
code point value which should be written to that location. */
387+
static inline void PyUnicode_WRITE(unsigned int kind, void *data,
388+
Py_ssize_t index, Py_UCS4 value)
389+
{
390+
if (kind == PyUnicode_1BYTE_KIND) {
391+
((Py_UCS1 *)data)[index] = (Py_UCS1)value;
392+
}
393+
else if (kind == PyUnicode_2BYTE_KIND) {
394+
((Py_UCS2 *)data)[index] = (Py_UCS2)value;
395+
}
396+
else {
397+
assert(kind == PyUnicode_4BYTE_KIND);
398+
((Py_UCS4 *)data)[index] = value;
399+
}
400+
}
391401
#define PyUnicode_WRITE(kind, data, index, value) \
392-
do { \
393-
switch ((kind)) { \
394-
case PyUnicode_1BYTE_KIND: { \
395-
((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
396-
break; \
397-
} \
398-
case PyUnicode_2BYTE_KIND: { \
399-
((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
400-
break; \
401-
} \
402-
default: { \
403-
assert((kind) == PyUnicode_4BYTE_KIND); \
404-
((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
405-
} \
406-
} \
407-
} while (0)
402+
PyUnicode_WRITE((unsigned int)(kind), (void*)(data), (index), (Py_UCS4)(value))
408403

409404
/* Read a code point from the string's canonical representation. No checks
410405
or ready calls are performed. */
406+
static inline Py_UCS4 PyUnicode_READ(unsigned int kind,
407+
const void *data, Py_ssize_t index)
408+
{
409+
if (kind == PyUnicode_1BYTE_KIND) {
410+
return ((const Py_UCS1 *)data)[index];
411+
}
412+
if (kind == PyUnicode_2BYTE_KIND) {
413+
return ((const Py_UCS2 *)data)[index];
414+
}
415+
return ((const Py_UCS4 *)data)[index];
416+
}
411417
#define PyUnicode_READ(kind, data, index) \
412-
((Py_UCS4) \
413-
((kind) == PyUnicode_1BYTE_KIND ? \
414-
((const Py_UCS1 *)(data))[(index)] : \
415-
((kind) == PyUnicode_2BYTE_KIND ? \
416-
((const Py_UCS2 *)(data))[(index)] : \
417-
((const Py_UCS4 *)(data))[(index)] \
418-
) \
419-
))
418+
PyUnicode_READ((unsigned int)(kind), (const void*)(data), (index))
420419

421420
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
422421
calls PyUnicode_KIND() and might call it twice. For single reads, use
423422
PyUnicode_READ_CHAR, for multiple consecutive reads callers should
424423
cache kind and use PyUnicode_READ instead. */
424+
static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
425+
{
426+
assert(PyUnicode_IS_READY(unicode));
427+
unsigned int kind = PyUnicode_KIND(unicode);
428+
if (kind == PyUnicode_1BYTE_KIND) {
429+
return PyUnicode_1BYTE_DATA(unicode)[index];
430+
}
431+
if (kind == PyUnicode_2BYTE_KIND) {
432+
return PyUnicode_2BYTE_DATA(unicode)[index];
433+
}
434+
return PyUnicode_4BYTE_DATA(unicode)[index];
435+
}
425436
#define PyUnicode_READ_CHAR(unicode, index) \
426-
(assert(PyUnicode_IS_READY(unicode)), \
427-
(Py_UCS4) \
428-
(PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
429-
((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
430-
(PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
431-
((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
432-
((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
433-
) \
434-
))
435-
436-
/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
437-
case. If the canonical representation is not yet set, it will still call
438-
_PyUnicode_Ready().
439-
Returns 0 on success and -1 on errors. */
440-
#define PyUnicode_READY(op) \
441-
((PyUnicode_IS_READY(op) ? \
442-
0 : _PyUnicode_Ready(_PyObject_CAST(op))))
437+
PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
443438

444439
/* Return a maximum character value which is suitable for creating another
445440
string based on op. This is always an approximation but more efficient
446441
than iterating over the string. */
442+
static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
443+
{
444+
assert(PyUnicode_IS_READY(op));
445+
if (PyUnicode_IS_ASCII(op)) {
446+
return 0x7fU;
447+
}
448+
449+
unsigned int kind = PyUnicode_KIND(op);
450+
if (kind == PyUnicode_1BYTE_KIND) {
451+
return 0xffU;
452+
}
453+
if (kind == PyUnicode_2BYTE_KIND) {
454+
return 0xffffU;
455+
}
456+
return 0x10ffffU;
457+
}
447458
#define PyUnicode_MAX_CHAR_VALUE(op) \
448-
(assert(PyUnicode_IS_READY(op)), \
449-
(PyUnicode_IS_ASCII(op) ? \
450-
(0x7f) : \
451-
(PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
452-
(0xffU) : \
453-
(PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
454-
(0xffffU) : \
455-
(0x10ffffU)))))
459+
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
456460

457461
Py_DEPRECATED(3.3)
458462
static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
@@ -479,12 +483,25 @@ PyAPI_FUNC(PyObject*) PyUnicode_New(
479483
objects which were created using the old API to the new flexible format
480484
introduced with PEP 393.
481485
482-
Don't call this function directly, use the public PyUnicode_READY() macro
486+
Don't call this function directly, use the public PyUnicode_READY() function
483487
instead. */
484488
PyAPI_FUNC(int) _PyUnicode_Ready(
485489
PyObject *unicode /* Unicode object */
486490
);
487491

492+
/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
493+
case. If the canonical representation is not yet set, it will still call
494+
_PyUnicode_Ready().
495+
Returns 0 on success and -1 on errors. */
496+
static inline int PyUnicode_READY(PyObject *op)
497+
{
498+
if (PyUnicode_IS_READY(op)) {
499+
return 0;
500+
}
501+
return _PyUnicode_Ready(op);
502+
}
503+
#define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
504+
488505
/* Get a copy of a Unicode string. */
489506
PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
490507
PyObject *unicode

0 commit comments

Comments
 (0)