-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_string.c
More file actions
277 lines (263 loc) · 5.13 KB
/
Copy pathaws_string.c
File metadata and controls
277 lines (263 loc) · 5.13 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Copyright (c) 2017 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aws_string.h"
#include "attributes.h"
static size_t aws_join_buflen_(size_t delim_strlen, char **list) PURE;
static char *aws_timenf_(const char *format, size_t length, const time_t *date) FORMAT_TIME_1 ALLOC_2 MALLOC;
static char *aws_vasprintf_(const char * format, va_list args);
/**
* returns a newly allocated string, or NULL on failure
*/
char *
aws_trim(char c, char * const str)
{
char *src = str;
char *dst;
size_t len;
if(!str)
{
return errno = EINVAL, NULL;
}
while(*src == c)
{
src++; /* ignore leading chars */
}
len = strlen(src);
while(len && *(src+len) == c)
{
len--; /* reduce length if trailing chars match */
}
dst = malloc(len + 1);
if(!dst)
{
return errno = ENOMEM, NULL;
}
(void) memcpy(dst, src, len);
*(dst+len) = '\0';
return dst;
}
/**
* returns a newly allocated string, or NULL on failure
*/
char *
aws_collapse(char c, char * const str)
{
char *src = str;
char *dst, *result;
if(!str)
{
return errno = EINVAL, NULL;
}
result = dst = malloc(strlen(str) + 1);
if(!dst)
{
return errno = ENOMEM, NULL;
}
for(; (*dst++ = *src); src++)
{
if(*src == c)
{
while(*(src + 1) == c)
{
src++;
}
}
}
*dst = '\0'; /* don't bother to realloc the buffer */
return result;
}
char *
aws_strtolower_inplace(char * const str)
{
char *c = str;
if(!str)
{
return errno = EINVAL, NULL;
}
while(*c)
{
*c = tolower(*c);
c++;
}
return str;
}
/**
* list argument can be NULL
* returns a newly allocated string, or NULL on failure
*/
char *
aws_join_char(char delim, char ** const list)
{
char **strs = list;
const size_t length = aws_join_buflen_(1, strs);
if(!length)
{
return strdup("");
}
char *s = malloc(length), *ptr = s;
if(!s)
{
return errno = ENOMEM, NULL;
}
while(*strs)
{
if(ptr != s)
{
*ptr++ = delim;
}
ptr = aws_stradd(ptr, *strs++);
}
return s;
}
/**
* list can be NULL
*/
static size_t
aws_join_buflen_(const size_t delim_strlen, char ** const list)
{
char **strs = list;
size_t length = 0;
while(*strs)
{
length += strlen(*strs++) + delim_strlen;
/* remove last delimiter and add terminating byte */
if(!strs)
{
length -= delim_strlen - 1;
}
}
return length;
}
char *
aws_stradd(char * const dst, char * const src)
{
#ifdef HAVE_STPCPY
return stpcpy(dst, src);
#else
return strchr(strcpy(dst, src), '\0');
#endif
}
/**
* returns a newly allocated string, or NULL on failure
*/
char *
aws_strf(const char * const format, ...)
{
char *s;
va_list args;
(void) va_start(args, format);
#ifdef HAVE_VASPRINTF
if(vasprintf(&s, format, args) == -1)
{
s = NULL;
}
#else
s = aws_vasprintf_(format, args);
#endif
(void) va_end(args);
return s;
}
static char *
aws_vasprintf_(const char * const format, va_list args)
{
const size_t assumed_to_be_big_enough = 128 * 1024;
char *s = malloc(assumed_to_be_big_enough), *tmp;
int r;
if(!s)
{
return NULL;
}
# ifdef HAVE_VSNPRINTF
r = vsnprintf(s, assumed_to_be_big_enough, format, args);
# else
r = vsprintf(s, format, args);
# endif
if(r < 0)
{
free(s);
return NULL;
}
*(s + r) = '\0';
/* attempt to truncate ptr to used length, or just carry on if realloc fails */
tmp = realloc(s, r + 1);
if(tmp)
{
s = tmp;
}
return s;
}
/**
* returns a newly allocated string, or NULL on failure
*/
char *
aws_timef(const char * const format, const time_t * const time)
{
/* TODO: it's hard to compute how much space strftime() will need */
return aws_timenf_(format, 1024, time);
}
/**
* pass in the maximum length of the output string including terminating byte
* returns a newly allocated string, or NULL on failure
*/
static char *
aws_timenf_(const char * const format, const size_t length, const time_t * const time)
{
struct tm brokentime;
if(!format || !time)
{
return errno = EINVAL, NULL;
}
if(!gmtime_r(time, &brokentime))
{
return NULL;
}
return aws_brokentimenf(format, length, &brokentime);
}
/**
* pass in the maximum length of the output string including terminating byte
* returns a newly allocated string, or NULL on failure
*/
char *
aws_brokentimenf(const char * const format, const size_t length, struct tm * const brokentime)
{
char *str;
if(!format || !brokentime)
{
return errno = EINVAL, NULL;
}
str = calloc(1, length);
if(!str)
{
return errno = ENOMEM, NULL;
}
if(!strftime(str, length, format, brokentime))
{
free(str);
return NULL;
}
return str;
}
int
aws_strempty(char *str)
{
return (!str || strcmp(str, "") == 0);
}