-
Notifications
You must be signed in to change notification settings - Fork 2
/
string_helper.c
160 lines (132 loc) · 3.13 KB
/
string_helper.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
int string_replace_in_buffer(char *buffer, size_t bufsz, const char *token,
const char *to)
{
size_t l_token = strlen(token), l_to = strlen(to);
char *s = buffer;
while (1) {
size_t remain = 0;
s = strstr(s, token);
if (!s)
break;
remain = strlen(s + l_token);
if (l_token >= l_to) {
/* xxxbcdefxxx: replace "bcdef" to "b"
* |---|
* token
*/
memmove(s, to, l_to);
memmove(s + l_to, s + l_token, remain);
} else /* l_token < l_to */ {
/* xxxbxxx: replace "b" to "bcdef" */
size_t expand = l_to - l_token, used = s - buffer;
if (expand + used + 1 >= bufsz) /* @bufsz limited */
return -1;
memmove(s + l_to, s + l_token, remain); /* expand first */
memmove(s, to, l_to);
}
s += l_to;
s[remain] = '\0';
}
return 0;
}
#define strict_strto(type, func) \
type strict_##func(const char *s, int base, int *err, const char *tail) \
{ \
type n = 0; \
char *endp; \
\
*err = 1; \
n = func(s, &endp, base); \
if (tail) { \
for (const char *s = &tail[0]; *s; s++) { \
if (*endp == *s) { \
*err = 0; \
break; \
} \
} \
} else if (*endp == '\0') { \
*err = 0; \
} \
\
return n; \
}
#define strict_strto_or_default(type, func) \
type strict_##func##_or_default(const char *s, int base, \
const char *tail, type defv) \
{ \
type n; \
int err; \
\
n = strict_##func(s, base, &err, tail); \
if (err) \
n = defv; \
return n; \
}
typedef long long long_long_t;
strict_strto(long long, strtoll);
strict_strto_or_default(long long, strtoll);
strict_strto(long, strtol);
strict_strto_or_default(long, strtol);
strict_strto(unsigned long, strtoul);
strict_strto_or_default(unsigned long, strtoul);
strict_strto(unsigned long long, strtoull);
strict_strto_or_default(unsigned long long, strtoull);
__attribute__((format(printf, 3, 4)))
size_t strncatprintf(char *s, size_t sz, const char *fmt, ...)
{
size_t n, used = 0;
va_list arg;
while (*s != '\0') {
s++;
used++;
sz--;
}
va_start(arg, fmt);
n = vsnprintf(s, sz, fmt, arg);
va_end(arg);
return used + n;
}
__attribute__((format(printf, 3, 4)))
size_t snprintf_continue(char **s, size_t *sz, const char *fmt, ...)
{
size_t n, written, freesz = *sz;
va_list arg;
va_start(arg, fmt);
n = vsnprintf(*s, freesz, fmt, arg);
va_end(arg);
if (n >= freesz) {
/* sz limited, only (freesz - 1) bytes is written */
written = freesz - 1;
} else {
written = n;
}
*s = *s + written;
*sz -= written;
return n;
}
ssize_t argv_buffer_split_with_delim(char *cmdlines, const char *delim,
char **argv, size_t sz_argv)
{
char *s, *save_ptr;
size_t argc = 0;
if (!cmdlines || !delim || !argv || sz_argv == 0)
return 0;
s = strtok_r(cmdlines, delim, &save_ptr);
if (!s)
goto done;
argv[argc++] = s;
while (argc < sz_argv - 1) {
s = strtok_r(NULL, delim, &save_ptr);
if (!s)
break;
argv[argc++] = s;
}
done:
argv[argc] = NULL;
return argc;
}