bytesobject.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Bytes (String) object interface */
  2. #ifndef Py_BYTESOBJECT_H
  3. #define Py_BYTESOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include <stdarg.h>
  8. /*
  9. Type PyBytesObject represents a character string. An extra zero byte is
  10. reserved at the end to ensure it is zero-terminated, but a size is
  11. present so strings with null bytes in them can be represented. This
  12. is an immutable object type.
  13. There are functions to create new string objects, to test
  14. an object for string-ness, and to get the
  15. string value. The latter function returns a null pointer
  16. if the object is not of the proper type.
  17. There is a variant that takes an explicit size as well as a
  18. variant that assumes a zero-terminated string. Note that none of the
  19. functions should be applied to nil objects.
  20. */
  21. /* Caching the hash (ob_shash) saves recalculation of a string's hash value.
  22. This significantly speeds up dict lookups. */
  23. #ifndef Py_LIMITED_API
  24. typedef struct {
  25. PyObject_VAR_HEAD
  26. Py_hash_t ob_shash;
  27. char ob_sval[1];
  28. /* Invariants:
  29. * ob_sval contains space for 'ob_size+1' elements.
  30. * ob_sval[ob_size] == 0.
  31. * ob_shash is the hash of the string or -1 if not computed yet.
  32. */
  33. } PyBytesObject;
  34. #endif
  35. PyAPI_DATA(PyTypeObject) PyBytes_Type;
  36. PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
  37. #define PyBytes_Check(op) \
  38. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
  39. #define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
  40. PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
  41. PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
  42. PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
  43. PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
  44. Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
  45. PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
  46. Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
  47. PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
  48. PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
  49. PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
  50. PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
  51. PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
  52. #ifndef Py_LIMITED_API
  53. PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
  54. PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
  55. const char *format,
  56. Py_ssize_t format_len,
  57. PyObject *args,
  58. int use_bytearray);
  59. PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
  60. PyObject *string,
  61. int use_bytearray);
  62. #endif
  63. PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
  64. const char *, Py_ssize_t,
  65. const char *);
  66. #ifndef Py_LIMITED_API
  67. /* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
  68. PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
  69. const char *, Py_ssize_t,
  70. const char *,
  71. const char **);
  72. #endif
  73. /* Macro, trading safety for speed */
  74. #ifndef Py_LIMITED_API
  75. #define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
  76. (((PyBytesObject *)(op))->ob_sval))
  77. #define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
  78. #endif
  79. /* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
  80. x must be an iterable object. */
  81. #ifndef Py_LIMITED_API
  82. PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
  83. #endif
  84. /* Provides access to the internal data buffer and size of a string
  85. object or the default encoded version of a Unicode object. Passing
  86. NULL as *len parameter will force the string buffer to be
  87. 0-terminated (passing a string with embedded NULL characters will
  88. cause an exception). */
  89. PyAPI_FUNC(int) PyBytes_AsStringAndSize(
  90. PyObject *obj, /* string or Unicode object */
  91. char **s, /* pointer to buffer variable */
  92. Py_ssize_t *len /* pointer to length variable or NULL
  93. (only possible for 0-terminated
  94. strings) */
  95. );
  96. /* Using the current locale, insert the thousands grouping
  97. into the string pointed to by buffer. For the argument descriptions,
  98. see Objects/stringlib/localeutil.h */
  99. #ifndef Py_LIMITED_API
  100. PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
  101. Py_ssize_t n_buffer,
  102. char *digits,
  103. Py_ssize_t n_digits,
  104. Py_ssize_t min_width);
  105. /* Using explicit passed-in values, insert the thousands grouping
  106. into the string pointed to by buffer. For the argument descriptions,
  107. see Objects/stringlib/localeutil.h */
  108. PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
  109. Py_ssize_t n_buffer,
  110. char *digits,
  111. Py_ssize_t n_digits,
  112. Py_ssize_t min_width,
  113. const char *grouping,
  114. const char *thousands_sep);
  115. #endif
  116. /* Flags used by string formatting */
  117. #define F_LJUST (1<<0)
  118. #define F_SIGN (1<<1)
  119. #define F_BLANK (1<<2)
  120. #define F_ALT (1<<3)
  121. #define F_ZERO (1<<4)
  122. #ifndef Py_LIMITED_API
  123. /* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
  124. A _PyBytesWriter variable must be declared at the end of variables in a
  125. function to optimize the memory allocation on the stack. */
  126. typedef struct {
  127. /* bytes, bytearray or NULL (when the small buffer is used) */
  128. PyObject *buffer;
  129. /* Number of allocated size. */
  130. Py_ssize_t allocated;
  131. /* Minimum number of allocated bytes,
  132. incremented by _PyBytesWriter_Prepare() */
  133. Py_ssize_t min_size;
  134. /* If non-zero, use a bytearray instead of a bytes object for buffer. */
  135. int use_bytearray;
  136. /* If non-zero, overallocate the buffer (default: 0).
  137. This flag must be zero if use_bytearray is non-zero. */
  138. int overallocate;
  139. /* Stack buffer */
  140. int use_small_buffer;
  141. char small_buffer[512];
  142. } _PyBytesWriter;
  143. /* Initialize a bytes writer
  144. By default, the overallocation is disabled. Set the overallocate attribute
  145. to control the allocation of the buffer. */
  146. PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
  147. /* Get the buffer content and reset the writer.
  148. Return a bytes object, or a bytearray object if use_bytearray is non-zero.
  149. Raise an exception and return NULL on error. */
  150. PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
  151. void *str);
  152. /* Deallocate memory of a writer (clear its internal buffer). */
  153. PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
  154. /* Allocate the buffer to write size bytes.
  155. Return the pointer to the beginning of buffer data.
  156. Raise an exception and return NULL on error. */
  157. PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
  158. Py_ssize_t size);
  159. /* Ensure that the buffer is large enough to write *size* bytes.
  160. Add size to the writer minimum size (min_size attribute).
  161. str is the current pointer inside the buffer.
  162. Return the updated current pointer inside the buffer.
  163. Raise an exception and return NULL on error. */
  164. PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
  165. void *str,
  166. Py_ssize_t size);
  167. /* Resize the buffer to make it larger.
  168. The new buffer may be larger than size bytes because of overallocation.
  169. Return the updated current pointer inside the buffer.
  170. Raise an exception and return NULL on error.
  171. Note: size must be greater than the number of allocated bytes in the writer.
  172. This function doesn't use the writer minimum size (min_size attribute).
  173. See also _PyBytesWriter_Prepare().
  174. */
  175. PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
  176. void *str,
  177. Py_ssize_t size);
  178. /* Write bytes.
  179. Raise an exception and return NULL on error. */
  180. PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
  181. void *str,
  182. const void *bytes,
  183. Py_ssize_t size);
  184. #endif /* Py_LIMITED_API */
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif /* !Py_BYTESOBJECT_H */