1 #ifndef CMARK_BUFFER_H
2 #define CMARK_BUFFER_H
3 
4 #include <stddef.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <limits.h>
8 #include <stdint.h>
9 #include "config.h"
10 #include "cmark-gfm.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef struct {
17   cmark_mem *mem;
18   unsigned char *ptr;
19   bufsize_t asize, size;
20 } cmark_strbuf;
21 
22 extern unsigned char cmark_strbuf__initbuf[];
23 
24 #define CMARK_BUF_INIT(mem)                                                    \
25   { mem, cmark_strbuf__initbuf, 0, 0 }
26 
27 /**
28  * Initialize a cmark_strbuf structure.
29  *
30  * For the cases where CMARK_BUF_INIT cannot be used to do static
31  * initialization.
32  */
33 CMARK_GFM_EXPORT
34 void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
35                        bufsize_t initial_size);
36 
37 /**
38  * Grow the buffer to hold at least `target_size` bytes.
39  */
40 CMARK_GFM_EXPORT
41 void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size);
42 
43 CMARK_GFM_EXPORT
44 void cmark_strbuf_free(cmark_strbuf *buf);
45 
46 CMARK_GFM_EXPORT
47 void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b);
48 
49 CMARK_GFM_EXPORT
50 bufsize_t cmark_strbuf_len(const cmark_strbuf *buf);
51 
52 CMARK_GFM_EXPORT
53 int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b);
54 
55 CMARK_GFM_EXPORT
56 unsigned char *cmark_strbuf_detach(cmark_strbuf *buf);
57 
58 CMARK_GFM_EXPORT
59 void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
60                             const cmark_strbuf *buf);
61 
cmark_strbuf_cstr(const cmark_strbuf * buf)62 static CMARK_INLINE const char *cmark_strbuf_cstr(const cmark_strbuf *buf) {
63   return (char *)buf->ptr;
64 }
65 
66 #define cmark_strbuf_at(buf, n) ((buf)->ptr[n])
67 
68 CMARK_GFM_EXPORT
69 void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
70                       bufsize_t len);
71 
72 CMARK_GFM_EXPORT
73 void cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
74 
75 CMARK_GFM_EXPORT
76 void cmark_strbuf_putc(cmark_strbuf *buf, int c);
77 
78 CMARK_GFM_EXPORT
79 void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
80                       bufsize_t len);
81 
82 CMARK_GFM_EXPORT
83 void cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
84 
85 CMARK_GFM_EXPORT
86 void cmark_strbuf_clear(cmark_strbuf *buf);
87 
88 CMARK_GFM_EXPORT
89 bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos);
90 
91 CMARK_GFM_EXPORT
92 bufsize_t cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, bufsize_t pos);
93 
94 CMARK_GFM_EXPORT
95 void cmark_strbuf_drop(cmark_strbuf *buf, bufsize_t n);
96 
97 CMARK_GFM_EXPORT
98 void cmark_strbuf_truncate(cmark_strbuf *buf, bufsize_t len);
99 
100 CMARK_GFM_EXPORT
101 void cmark_strbuf_rtrim(cmark_strbuf *buf);
102 
103 CMARK_GFM_EXPORT
104 void cmark_strbuf_trim(cmark_strbuf *buf);
105 
106 CMARK_GFM_EXPORT
107 void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
108 
109 CMARK_GFM_EXPORT
110 void cmark_strbuf_unescape(cmark_strbuf *s);
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif
117