]>
git.saurik.com Git - redis.git/blob - deps/lua/src/strbuf.h
1 /* strbuf - String buffer routines
3 * Copyright (c) 2010-2011 Mark Pulford <mark@kyne.com.au>
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
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()
44 #ifndef STRBUF_DEFAULT_SIZE
45 #define STRBUF_DEFAULT_SIZE 1023
47 #ifndef STRBUF_DEFAULT_INCREMENT
48 #define STRBUF_DEFAULT_INCREMENT -2
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
);
57 extern void strbuf_free(strbuf_t
*s
);
58 extern char *strbuf_free_to_string(strbuf_t
*s
, int *len
);
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
);
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
);
75 /* Reset string for before use */
76 static inline void strbuf_reset(strbuf_t
*s
)
81 static inline int strbuf_allocated(strbuf_t
*s
)
83 return s
->buf
!= NULL
;
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
)
90 return s
->size
- s
->length
- 1;
93 static inline void strbuf_ensure_empty_length(strbuf_t
*s
, int len
)
95 if (len
> strbuf_empty_length(s
))
96 strbuf_resize(s
, s
->length
+ len
);
99 static inline int strbuf_length(strbuf_t
*s
)
104 static inline void strbuf_append_char(strbuf_t
*s
, const char c
)
106 strbuf_ensure_empty_length(s
, 1);
107 s
->buf
[s
->length
++] = c
;
110 static inline void strbuf_append_char_unsafe(strbuf_t
*s
, const char c
)
112 s
->buf
[s
->length
++] = c
;
115 static inline void strbuf_append_mem(strbuf_t
*s
, const char *c
, int len
)
117 strbuf_ensure_empty_length(s
, len
);
118 memcpy(s
->buf
+ s
->length
, c
, len
);
122 static inline void strbuf_append_mem_unsafe(strbuf_t
*s
, const char *c
, int len
)
124 memcpy(s
->buf
+ s
->length
, c
, len
);
128 static inline void strbuf_ensure_null(strbuf_t
*s
)
130 s
->buf
[s
->length
] = 0;
133 static inline char *strbuf_string(strbuf_t
*s
, int *len
)
141 /* vi:ai et sw=4 ts=4: