@@ -287,37 +287,80 @@ PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
287
287
#define SSTATE_INTERNED_IMMORTAL 2
288
288
289
289
/* Use only if you know it's a string */
290
- #define PyUnicode_CHECK_INTERNED (op ) \
291
- (_PyASCIIObject_CAST(op)->state.interned)
290
+ static inline unsigned int PyUnicode_CHECK_INTERNED (PyObject *op) {
291
+ return _PyASCIIObject_CAST (op)->state .interned ;
292
+ }
293
+ #define PyUnicode_CHECK_INTERNED (op ) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
294
+
295
+ /* Fast check to determine whether an object is ready. Equivalent to:
296
+ PyUnicode_IS_COMPACT(op) || _PyUnicodeObject_CAST(op)->data.any */
297
+ static inline unsigned int PyUnicode_IS_READY (PyObject *op) {
298
+ return _PyASCIIObject_CAST (op)->state .ready ;
299
+ }
300
+ #define PyUnicode_IS_READY (op ) PyUnicode_IS_READY(_PyObject_CAST(op))
292
301
293
302
/* Return true if the string contains only ASCII characters, or 0 if not. The
294
303
string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
295
304
ready. */
296
- #define PyUnicode_IS_ASCII (op ) \
297
- (assert(PyUnicode_IS_READY(op)), \
298
- _PyASCIIObject_CAST (op)->state.ascii)
305
+ static inline unsigned int PyUnicode_IS_ASCII (PyObject *op) {
306
+ assert (PyUnicode_IS_READY (op));
307
+ return _PyASCIIObject_CAST (op)->state .ascii ;
308
+ }
309
+ #define PyUnicode_IS_ASCII (op ) PyUnicode_IS_ASCII(_PyObject_CAST(op))
299
310
300
311
/* Return true if the string is compact or 0 if not.
301
312
No type checks or Ready calls are performed. */
302
- #define PyUnicode_IS_COMPACT (op ) \
303
- (_PyASCIIObject_CAST(op)->state.compact)
313
+ static inline unsigned int PyUnicode_IS_COMPACT (PyObject *op) {
314
+ return _PyASCIIObject_CAST (op)->state .compact ;
315
+ }
316
+ #define PyUnicode_IS_COMPACT (op ) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
304
317
305
318
/* Return true if the string is a compact ASCII string (use PyASCIIObject
306
319
structure), or 0 if not. No type checks or Ready calls are performed. */
307
- #define PyUnicode_IS_COMPACT_ASCII (op ) \
308
- (_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op))
320
+ static inline int PyUnicode_IS_COMPACT_ASCII (PyObject *op) {
321
+ return (_PyASCIIObject_CAST (op)->state .ascii && PyUnicode_IS_COMPACT (op));
322
+ }
323
+ #define PyUnicode_IS_COMPACT_ASCII (op ) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
309
324
310
325
enum PyUnicode_Kind {
311
326
/* String contains only wstr byte characters. This is only possible
312
327
when the string was created with a legacy API and _PyUnicode_Ready()
313
328
has not been called yet. */
314
329
PyUnicode_WCHAR_KIND = 0 ,
315
- /* Return values of the PyUnicode_KIND() macro : */
330
+ /* Return values of the PyUnicode_KIND() function : */
316
331
PyUnicode_1BYTE_KIND = 1 ,
317
332
PyUnicode_2BYTE_KIND = 2 ,
318
333
PyUnicode_4BYTE_KIND = 4
319
334
};
320
335
336
+ /* Return one of the PyUnicode_*_KIND values defined above. */
337
+ #define PyUnicode_KIND (op ) \
338
+ (assert(PyUnicode_Check(op)), \
339
+ assert (PyUnicode_IS_READY(op)), \
340
+ ((PyASCIIObject *)(op))->state.kind)
341
+
342
+ /* Return a void pointer to the raw unicode buffer. */
343
+ static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
344
+ if (PyUnicode_IS_ASCII (op)) {
345
+ return (void *)(_PyASCIIObject_CAST (op) + 1 );
346
+ }
347
+ return (void *)(_PyCompactUnicodeObject_CAST (op) + 1 );
348
+ }
349
+
350
+ static inline void * _PyUnicode_NONCOMPACT_DATA (PyObject *op) {
351
+ void *data = _PyUnicodeObject_CAST (op)->data .any ;
352
+ assert (data != NULL );
353
+ return data;
354
+ }
355
+
356
+ static inline void * PyUnicode_DATA (PyObject *op) {
357
+ if (PyUnicode_IS_COMPACT (op)) {
358
+ return _PyUnicode_COMPACT_DATA (op);
359
+ }
360
+ return _PyUnicode_NONCOMPACT_DATA (op);
361
+ }
362
+ #define PyUnicode_DATA (op ) PyUnicode_DATA(_PyObject_CAST(op))
363
+
321
364
/* Return pointers to the canonical representation cast to unsigned char,
322
365
Py_UCS2, or Py_UCS4 for direct character access.
323
366
No checks are performed, use PyUnicode_KIND() before to ensure
@@ -327,24 +370,14 @@ enum PyUnicode_Kind {
327
370
#define PyUnicode_2BYTE_DATA (op ) ((Py_UCS2*)PyUnicode_DATA(op))
328
371
#define PyUnicode_4BYTE_DATA (op ) ((Py_UCS4*)PyUnicode_DATA(op))
329
372
330
- /* Return one of the PyUnicode_*_KIND values defined above. */
331
- #define PyUnicode_KIND (op ) \
332
- (assert(PyUnicode_IS_READY(op)), \
333
- _PyASCIIObject_CAST (op)->state.kind)
334
-
335
- /* Return a void pointer to the raw unicode buffer. */
336
- #define _PyUnicode_COMPACT_DATA (op ) \
337
- (PyUnicode_IS_ASCII(op) ? \
338
- ((void *)(_PyASCIIObject_CAST(op) + 1 )) : \
339
- ((void *)(_PyCompactUnicodeObject_CAST(op) + 1 )))
340
-
341
- #define _PyUnicode_NONCOMPACT_DATA (op ) \
342
- (assert(_PyUnicodeObject_CAST(op)->data.any), \
343
- (_PyUnicodeObject_CAST(op)->data.any))
344
-
345
- #define PyUnicode_DATA (op ) \
346
- (PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
347
- _PyUnicode_NONCOMPACT_DATA (op))
373
+ /* Returns the length of the unicode string. The caller has to make sure that
374
+ the string has it's canonical representation set before calling
375
+ this function. Call PyUnicode_(FAST_)Ready to ensure that. */
376
+ static inline Py_ssize_t PyUnicode_GET_LENGTH (PyObject *op) {
377
+ assert (PyUnicode_IS_READY (op));
378
+ return _PyASCIIObject_CAST (op)->length ;
379
+ }
380
+ #define PyUnicode_GET_LENGTH (op ) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
348
381
349
382
/* In the access macros below, "kind" may be evaluated more than once.
350
383
All other macro parameters are evaluated exactly once, so it is safe
@@ -400,19 +433,6 @@ enum PyUnicode_Kind {
400
433
) \
401
434
))
402
435
403
- /* Returns the length of the unicode string. The caller has to make sure that
404
- the string has it's canonical representation set before calling
405
- this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
406
- #define PyUnicode_GET_LENGTH (op ) \
407
- (assert(PyUnicode_IS_READY(op)), \
408
- _PyASCIIObject_CAST (op)->length)
409
-
410
-
411
- /* Fast check to determine whether an object is ready. Equivalent to
412
- PyUnicode_IS_COMPACT(op) || _PyUnicodeObject_CAST(op)->data.any */
413
-
414
- #define PyUnicode_IS_READY (op ) (_PyASCIIObject_CAST(op)->state.ready)
415
-
416
436
/* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
417
437
case. If the canonical representation is not yet set, it will still call
418
438
_PyUnicode_Ready().
0 commit comments