Debian Codegraph @

Include/internal/pycore_bytesobject.h

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