1- /*
2- * string functions helpful on Linux (and sometimes BSD)
1+ /**
2+ * @file c89stringutils_string_extras.c
3+ * @brief string functions helpful on Linux (and sometimes BSD)
34 * are now made available on other platforms (Windows, SunOS, &etc.)
4- * * /
5+ */
56
67/* clang-format off */
78#include "c89stringutils_string_extras.h"
2021#pragma GCC diagnostic ignored "-Wnonnull-compare"
2122#endif
2223
24+ /**
25+ * @brief Log debug message
26+ * @param fmt The format string.
27+ * @param ... The arguments.
28+ */
2329void c89stringutils_log_debug (const char * fmt , ...) {
2430 int rc ;
2531 va_list args ;
@@ -61,6 +67,14 @@ void c89stringutils_log_debug(const char *fmt, ...) {
6167#define _vsnprintf vsnprintf
6268#endif /* ANY_BSD */
6369
70+ /**
71+ * @brief Implement vsnprintf for platforms that don't have it.
72+ * @param buffer The buffer to write to.
73+ * @param count The maximum number of characters to write.
74+ * @param format The format string.
75+ * @param args The va_list of arguments.
76+ * @return The number of characters written, or a negative value on error.
77+ */
6478static int wtf_vsnprintf (char * buffer , size_t count , const char * format ,
6579 va_list args ) {
6680 int rc ;
@@ -96,6 +110,14 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
96110
97111#define HAVE_STRNCASECMP_H
98112
113+ /**
114+ * @brief Compare at most n characters of two strings, ignoring case.
115+ * @param s1 The first string to compare.
116+ * @param s2 The second string to compare.
117+ * @param n The maximum number of characters to compare.
118+ * @return An integer less than, equal to, or greater than zero if s1 is found,
119+ * respectively, to be less than, to match, or be greater than s2.
120+ */
99121int strncasecmp (const char * s1 , const char * s2 , size_t n ) {
100122 int rc ;
101123 if (s1 == NULL || s2 == NULL ) {
@@ -106,6 +128,13 @@ int strncasecmp(const char *s1, const char *s2, size_t n) {
106128 return rc ;
107129}
108130
131+ /**
132+ * @brief Compare two strings, ignoring case.
133+ * @param s1 The first string to compare.
134+ * @param s2 The second string to compare.
135+ * @return An integer less than, equal to, or greater than zero if s1 is found,
136+ * respectively, to be less than, to match, or be greater than s2.
137+ */
109138int strcasecmp (const char * s1 , const char * s2 ) {
110139 int rc ;
111140 if (s1 == NULL || s2 == NULL ) {
@@ -121,6 +150,15 @@ int strcasecmp(const char *s1, const char *s2) {
121150#ifndef HAVE_STRNSTR
122151#define HAVE_STRNSTR
123152
153+ /**
154+ * @brief Locate a substring in a string, looking at no more than len
155+ * characters.
156+ * @param buffer The string to search.
157+ * @param target The substring to find.
158+ * @param bufferLength The maximum number of characters to search.
159+ * @return A pointer to the first occurrence of little in big, or NULL if not
160+ * found.
161+ */
124162char * strnstr (const char * buffer , const char * target , size_t bufferLength ) {
125163 size_t targetLength ;
126164 const char * start ;
@@ -154,6 +192,13 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) {
154192
155193/* `strcasestr` from MUSL */
156194
195+ /**
196+ * @brief Locate a substring in a string, ignoring case.
197+ * @param h The string to search.
198+ * @param n The substring to find.
199+ * @return A pointer to the first occurrence of little in big, or NULL if not
200+ * found.
201+ */
157202char * strcasestr (const char * h , const char * n ) {
158203 size_t l ;
159204 int rc ;
@@ -179,6 +224,11 @@ char *strcasestr(const char *h, const char *n) {
179224#define HAVE_STRERRORLEN_S
180225/* MIT licensed function from Safe C Library */
181226
227+ /**
228+ * @brief Get the length of a string describing an error number.
229+ * @param errnum The error number.
230+ * @return The length of the string describing the error.
231+ */
182232size_t strerrorlen_s (errno_t errnum ) {
183233#ifndef ESNULLP
184234#define ESNULLP (400) /* null ptr */
@@ -245,6 +295,15 @@ size_t strerrorlen_s(errno_t errnum) {
245295
246296#define INIT_SZ 128
247297
298+ /**
299+ * @brief Write formatted output to a dynamically allocated string using a
300+ * va_list.
301+ * @param str A pointer to a string pointer where the allocated string will be
302+ * stored.
303+ * @param fmt The format string.
304+ * @param ap The va_list of arguments.
305+ * @return The number of characters printed, or -1 on error.
306+ */
248307extern int vasprintf (char * * str , const char * fmt , va_list ap ) {
249308 int rc ;
250309 va_list ap2 ;
@@ -314,6 +373,14 @@ extern int vasprintf(char **str, const char *fmt, va_list ap) {
314373 return rc ;
315374}
316375
376+ /**
377+ * @brief Write formatted output to a dynamically allocated string.
378+ * @param str A pointer to a string pointer where the allocated string will be
379+ * stored.
380+ * @param fmt The format string.
381+ * @param ... The arguments.
382+ * @return The number of characters printed, or -1 on error.
383+ */
317384extern int asprintf (char * * str , const char * fmt , ...) {
318385 int rc ;
319386 va_list ap ;
@@ -352,6 +419,15 @@ extern int asprintf(char **str, const char *fmt, ...) {
352419
353420#ifndef HAVE_JASPRINTF
354421#define HAVE_JASPRINTF
422+ /**
423+ * @brief `jasprintf`, a version of `asprintf` that concatenates on successive
424+ * calls: char *s = NULL; jasprintf(&s, "foo%s", "bar"); jasprintf(&s, "can%s",
425+ * "haz"); free(s);
426+ * @param unto The string to append to.
427+ * @param fmt The format string.
428+ * @param ... The arguments.
429+ * @return The concatenated string.
430+ */
355431char * jasprintf (char * * unto , const char * fmt , ...) {
356432 va_list args ;
357433 size_t base_length ;
0 commit comments