| 1 |
#ifndef Py_INTERNAL_BYTESOBJECT_H |
| 2 |
#define Py_INTERNAL_BYTESOBJECT_H |
| 3 |
#ifdef __cplusplus |
| 4 |
extern "C" { |
| 5 |
#endif |
| 6 |
|
| 7 |
#ifndef Py_BUILD_CORE |
| 8 |
# error "this header requires Py_BUILD_CORE define" |
| 9 |
#endif |
| 10 |
|
| 11 |
extern PyObject* _PyBytes_FormatEx( |
| 12 |
const char *format, |
| 13 |
Py_ssize_t format_len, |
| 14 |
PyObject *args, |
| 15 |
int use_bytearray); |
| 16 |
|
| 17 |
extern PyObject* _PyBytes_FromHex( |
| 18 |
PyObject *string, |
| 19 |
int use_bytearray); |
| 20 |
|
| 21 |
// Helper for PyBytes_DecodeEscape that detects invalid escape chars. |
| 22 |
// Export for test_peg_generator. |
| 23 |
PyAPI_FUNC(PyObject*) _PyBytes_DecodeEscape2(const char *, Py_ssize_t, |
| 24 |
const char *, |
| 25 |
int *, const char **); |
| 26 |
|
| 27 |
|
| 28 |
// Substring Search. |
| 29 |
// |
| 30 |
// Returns the index of the first occurrence of |
| 31 |
// a substring ("needle") in a larger text ("haystack"). |
| 32 |
// If the needle is not found, return -1. |
| 33 |
// If the needle is found, add offset to the index. |
| 34 |
// |
| 35 |
// Export for 'mmap' shared extension. |
| 36 |
PyAPI_FUNC(Py_ssize_t) |
| 37 |
_PyBytes_Find(const char *haystack, Py_ssize_t len_haystack, |
| 38 |
const char *needle, Py_ssize_t len_needle, |
| 39 |
Py_ssize_t offset); |
| 40 |
|
| 41 |
// Same as above, but search right-to-left. |
| 42 |
// Export for 'mmap' shared extension. |
| 43 |
PyAPI_FUNC(Py_ssize_t) |
| 44 |
_PyBytes_ReverseFind(const char *haystack, Py_ssize_t len_haystack, |
| 45 |
const char *needle, Py_ssize_t len_needle, |
| 46 |
Py_ssize_t offset); |
| 47 |
|
| 48 |
|
| 49 |
// Helper function to implement the repeat and inplace repeat methods on a |
| 50 |
// buffer. |
| 51 |
// |
| 52 |
// len_dest is assumed to be an integer multiple of len_src. |
| 53 |
// If src equals dest, then assume the operation is inplace. |
| 54 |
// |
| 55 |
// This method repeately doubles the number of bytes copied to reduce |
| 56 |
// the number of invocations of memcpy. |
| 57 |
// |
| 58 |
// Export for 'array' shared extension. |
| 59 |
PyAPI_FUNC(void) |
| 60 |
_PyBytes_Repeat(char* dest, Py_ssize_t len_dest, |
| 61 |
const char* src, Py_ssize_t len_src); |
| 62 |
|
| 63 |
/* --- _PyBytesWriter ----------------------------------------------------- */ |
| 64 |
|
| 65 |
/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer". |
| 66 |
A _PyBytesWriter variable must be declared at the end of variables in a |
| 67 |
function to optimize the memory allocation on the stack. */ |
| 68 |
typedef struct { |
| 69 |
/* bytes, bytearray or NULL (when the small buffer is used) */ |
| 70 |
PyObject *buffer; |
| 71 |
|
| 72 |
/* Number of allocated size. */ |
| 73 |
Py_ssize_t allocated; |
| 74 |
|
| 75 |
/* Minimum number of allocated bytes, |
| 76 |
incremented by _PyBytesWriter_Prepare() */ |
| 77 |
Py_ssize_t min_size; |
| 78 |
|
| 79 |
/* If non-zero, use a bytearray instead of a bytes object for buffer. */ |
| 80 |
int use_bytearray; |
| 81 |
|
| 82 |
/* If non-zero, overallocate the buffer (default: 0). |
| 83 |
This flag must be zero if use_bytearray is non-zero. */ |
| 84 |
int overallocate; |
| 85 |
|
| 86 |
/* Stack buffer */ |
| 87 |
int use_small_buffer; |
| 88 |
char small_buffer[512]; |
| 89 |
} _PyBytesWriter; |
| 90 |
|
| 91 |
/* Initialize a bytes writer |
| 92 |
|
| 93 |
By default, the overallocation is disabled. Set the overallocate attribute |
| 94 |
to control the allocation of the buffer. |
| 95 |
|
| 96 |
Export _PyBytesWriter API for '_pickle' shared extension. */ |
| 97 |
PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer); |
| 98 |
|
| 99 |
/* Get the buffer content and reset the writer. |
| 100 |
Return a bytes object, or a bytearray object if use_bytearray is non-zero. |
| 101 |
Raise an exception and return NULL on error. */ |
| 102 |
PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer, |
| 103 |
void *str); |
| 104 |
|
| 105 |
/* Deallocate memory of a writer (clear its internal buffer). */ |
| 106 |
PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer); |
| 107 |
|
| 108 |
/* Allocate the buffer to write size bytes. |
| 109 |
Return the pointer to the beginning of buffer data. |
| 110 |
Raise an exception and return NULL on error. */ |
| 111 |
PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer, |
| 112 |
Py_ssize_t size); |
| 113 |
|
| 114 |
/* Ensure that the buffer is large enough to write *size* bytes. |
| 115 |
Add size to the writer minimum size (min_size attribute). |
| 116 |
|
| 117 |
str is the current pointer inside the buffer. |
| 118 |
Return the updated current pointer inside the buffer. |
| 119 |
Raise an exception and return NULL on error. */ |
| 120 |
PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer, |
| 121 |
void *str, |
| 122 |
Py_ssize_t size); |
| 123 |
|
| 124 |
/* Resize the buffer to make it larger. |
| 125 |
The new buffer may be larger than size bytes because of overallocation. |
| 126 |
Return the updated current pointer inside the buffer. |
| 127 |
Raise an exception and return NULL on error. |
| 128 |
|
| 129 |
Note: size must be greater than the number of allocated bytes in the writer. |
| 130 |
|
| 131 |
This function doesn't use the writer minimum size (min_size attribute). |
| 132 |
|
| 133 |
See also _PyBytesWriter_Prepare(). |
| 134 |
*/ |
| 135 |
PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer, |
| 136 |
void *str, |
| 137 |
Py_ssize_t size); |
| 138 |
|
| 139 |
/* Write bytes. |
| 140 |
Raise an exception and return NULL on error. */ |
| 141 |
PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, |
| 142 |
void *str, |
| 143 |
const void *bytes, |
| 144 |
Py_ssize_t size); |
| 145 |
|
| 146 |
#ifdef __cplusplus |
| 147 |
} |
| 148 |
#endif |
| 149 |
#endif /* !Py_INTERNAL_BYTESOBJECT_H */ |