45
45
#define Py_UNICODE_ISALPHA (ch ) _PyUnicode_IsAlpha(ch)
46
46
47
47
#define Py_UNICODE_ISALNUM (ch ) \
48
- (Py_UNICODE_ISALPHA(ch) || \
48
+ (Py_UNICODE_ISALPHA(ch) || \
49
49
Py_UNICODE_ISDECIMAL (ch) || \
50
50
Py_UNICODE_ISDIGIT(ch) || \
51
51
Py_UNICODE_ISNUMERIC(ch))
@@ -379,80 +379,84 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
379
379
}
380
380
#define PyUnicode_GET_LENGTH (op ) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
381
381
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
387
383
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.
389
385
index is the index in the string (starts at 0) and value is the new
390
386
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
+ }
391
401
#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))
408
403
409
404
/* Read a code point from the string's canonical representation. No checks
410
405
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
+ }
411
417
#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))
420
419
421
420
/* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
422
421
calls PyUnicode_KIND() and might call it twice. For single reads, use
423
422
PyUnicode_READ_CHAR, for multiple consecutive reads callers should
424
423
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
+ }
425
436
#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))
443
438
444
439
/* Return a maximum character value which is suitable for creating another
445
440
string based on op. This is always an approximation but more efficient
446
441
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
+ }
447
458
#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))
456
460
457
461
Py_DEPRECATED(3.3 )
458
462
static inline Py_ssize_t PyUnicode_WSTR_LENGTH(PyObject *op) {
@@ -479,12 +483,25 @@ PyAPI_FUNC(PyObject*) PyUnicode_New(
479
483
objects which were created using the old API to the new flexible format
480
484
introduced with PEP 393.
481
485
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
483
487
instead. */
484
488
PyAPI_FUNC (int ) _PyUnicode_Ready(
485
489
PyObject *unicode /* Unicode object */
486
490
);
487
491
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
+
488
505
/* Get a copy of a Unicode string. */
489
506
PyAPI_FUNC (PyObject*) _PyUnicode_Copy(
490
507
PyObject *unicode
0 commit comments