Skip to content

Commit 475d5e3

Browse files
committed
Adhere to my quality standards and MSVC fixes
1 parent 7b7f9b4 commit 475d5e3

3 files changed

Lines changed: 29 additions & 29 deletions

File tree

c89stringutils/c89stringutils_string_extras.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @param fmt The format string.
2727
* @param ... The arguments.
2828
*/
29-
void c89stringutils_log_debug(const char *fmt, ...) {
29+
C89STRINGUTILS_EXPORT void c89stringutils_log_debug(const char *fmt, ...) {
3030
int rc;
3131
va_list args;
3232
va_start(args, fmt);
@@ -127,7 +127,8 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
127127
* @return An integer less than, equal to, or greater than zero if s1 is found,
128128
* respectively, to be less than, to match, or be greater than s2.
129129
*/
130-
int strncasecmp(const char *s1, const char *s2, size_t n) {
130+
C89STRINGUTILS_EXPORT int strncasecmp(const char *s1, const char *s2,
131+
size_t n) {
131132
int rc;
132133
if (s1 == NULL || s2 == NULL) {
133134
LOG_DEBUG("s1 or s2 is NULL");
@@ -144,7 +145,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n) {
144145
* @return An integer less than, equal to, or greater than zero if s1 is found,
145146
* respectively, to be less than, to match, or be greater than s2.
146147
*/
147-
int strcasecmp(const char *s1, const char *s2) {
148+
C89STRINGUTILS_EXPORT int strcasecmp(const char *s1, const char *s2) {
148149
int rc;
149150
if (s1 == NULL || s2 == NULL) {
150151
LOG_DEBUG("s1 or s2 is NULL");
@@ -168,7 +169,8 @@ int strcasecmp(const char *s1, const char *s2) {
168169
* @return A pointer to the first occurrence of little in big, or NULL if not
169170
* found.
170171
*/
171-
char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
172+
C89STRINGUTILS_EXPORT char *strnstr(const char *buffer, const char *target,
173+
size_t bufferLength) {
172174
size_t targetLength;
173175
const char *start;
174176
size_t remaining;
@@ -208,7 +210,7 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
208210
* @return A pointer to the first occurrence of little in big, or NULL if not
209211
* found.
210212
*/
211-
char *strcasestr(const char *h, const char *n) {
213+
C89STRINGUTILS_EXPORT char *strcasestr(const char *h, const char *n) {
212214
size_t l;
213215
int rc;
214216
if (h == NULL || n == NULL) {
@@ -238,7 +240,7 @@ char *strcasestr(const char *h, const char *n) {
238240
* @param errnum The error number.
239241
* @return The length of the string describing the error.
240242
*/
241-
size_t strerrorlen_s(errno_t errnum) {
243+
C89STRINGUTILS_EXPORT size_t strerrorlen_s(errno_t errnum) {
242244
#ifndef ESNULLP
243245
#define ESNULLP (400) /* null ptr */
244246
#endif
@@ -313,7 +315,7 @@ size_t strerrorlen_s(errno_t errnum) {
313315
* @param ap The va_list of arguments.
314316
* @return The number of characters printed, or -1 on error.
315317
*/
316-
extern int vasprintf(char **str, const char *fmt, va_list ap) {
318+
C89STRINGUTILS_EXPORT int vasprintf(char **str, const char *fmt, va_list ap) {
317319
int rc;
318320
va_list ap2;
319321
char *string, *newstr;
@@ -390,7 +392,7 @@ extern int vasprintf(char **str, const char *fmt, va_list ap) {
390392
* @param ... The arguments.
391393
* @return The number of characters printed, or -1 on error.
392394
*/
393-
extern int asprintf(char **str, const char *fmt, ...) {
395+
C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...) {
394396
int rc;
395397
va_list ap;
396398

@@ -437,19 +439,19 @@ extern int asprintf(char **str, const char *fmt, ...) {
437439
* @param ... The arguments.
438440
* @return The concatenated string.
439441
*/
440-
char *jasprintf(char **unto, const char *fmt, ...) {
442+
C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...) {
441443
va_list args;
442444
size_t base_length;
443445
int length;
444446
int rc;
445447
char *result;
446448

447-
if (fmt == NULL) {
448-
LOG_DEBUG("fmt is NULL");
449-
return NULL;
449+
if (unto == NULL || fmt == NULL) {
450+
LOG_DEBUG("unto or fmt is NULL");
451+
return -1;
450452
}
451453

452-
base_length = unto && *unto ? strlen(*unto) : 0;
454+
base_length = *unto ? strlen(*unto) : 0;
453455

454456
va_start(args, fmt);
455457
/* check length for failure */
@@ -467,15 +469,14 @@ char *jasprintf(char **unto, const char *fmt, ...) {
467469
va_end(args);
468470

469471
if (length < 0)
470-
return NULL;
472+
return -1;
471473

472474
/* check result for failure */
473-
result =
474-
(char *)realloc(unto ? *unto : NULL, base_length + (size_t)length + 1);
475+
result = (char *)realloc(*unto, base_length + (size_t)length + 1);
475476

476477
if (result == NULL) {
477478
LOG_DEBUG("realloc failed with rc=-1");
478-
return NULL;
479+
return -1;
479480
}
480481

481482
va_start(args, fmt);
@@ -486,28 +487,25 @@ char *jasprintf(char **unto, const char *fmt, ...) {
486487
/* handle error, printing the nonzero exit code for debug purposes */
487488
LOG_DEBUG("vsprintf_s failed with rc=%d", rc);
488489
free(result);
489-
if (unto)
490-
*unto = NULL;
490+
*unto = NULL;
491491
va_end(args);
492-
return NULL;
492+
return -1;
493493
}
494494
#else
495495
rc = vsprintf(result + base_length, fmt, args);
496496
if (rc < 0) {
497497
LOG_DEBUG("vsprintf failed with rc=%d", rc);
498498
free(result);
499-
if (unto)
500-
*unto = NULL;
499+
*unto = NULL;
501500
va_end(args);
502-
return NULL;
501+
return -1;
503502
}
504503
#endif
505504
va_end(args);
506505

507-
if (unto)
508-
*unto = result;
506+
*unto = result;
509507

510-
return result;
508+
return 0;
511509
}
512510
#endif /* !HAVE_JASPRINTF */
513511

c89stringutils/c89stringutils_string_extras.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extern C89STRINGUTILS_EXPORT int asprintf(char **str, const char *fmt, ...);
220220
* @param ... The arguments.
221221
* @return The concatenated string.
222222
*/
223-
extern C89STRINGUTILS_EXPORT char *jasprintf(char **unto, const char *fmt, ...);
223+
extern C89STRINGUTILS_EXPORT int jasprintf(char **unto, const char *fmt, ...);
224224

225225
#endif /* !HAVE_JASPRINTF */
226226

c89stringutils/tests/test_string_extras.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ TEST x_asprintf_should_succeed(void) {
5959
*/
6060
TEST x_jasprintf_should_succeed(void) {
6161
char *s = NULL;
62-
jasprintf(&s, "foo%s", "bar");
63-
jasprintf(&s, "can%s", "haz");
62+
int rc1 = jasprintf(&s, "foo%s", "bar");
63+
int rc2 = jasprintf(&s, "can%s", "haz");
64+
ASSERT_EQ(0, rc1);
65+
ASSERT_EQ(0, rc2);
6466
ASSERT_STR_EQ("foobarcanhaz", s);
6567
free(s);
6668
PASS();

0 commit comments

Comments
 (0)