1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <errno.h>
#include <stdlib.h>
#include <cu/debug.h>
#include <cu/string.h>
struct string *new_string(const char *s)
{
struct string *n = malloc(sizeof(struct string));
if (!n) {
error("allocating new string: %s\n", strerror(errno));
return NULL;
}
if (!s) {
n->len = 1;
n->buf = calloc(1, 1);
return n;
}
n->len = strlen(s) + 1;
n->buf = strdup(s);
return n;
}
void destroy_string(struct string *s)
{
free(s->buf);
free(s);
}
int str_append(struct string *s, char c)
{
s->len += 1;
s->buf = realloc(s->buf, s->len);
if (!s->buf) {
error("appending to string: %s\n", strerror(errno));
return -1;
}
s->buf[s->len - 2] = c;
s->buf[s->len - 1] = 0;
return 0;
}
int str_concat(struct string *s, const char *c)
{
size_t c_len = strlen(c);
size_t len = s->len + c_len;
s->buf = realloc(s->buf, len);
if (!s->buf) {
error("concatenating strings: %s\n", strerror(errno));
return -1;
}
strncat(s->buf + s->len - 1, c, c_len);
s->len = len;
return 0;
}
int str_add(struct string *s, struct string *c)
{
size_t len = s->len + c->len - 1;
s->buf = realloc(s->buf, len);
if (!s->buf) {
error("adding to string: %s\n", strerror(errno));
return -1;
}
strncat(s->buf + s->len - 1, c->buf, c->len);
s->len = len;
return 0;
}
bool str_compare(struct string *s, const char *c)
{
return strncmp(s->buf, c, s->len - 1) == 0;
}
void str_clear(struct string *s)
{
s->len = 1;
if (s->buf)
s->buf[0] = 0;
}
|