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