Coverage Report

Created: 2025-01-28 06:45

/src/tarantool/third_party/lua-cjson/strbuf.h
Line
Count
Source (jump to first uncovered line)
1
/* strbuf - String buffer routines
2
 *
3
 * Copyright (c) 2010-2012  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
struct ibuf;
29
30
/* Size: Total bytes allocated to *buf
31
 * Length: String length, excluding optional NULL terminator.
32
 * Increment: Allocation increments when resizing the string buffer.
33
 * Dynamic: True if created via strbuf_new()
34
 */
35
36
typedef struct {
37
    char *buf;
38
    int size;
39
    int length;
40
    int increment;
41
    int reallocs;
42
    int debug;
43
    /** Backend allocator for the buffer data. */
44
    struct ibuf *ibuf;
45
} strbuf_t;
46
47
#ifndef STRBUF_DEFAULT_SIZE
48
0
#define STRBUF_DEFAULT_SIZE 1023
49
#endif
50
#ifndef STRBUF_DEFAULT_INCREMENT
51
0
#define STRBUF_DEFAULT_INCREMENT -2
52
#endif
53
54
/* Initialise */
55
extern void strbuf_create(strbuf_t *s, int len, struct ibuf *ibuf);
56
extern void strbuf_set_increment(strbuf_t *s, int increment);
57
58
/* Release */
59
extern void strbuf_destroy(strbuf_t *s);
60
61
/* Management */
62
extern void strbuf_resize(strbuf_t *s, int len);
63
static int strbuf_empty_length(strbuf_t *s);
64
static int strbuf_length(strbuf_t *s);
65
static char *strbuf_string(strbuf_t *s, int *len);
66
static void strbuf_ensure_empty_length(strbuf_t *s, int len);
67
static char *strbuf_empty_ptr(strbuf_t *s);
68
static void strbuf_extend_length(strbuf_t *s, int len);
69
70
/* Update */
71
extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
72
extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
73
static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
74
extern void strbuf_append_string(strbuf_t *s, const char *str);
75
static void strbuf_append_char(strbuf_t *s, const char c);
76
static void strbuf_ensure_null(strbuf_t *s);
77
78
/* Reset string for before use */
79
static inline void strbuf_reset(strbuf_t *s)
80
0
{
81
0
    s->length = 0;
82
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_reset
Unexecuted instantiation: strbuf.c:strbuf_reset
83
84
/* Return bytes remaining in the string buffer
85
 * Ensure there is space for a NULL terminator. */
86
static inline int strbuf_empty_length(strbuf_t *s)
87
0
{
88
0
    return s->size - s->length - 1;
89
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_empty_length
Unexecuted instantiation: strbuf.c:strbuf_empty_length
90
91
static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
92
0
{
93
0
    if (len > strbuf_empty_length(s))
94
0
        strbuf_resize(s, s->length + len);
95
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_ensure_empty_length
Unexecuted instantiation: strbuf.c:strbuf_ensure_empty_length
96
97
static inline char *strbuf_empty_ptr(strbuf_t *s)
98
0
{
99
0
    return s->buf + s->length;
100
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_empty_ptr
Unexecuted instantiation: strbuf.c:strbuf_empty_ptr
101
102
static inline void strbuf_extend_length(strbuf_t *s, int len)
103
0
{
104
0
    s->length += len;
105
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_extend_length
Unexecuted instantiation: strbuf.c:strbuf_extend_length
106
107
static inline int strbuf_length(strbuf_t *s)
108
0
{
109
0
    return s->length;
110
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_length
Unexecuted instantiation: strbuf.c:strbuf_length
111
112
static inline void strbuf_append_char(strbuf_t *s, const char c)
113
0
{
114
0
    strbuf_ensure_empty_length(s, 1);
115
0
    s->buf[s->length++] = c;
116
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_append_char
Unexecuted instantiation: strbuf.c:strbuf_append_char
117
118
static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
119
0
{
120
0
    s->buf[s->length++] = c;
121
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_append_char_unsafe
Unexecuted instantiation: strbuf.c:strbuf_append_char_unsafe
122
123
static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
124
0
{
125
0
    strbuf_ensure_empty_length(s, len);
126
0
    memcpy(s->buf + s->length, c, len);
127
0
    s->length += len;
128
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_append_mem
Unexecuted instantiation: strbuf.c:strbuf_append_mem
129
130
static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
131
0
{
132
0
    memcpy(s->buf + s->length, c, len);
133
0
    s->length += len;
134
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_append_mem_unsafe
Unexecuted instantiation: strbuf.c:strbuf_append_mem_unsafe
135
136
static inline void strbuf_ensure_null(strbuf_t *s)
137
0
{
138
0
    s->buf[s->length] = 0;
139
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_ensure_null
Unexecuted instantiation: strbuf.c:strbuf_ensure_null
140
141
static inline char *strbuf_string(strbuf_t *s, int *len)
142
0
{
143
0
    if (len)
144
0
        *len = s->length;
145
146
0
    return s->buf;
147
0
}
Unexecuted instantiation: lua_cjson.c:strbuf_string
Unexecuted instantiation: strbuf.c:strbuf_string
148
149
/* vi:ai et sw=4 ts=4:
150
 */