Skip to content

Commit 2a9535b

Browse files
committed
MSVC improvements
1 parent 475d5e3 commit 2a9535b

6 files changed

Lines changed: 143 additions & 117 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ jobs:
3434
3535
int main(void) {
3636
char* x = NULL;
37-
int len = asprintf(&x, "hello %s", "world");
38-
if (len > 0 && x != NULL) {
37+
int len = 0;
38+
int rc = c89stringutils_asprintf(&x, &len, "hello %s", "world");
39+
if (rc == 0 && x != NULL) {
3940
printf("%s\n", x);
4041
free(x);
4142
return 0;

c89stringutils/c89stringutils_string_extras.c

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
115115

116116
#endif /* !HAVE_SNPRINTF_H */
117117

118-
#ifndef HAVE_STRNCASECMP_H
119-
120118
#define HAVE_STRNCASECMP_H
121119

122120
/**
@@ -127,15 +125,19 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
127125
* @return An integer less than, equal to, or greater than zero if s1 is found,
128126
* respectively, to be less than, to match, or be greater than s2.
129127
*/
130-
C89STRINGUTILS_EXPORT int strncasecmp(const char *s1, const char *s2,
131-
size_t n) {
132-
int rc;
133-
if (s1 == NULL || s2 == NULL) {
134-
LOG_DEBUG("s1 or s2 is NULL");
128+
C89STRINGUTILS_EXPORT int c89stringutils_strncasecmp(const char *s1,
129+
const char *s2, size_t n) {
130+
if (s1 == s2)
131+
return 0;
132+
if (s1 == NULL)
135133
return -1;
136-
}
137-
rc = _strnicmp(s1, s2, n);
138-
return rc;
134+
if (s2 == NULL)
135+
return 1;
136+
#if defined(_MSC_VER)
137+
return _strnicmp(s1, s2, n);
138+
#else
139+
return strncasecmp(s1, s2, n);
140+
#endif
139141
}
140142

141143
/**
@@ -145,19 +147,21 @@ C89STRINGUTILS_EXPORT int strncasecmp(const char *s1, const char *s2,
145147
* @return An integer less than, equal to, or greater than zero if s1 is found,
146148
* respectively, to be less than, to match, or be greater than s2.
147149
*/
148-
C89STRINGUTILS_EXPORT int strcasecmp(const char *s1, const char *s2) {
149-
int rc;
150-
if (s1 == NULL || s2 == NULL) {
151-
LOG_DEBUG("s1 or s2 is NULL");
150+
C89STRINGUTILS_EXPORT int c89stringutils_strcasecmp(const char *s1,
151+
const char *s2) {
152+
if (s1 == s2)
153+
return 0;
154+
if (s1 == NULL)
152155
return -1;
153-
}
154-
rc = _stricmp(s1, s2);
155-
return rc;
156+
if (s2 == NULL)
157+
return 1;
158+
#if defined(_MSC_VER)
159+
return _stricmp(s1, s2);
160+
#else
161+
return strcasecmp(s1, s2);
162+
#endif
156163
}
157164

158-
#endif /* !HAVE_STRNCASECMP_H */
159-
160-
#ifndef HAVE_STRNSTR
161165
#define HAVE_STRNSTR
162166

163167
/**
@@ -169,8 +173,9 @@ C89STRINGUTILS_EXPORT int strcasecmp(const char *s1, const char *s2) {
169173
* @return A pointer to the first occurrence of little in big, or NULL if not
170174
* found.
171175
*/
172-
C89STRINGUTILS_EXPORT char *strnstr(const char *buffer, const char *target,
173-
size_t bufferLength) {
176+
C89STRINGUTILS_EXPORT char *c89stringutils_strnstr(const char *buffer,
177+
const char *target,
178+
size_t bufferLength) {
174179
size_t targetLength;
175180
const char *start;
176181
size_t remaining;
@@ -180,8 +185,9 @@ C89STRINGUTILS_EXPORT char *strnstr(const char *buffer, const char *target,
180185
return NULL;
181186
}
182187
targetLength = strlen(target);
183-
if (targetLength == 0)
188+
if (targetLength == 0) {
184189
return (char *)buffer;
190+
}
185191
remaining = bufferLength;
186192
for (start = buffer; *start && remaining >= targetLength;
187193
start++, remaining--) {
@@ -192,13 +198,9 @@ C89STRINGUTILS_EXPORT char *strnstr(const char *buffer, const char *target,
192198
}
193199
}
194200
}
195-
return 0;
201+
return NULL;
196202
}
197203

198-
#endif /* !HAVE_STRNSTR */
199-
200-
#ifndef HAVE_STRCASESTR_H
201-
202204
#define HAVE_STRCASESTR_H
203205

204206
/* `strcasestr` from MUSL */
@@ -210,28 +212,27 @@ C89STRINGUTILS_EXPORT char *strnstr(const char *buffer, const char *target,
210212
* @return A pointer to the first occurrence of little in big, or NULL if not
211213
* found.
212214
*/
213-
C89STRINGUTILS_EXPORT char *strcasestr(const char *h, const char *n) {
215+
C89STRINGUTILS_EXPORT char *c89stringutils_strcasestr(const char *h,
216+
const char *n) {
214217
size_t l;
215-
int rc;
218+
int cmp;
216219
if (h == NULL || n == NULL) {
217220
LOG_DEBUG("h or n is NULL");
218221
return NULL;
219222
}
220223
l = strlen(n);
221-
if (l == 0)
224+
if (l == 0) {
222225
return (char *)h;
226+
}
223227
for (; *h; h++) {
224-
rc = strncasecmp(h, n, l);
225-
if (rc == 0)
228+
cmp = c89stringutils_strncasecmp(h, n, l);
229+
if (cmp == 0) {
226230
return (char *)h;
231+
}
227232
}
228-
return 0;
233+
return NULL;
229234
}
230235

231-
#endif /* !HAVE_STRCASESTR_H */
232-
233-
#ifndef HAVE_STRERRORLEN_S
234-
235236
#define HAVE_STRERRORLEN_S
236237
/* MIT licensed function from Safe C Library */
237238

@@ -240,7 +241,7 @@ C89STRINGUTILS_EXPORT char *strcasestr(const char *h, const char *n) {
240241
* @param errnum The error number.
241242
* @return The length of the string describing the error.
242243
*/
243-
C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum) {
244+
C89STRINGUTILS_EXPORT size_t c89stringutils_strerrorlen_s(errno_t errnum) {
244245
#ifndef ESNULLP
245246
#define ESNULLP (400) /* null ptr */
246247
#endif
@@ -268,7 +269,7 @@ C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum) {
268269
};
269270

270271
if (errnum >= ESNULLP && errnum <= ESLAST) {
271-
return len_errmsgs_s[errnum - ESNULLP] - 1;
272+
return (size_t)(len_errmsgs_s[errnum - ESNULLP] - 1);
272273
} else {
273274
#ifdef _MSC_VER
274275
char errbuf[256];
@@ -287,9 +288,6 @@ C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum) {
287288
}
288289
}
289290

290-
#endif /* !HAVE_STRERRORLEN_S */
291-
292-
#ifndef HAVE_ASPRINTF
293291
#define HAVE_ASPRINTF
294292

295293
#ifndef VA_COPY
@@ -315,7 +313,8 @@ C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum) {
315313
* @param ap The va_list of arguments.
316314
* @return The number of characters printed, or -1 on error.
317315
*/
318-
C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
316+
C89STRINGUTILS_EXPORT int c89stringutils_vasprintf(char **str, const char *fmt,
317+
va_list ap) {
319318
int rc;
320319
va_list ap2;
321320
char *string, *newstr;
@@ -333,7 +332,11 @@ C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
333332
}
334333

335334
VA_COPY(ap2, ap);
335+
#if defined(_MSC_VER)
336+
rc = vsnprintf_s(string, INIT_SZ, _TRUNCATE, fmt, ap2);
337+
#else
336338
rc = vsnprintf(string, INIT_SZ, fmt, ap2);
339+
#endif
337340
va_end(ap2);
338341
if (rc >= 0 && rc < INIT_SZ) { /* succeeded with initial alloc */
339342
*str = string;
@@ -350,7 +353,11 @@ C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
350353
goto fail;
351354
}
352355
VA_COPY(ap2, ap);
356+
#if defined(_MSC_VER)
357+
rc = vsnprintf_s(newstr, len, _TRUNCATE, fmt, ap2);
358+
#else
353359
rc = vsnprintf(newstr, len, fmt, ap2);
360+
#endif
354361
va_end(ap2);
355362
if (rc < 0 || (size_t)rc >= len) { /* failed with realloc'ed string */
356363
free(newstr);
@@ -381,7 +388,7 @@ C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
381388
}
382389
*str = NULL;
383390
errno = ENOMEM;
384-
return rc;
391+
return -1;
385392
}
386393

387394
/**
@@ -392,7 +399,8 @@ C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
392399
* @param ... The arguments.
393400
* @return The number of characters printed, or -1 on error.
394401
*/
395-
C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...) {
402+
C89STRINGUTILS_EXPORT int c89stringutils_asprintf(char **str, const char *fmt,
403+
...) {
396404
int rc;
397405
va_list ap;
398406

@@ -403,7 +411,7 @@ C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...) {
403411

404412
*str = NULL;
405413
va_start(ap, fmt);
406-
rc = vasprintf(str, fmt, ap);
414+
rc = c89stringutils_vasprintf(str, fmt, ap);
407415
va_end(ap);
408416

409417
if (rc < 0) {
@@ -426,20 +434,18 @@ C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...) {
426434
return rc;
427435
}
428436

429-
#endif /* !HAVE_ASPRINTF */
430-
431-
#ifndef HAVE_JASPRINTF
432437
#define HAVE_JASPRINTF
433438
/**
434439
* @brief `jasprintf`, a version of `asprintf` that concatenates on successive
435-
* calls: char *s = NULL; jasprintf(&s, "foo%s", "bar"); jasprintf(&s, "can%s",
436-
* "haz"); free(s);
440+
* calls: char *s = NULL; c89stringutils_jasprintf(&s, "foo%s", "bar");
441+
* c89stringutils_jasprintf(&s, "can%s", "haz"); free(s);
437442
* @param unto The string to append to.
438443
* @param fmt The format string.
439444
* @param ... The arguments.
440445
* @return The concatenated string.
441446
*/
442-
C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...) {
447+
C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto, const char *fmt,
448+
...) {
443449
va_list args;
444450
size_t base_length;
445451
int length;
@@ -507,7 +513,6 @@ C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...) {
507513

508514
return 0;
509515
}
510-
#endif /* !HAVE_JASPRINTF */
511516

512517
#if defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__)
513518
#pragma GCC diagnostic pop

c89stringutils/c89stringutils_string_extras.h

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ typedef int errno_t;
119119
#endif /* HAVE_STRINGS_H */
120120
/* clang-format on */
121121

122-
#ifndef HAVE_STRNCASECMP_H
123-
124122
/**
125123
* @brief Compare at most n characters of two strings, ignoring case.
126124
* @param s1 The first string to compare.
@@ -129,8 +127,8 @@ typedef int errno_t;
129127
* @return An integer less than, equal to, or greater than zero if s1 is found,
130128
* respectively, to be less than, to match, or be greater than s2.
131129
*/
132-
extern C89STRINGUTILS_EXPORT int strncasecmp(const char *s1, const char *s2,
133-
size_t n);
130+
extern C89STRINGUTILS_EXPORT int
131+
c89stringutils_strncasecmp(const char *s1, const char *s2, size_t n);
134132

135133
/**
136134
* @brief Compare two strings, ignoring case.
@@ -139,11 +137,8 @@ extern C89STRINGUTILS_EXPORT int strncasecmp(const char *s1, const char *s2,
139137
* @return An integer less than, equal to, or greater than zero if s1 is found,
140138
* respectively, to be less than, to match, or be greater than s2.
141139
*/
142-
extern C89STRINGUTILS_EXPORT int strcasecmp(const char *s1, const char *s2);
143-
144-
#endif /* !HAVE_STRNCASECMP_H */
145-
146-
#ifndef HAVE_STRNSTR
140+
extern C89STRINGUTILS_EXPORT int c89stringutils_strcasecmp(const char *s1,
141+
const char *s2);
147142

148143
/**
149144
* @brief Locate a substring in a string, looking at no more than len
@@ -154,12 +149,9 @@ extern C89STRINGUTILS_EXPORT int strcasecmp(const char *s1, const char *s2);
154149
* @return A pointer to the first occurrence of little in big, or NULL if not
155150
* found.
156151
*/
157-
extern C89STRINGUTILS_EXPORT char *
158-
strnstr(const char *buffer, const char *target, size_t bufferLength);
159-
160-
#endif /* !HAVE_STRNSTR */
161-
162-
#ifndef HAVE_STRCASESTR_H
152+
extern C89STRINGUTILS_EXPORT char *c89stringutils_strnstr(const char *buffer,
153+
const char *target,
154+
size_t bufferLength);
163155

164156
/**
165157
* @brief Locate a substring in a string, ignoring case.
@@ -168,22 +160,16 @@ strnstr(const char *buffer, const char *target, size_t bufferLength);
168160
* @return A pointer to the first occurrence of little in big, or NULL if not
169161
* found.
170162
*/
171-
extern C89STRINGUTILS_EXPORT char *strcasestr(const char *h, const char *n);
172-
173-
#endif /* !HAVE_STRCASESTR_H */
174-
175-
#ifndef HAVE_STRERRORLEN_S
163+
extern C89STRINGUTILS_EXPORT char *c89stringutils_strcasestr(const char *h,
164+
const char *n);
176165

177166
/**
178167
* @brief Get the length of a string describing an error number.
179168
* @param errnum The error number.
180169
* @return The length of the string describing the error.
181170
*/
182-
extern C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum);
183-
184-
#endif /* !HAVE_STRERRORLEN_S */
185-
186-
#ifndef HAVE_ASPRINTF
171+
extern C89STRINGUTILS_EXPORT size_t
172+
c89stringutils_strerrorlen_s(errno_t errnum);
187173

188174
/**
189175
* @brief Write formatted output to a dynamically allocated string using a
@@ -194,8 +180,8 @@ extern C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum);
194180
* @param ap The va_list of arguments.
195181
* @return The number of characters printed, or -1 on error.
196182
*/
197-
extern C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt,
198-
va_list ap);
183+
extern C89STRINGUTILS_EXPORT int
184+
c89stringutils_vasprintf(char **str, const char *fmt, va_list ap);
199185

200186
/**
201187
* @brief Write formatted output to a dynamically allocated string.
@@ -205,24 +191,20 @@ extern C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt,
205191
* @param ... The arguments.
206192
* @return The number of characters printed, or -1 on error.
207193
*/
208-
extern C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...);
209-
210-
#endif /* !HAVE_ASPRINTF */
211-
212-
#ifndef HAVE_JASPRINTF
194+
extern C89STRINGUTILS_EXPORT int c89stringutils_asprintf(char **str,
195+
const char *fmt, ...);
213196

214197
/**
215198
* @brief `jasprintf`, a version of `asprintf` that concatenates on successive
216-
* calls: char *s = NULL; jasprintf(&s, "foo%s", "bar"); jasprintf(&s, "can%s",
217-
* "haz"); free(s);
199+
* calls: char *s = NULL; c89stringutils_jasprintf(&s, "foo%s", "bar");
200+
* c89stringutils_jasprintf(&s, "can%s", "haz"); free(s);
218201
* @param unto The string to append to.
219202
* @param fmt The format string.
220203
* @param ... The arguments.
221204
* @return The concatenated string.
222205
*/
223-
extern C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...);
224-
225-
#endif /* !HAVE_JASPRINTF */
206+
extern C89STRINGUTILS_EXPORT int c89stringutils_jasprintf(char **unto,
207+
const char *fmt, ...);
226208

227209
#ifdef __cplusplus
228210
}

0 commit comments

Comments
 (0)