diff options
author | axtlos <axtlos@disroot.org> | 2024-09-27 21:31:47 +0200 |
---|---|---|
committer | axtlos <axtlos@disroot.org> | 2024-09-27 21:31:47 +0200 |
commit | b2182396df5d99ff5c26161aaffa5ced879742c5 (patch) | |
tree | c848fc157ab55c63b49b063d212c1671fa41afae | |
parent | 7aa261f8d57cb53cc334f6bf1920a6f8517ff054 (diff) | |
download | extlib-b2182396df5d99ff5c26161aaffa5ced879742c5.tar.gz extlib-b2182396df5d99ff5c26161aaffa5ced879742c5.tar.bz2 |
Fix join_str on musl and add manpage
-rw-r--r-- | doc/join_str.3 | 35 | ||||
-rw-r--r-- | src/extlib.h | 2 | ||||
-rw-r--r-- | src/extstring.c | 17 | ||||
-rw-r--r-- | tests/driver.c | 2 | ||||
-rw-r--r-- | tests/test_join_str.c | 4 |
5 files changed, 49 insertions, 11 deletions
diff --git a/doc/join_str.3 b/doc/join_str.3 new file mode 100644 index 0000000..3c778ae --- /dev/null +++ b/doc/join_str.3 @@ -0,0 +1,35 @@ +'\" t +.\" Copyright 2024 axtlos (axtlos@disroot.org) +.\" +.\" SPDX-License-Identifier: BSD-3-Clause + +.TH trim 3 2024-09-27 "extlib" +.SH NAME +join_str \- join an array of strings seperated by a given delimiter +.SH LIBRARY +extlib extended standard library +.RI ( libextlib ", " \-lextlib ) +.SH SYNOPSIS +.nf +.B #include <extlib.h> +.P +.BI "char *join_str (char **s, size_t len, char *delim);" +.P +.fi +.SH DESCRIPTION +The +.BR join_str () +function joins the first +.I len +elements of +.I **s +into one string where each element of the array is seperated by +.IR *delim . +.fi +.SH RETURN VALUE +.BR join_str () +returns the joined string. +.SH AUTHORS +This manual page was written by Rose +.IR <axtlos@disroot.org> . +.\" SRC END diff --git a/src/extlib.h b/src/extlib.h index c36b919..03bb53b 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -66,7 +66,7 @@ char *trim (char *s, int *rem_front, int *rem_back); char *replace_str (char *s, char *old, char *replace); /// Join an array of strings into one string -char *join_str (char **s, size_t len, char delim); +char *join_str (char **s, size_t len, char *delim); /// Limit a value to a maximum float max (float v, float max_v); diff --git a/src/extstring.c b/src/extstring.c index 2fea204..b69b1f7 100644 --- a/src/extstring.c +++ b/src/extstring.c @@ -103,18 +103,21 @@ replace_str (char *s, char *old, char *replace) }; char * -join_str (char **s, size_t len, char delim) +join_str (char **s, size_t len, char *delim) { char *ret = strdup (s[0]); - size_t ret_len = sizeof (char) * len + strlen (ret); + size_t ret_len = strlen (delim) * len + strlen (ret) + 1; + size_t old_ret_len = ret_len; for (int i = 1; i < len; i++) { - char *ret_tmp = realloc (ret, ret_len + strlen (s[i]) + 1); - ret_len += strlen (s[i]) + 1; - if (ret_tmp != NULL) - ret = ret_tmp; - sprintf (ret, "%s%c%s", ret, delim, s[i]); + char *ret_tmp = malloc (ret_len + strlen (s[i])); // i should use realloc here but that breaks on musl so malloc it is :3 + ret_len += strlen (s[i]) + 1; + if (ret_tmp != NULL) { + sprintf (ret_tmp, "%s%s%s", ret, delim, s[i]); + free_secure ((void **) &ret, strlen (ret)); + ret = ret_tmp; + } } return ret; diff --git a/tests/driver.c b/tests/driver.c index 235d598..d3739b5 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -21,7 +21,7 @@ test_runner (struct test_t *test, int n) if (test_result == 0) printf ("Test Case %d: %s -- %sSUCCESS%s\n\n", n, test->test_name, success, reset); else - printf ("Test Case %d: %s -- %sFAIL %d%s\n\n", n, test->test_name, test_result, fail, reset); + printf ("Test Case %d: %s -- %sFAIL %d%s\n\n", n, test->test_name, fail, test_result, reset); } void diff --git a/tests/test_join_str.c b/tests/test_join_str.c index 711c864..f756b2e 100644 --- a/tests/test_join_str.c +++ b/tests/test_join_str.c @@ -11,9 +11,9 @@ test_join_str () for (int i = 0; i < 3; i++) test_string[i] = strdup ("hello"); - char *joined_string = join_str (test_string, 3, ' '); + char *joined_string = join_str (test_string, 3, " && "); - if (strcmp (joined_string, "hello hello hello") != 0) + if (strcmp (joined_string, "hello && hello && hello") != 0) errno = 1; free_secure ((void **) &test_string[0], strlen (test_string[0])); |