diff --git a/clib.json b/clib.json index ded5b77..8ef6699 100644 --- a/clib.json +++ b/clib.json @@ -1,6 +1,6 @@ { "name": "libutil", - "version": "0.0.13", + "version": "0.1.13", "author": "Matthew Zito", "repo": "exbotanical/libutil", "license": "MIT", diff --git a/include/libutil.h b/include/libutil.h index a71fd39..fdc8a22 100644 --- a/include/libutil.h +++ b/include/libutil.h @@ -229,7 +229,7 @@ void buffer_free(buffer_t *buf); /** * Returns a formatted string. Uses printf syntax. */ -char *fmt_str(char *fmt, ...); +char *s_fmt(char *fmt, ...); /** * s_truncate truncates the given string `s` by `n` characters. diff --git a/src/fmt.c b/src/fmt.c deleted file mode 100644 index 040b945..0000000 --- a/src/fmt.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include - -#include "libutil.h" - -char *fmt_str(char *fmt, ...) { - va_list args, args_cp; - va_start(args, fmt); - va_copy(args_cp, args); - - // Pass length of zero first to determine number of bytes needed - unsigned int n = vsnprintf(NULL, 0, fmt, args) + 1; - char *buf = malloc(n); - if (!buf) { - return NULL; - } - - vsnprintf(buf, n, fmt, args_cp); - - va_end(args); - va_end(args_cp); - - return buf; -} diff --git a/src/str.c b/src/str.c index dcac53c..8a7312a 100644 --- a/src/str.c +++ b/src/str.c @@ -1,4 +1,5 @@ #include // for toupper +#include #include // for snprintf #include #include @@ -212,3 +213,23 @@ array_t *s_split(const char *s, const char *delim) { return tokens; } + +char *s_fmt(char *fmt, ...) { + va_list args, args_cp; + va_start(args, fmt); + va_copy(args_cp, args); + + // Pass length of zero first to determine number of bytes needed + unsigned int n = vsnprintf(NULL, 0, fmt, args) + 1; + char *buf = malloc(n); + if (!buf) { + return NULL; + } + + vsnprintf(buf, n, fmt, args_cp); + + va_end(args); + va_end(args_cp); + + return buf; +} diff --git a/t/fmt_test.c b/t/fmt_test.c deleted file mode 100644 index 776fdd1..0000000 --- a/t/fmt_test.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "libutil.h" -#include "tap.c/tap.h" -#include "tests.h" - -void test_fmt_str(void) { - char *formatted = fmt_str("%s %d %s", "test", 11, "string"); - is(formatted, "test 11 string", "formats each part into a single string"); - - free(formatted); -} - -void run_fmt_tests(void) { test_fmt_str(); } diff --git a/t/main.c b/t/main.c index fe0096b..3f30e47 100644 --- a/t/main.c +++ b/t/main.c @@ -6,7 +6,6 @@ int main() { run_array_tests(); run_buffer_tests(); - run_fmt_tests(); run_str_tests(); done_testing(); diff --git a/t/str_test.c b/t/str_test.c index f7ee703..c3d485d 100644 --- a/t/str_test.c +++ b/t/str_test.c @@ -290,6 +290,13 @@ void test_s_split_end_match(void) { array_free_ptrs(paths); } +void test_s_fmt(void) { + char *formatted = s_fmt("%s %d %s", "test", 11, "string"); + is(formatted, "test 11 string", "formats each part into a single string"); + + free(formatted); +} + void run_str_tests(void) { test_s_copy(); @@ -325,4 +332,6 @@ void run_str_tests(void) { test_s_split_no_match(); test_s_split_empty_input(); test_s_split_end_match(); + + test_s_fmt(); } diff --git a/t/tests.h b/t/tests.h index eb05599..1700852 100644 --- a/t/tests.h +++ b/t/tests.h @@ -3,7 +3,6 @@ void run_array_tests(void); void run_buffer_tests(void); -void run_fmt_tests(void); void run_str_tests(void); #endif /* TESTS_H */