]> git.saurik.com Git - redis.git/blob - deps/lua/src/strbuf.h
Merge remote-tracking branch 'origin/unstable' into unstable
[redis.git] / deps / lua / src / strbuf.h
1 /* strbuf - String buffer routines
2 *
3 * Copyright (c) 2010-2011 Mark Pulford <mark@kyne.com.au>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdlib.h>
26 #include <stdarg.h>
27
28 /* Size: Total bytes allocated to *buf
29 * Length: String length, excluding optional NULL terminator.
30 * Increment: Allocation increments when resizing the string buffer.
31 * Dynamic: True if created via strbuf_new()
32 */
33
34 typedef struct {
35 char *buf;
36 int size;
37 int length;
38 int increment;
39 int dynamic;
40 int reallocs;
41 int debug;
42 } strbuf_t;
43
44 #ifndef STRBUF_DEFAULT_SIZE
45 #define STRBUF_DEFAULT_SIZE 1023
46 #endif
47 #ifndef STRBUF_DEFAULT_INCREMENT
48 #define STRBUF_DEFAULT_INCREMENT -2
49 #endif
50
51 /* Initialise */
52 extern strbuf_t *strbuf_new(int len);
53 extern void strbuf_init(strbuf_t *s, int len);
54 extern void strbuf_set_increment(strbuf_t *s, int increment);
55
56 /* Release */
57 extern void strbuf_free(strbuf_t *s);
58 extern char *strbuf_free_to_string(strbuf_t *s, int *len);
59
60 /* Management */
61 extern void strbuf_resize(strbuf_t *s, int len);
62 static int strbuf_empty_length(strbuf_t *s);
63 static int strbuf_length(strbuf_t *s);
64 static char *strbuf_string(strbuf_t *s, int *len);
65 static void strbuf_ensure_empty_length(strbuf_t *s, int len);
66
67 /* Update */
68 extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
69 extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
70 static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
71 extern void strbuf_append_string(strbuf_t *s, const char *str);
72 static void strbuf_append_char(strbuf_t *s, const char c);
73 static void strbuf_ensure_null(strbuf_t *s);
74
75 /* Reset string for before use */
76 static inline void strbuf_reset(strbuf_t *s)
77 {
78 s->length = 0;
79 }
80
81 static inline int strbuf_allocated(strbuf_t *s)
82 {
83 return s->buf != NULL;
84 }
85
86 /* Return bytes remaining in the string buffer
87 * Ensure there is space for a NULL terminator. */
88 static inline int strbuf_empty_length(strbuf_t *s)
89 {
90 return s->size - s->length - 1;
91 }
92
93 static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
94 {
95 if (len > strbuf_empty_length(s))
96 strbuf_resize(s, s->length + len);
97 }
98
99 static inline int strbuf_length(strbuf_t *s)
100 {
101 return s->length;
102 }
103
104 static inline void strbuf_append_char(strbuf_t *s, const char c)
105 {
106 strbuf_ensure_empty_length(s, 1);
107 s->buf[s->length++] = c;
108 }
109
110 static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
111 {
112 s->buf[s->length++] = c;
113 }
114
115 static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
116 {
117 strbuf_ensure_empty_length(s, len);
118 memcpy(s->buf + s->length, c, len);
119 s->length += len;
120 }
121
122 static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
123 {
124 memcpy(s->buf + s->length, c, len);
125 s->length += len;
126 }
127
128 static inline void strbuf_ensure_null(strbuf_t *s)
129 {
130 s->buf[s->length] = 0;
131 }
132
133 static inline char *strbuf_string(strbuf_t *s, int *len)
134 {
135 if (len)
136 *len = s->length;
137
138 return s->buf;
139 }
140
141 /* vi:ai et sw=4 ts=4:
142 */