~donmcc/astr

f4dd4ca4751396f256f3b9dd6c65c265c1a33ef1 — Don McCaughey 1 year, 8 months ago 4b4d0e6
Change "formatted" to "format".

Rename `astr_alloc_formatted()` and `astr_alloc_formatted_args()`
functions to `astr_alloc_format()` and `astr_alloc_format_args()`
respectively.

Also rename related tests and update the readme and man page.
5 files changed, 33 insertions(+), 32 deletions(-)

M README.md
M astr.3
M astr.c
M astr.h
M astr_tests.c
M README.md => README.md +2 -2
@@ 20,12 20,12 @@ responsible for calling [`free()`][1] on the returned pointer.
Allocate an empty string.

    char *
    astr_alloc_formatted(char const *format, ...);
    astr_alloc_format(char const *format, ...);

Allocate a formatted string.

    char *
    astr_alloc_formatted_args(char const *format, va_list arguments);
    astr_alloc_format_args(char const *format, va_list arguments);

Allocate a formatted string using a `va_list` object.


M astr.3 => astr.3 +9 -8
@@ 3,7 3,8 @@
.Os
.Sh NAME
.Nm astr_alloc_empty ,
.Nm astr_alloc_formatted ,
.Nm astr_alloc_format ,
.Nm astr_alloc_format_args ,
.Nm astr_eq_bytes
.Nd functions for working with dynamically allocated strings.
.Sh SYNOPSIS


@@ 11,9 12,9 @@
.Ft char *
.Fn astr_alloc_empty "void"
.Ft char *
.Fn astr_alloc_formatted "char const *format" "..."
.Fn astr_alloc_format "char const *format" "..."
.Ft char *
.Fn astr_alloc_formatted_args "char const *format" "va_list arguments"
.Fn astr_alloc_format_args "char const *format" "va_list arguments"
.Ft bool
.Fn astr_eq_bytes "char const s1" "char const s2"
.Sh DESCRIPTION


@@ 34,7 35,7 @@ in their name; the caller is responsible for calling
.Fn free
on the returned pointer.
Functions with
.Sy _formatted
.Sy _format
in their name take a
.Fa format
string as specified for


@@ 53,9 54,9 @@ object defined in
.Fn astr_alloc_empty
allocates an empty string.
.Pp
.Fn astr_alloc_formatted
.Fn astr_alloc_format
and
.Fn astr_alloc_formatted_args
.Fn astr_alloc_format_args
allocate formatted strings.
.Pp
.Fn astr_eq_bytes


@@ 69,9 70,9 @@ may be
.Sh RETURN VALUES
.Fn astr_alloc_empty
returns a pointer to a zero-length string on success.
.Fn astr_alloc_formatted
.Fn astr_alloc_format
and
.Fn astr_alloc_formatted_args
.Fn astr_alloc_format_args
return a pointer to a formatted string on success.
.Pp
If there is an error, these functions return a

M astr.c => astr.c +3 -3
@@ 9,18 9,18 @@ astr_alloc_empty(void);


char *
astr_alloc_formatted(char const *format, ...)
astr_alloc_format(char const *format, ...)
{
    va_list arguments;
    va_start(arguments, format);
    char *s = astr_alloc_formatted_args(format, arguments);
    char *s = astr_alloc_format_args(format, arguments);
    va_end(arguments);
    return s;
}


char *
astr_alloc_formatted_args(char const *format, va_list arguments)
astr_alloc_format_args(char const *format, va_list arguments)
{
    if (!format) {
        errno = EINVAL;

M astr.h => astr.h +3 -3
@@ 8,15 8,15 @@


char *
astr_alloc_formatted(char const *format, ...);
astr_alloc_format(char const *format, ...);

char *
astr_alloc_formatted_args(char const *format, va_list arguments);
astr_alloc_format_args(char const *format, va_list arguments);

inline char *
astr_alloc_empty(void)
{
    return astr_alloc_formatted("");
    return astr_alloc_format("");
}

inline bool

M astr_tests.c => astr_tests.c +16 -16
@@ 15,9 15,9 @@ test_astr_alloc_empty(void)


static void
test_astr_alloc_formatted(void)
test_astr_alloc_format(void)
{
    char *s = astr_alloc_formatted("answer: %i", 42);
    char *s = astr_alloc_format("answer: %i", 42);
    assert(s);
    assert(10 == strlen(s));
    assert(astr_eq_bytes("answer: 42", s));


@@ 26,10 26,10 @@ test_astr_alloc_formatted(void)


static void
test_astr_alloc_formatted_for_NULL_format(void)
test_astr_alloc_format_for_NULL_format(void)
{
    errno = 0;
    char *s = astr_alloc_formatted(NULL);
    char *s = astr_alloc_format(NULL);
    assert(!s);
    assert(EINVAL == errno);
    free(s);


@@ 37,20 37,20 @@ test_astr_alloc_formatted_for_NULL_format(void)


static char *
helper_alloc_formatted(char const *format, ...)
helper_alloc_format(char const *format, ...)
{
    va_list arguments;
    va_start(arguments, format);
    char *s = astr_alloc_formatted_args(format, arguments);
    char *s = astr_alloc_format_args(format, arguments);
    va_end(arguments);
    return s;
}


static void
test_astr_alloc_formatted_args(void)
test_astr_alloc_format_args(void)
{
    char *s = helper_alloc_formatted("answer: %i", 42);
    char *s = helper_alloc_format("answer: %i", 42);
    assert(s);
    assert(10 == strlen(s));
    assert(astr_eq_bytes("answer: 42", s));


@@ 59,10 59,10 @@ test_astr_alloc_formatted_args(void)


static void
test_astr_alloc_formatted_args_for_NULL_format(void)
test_astr_alloc_format_args_for_NULL_format(void)
{
    errno = 0;
    char *s = helper_alloc_formatted(NULL);
    char *s = helper_alloc_format(NULL);
    assert(!s);
    assert(EINVAL == errno);
    free(s);


@@ 72,7 72,7 @@ test_astr_alloc_formatted_args_for_NULL_format(void)
static void
test_astr_eq_bytes(void)
{
    char *s = astr_alloc_formatted("foo");
    char *s = astr_alloc_format("foo");

    assert(astr_eq_bytes("foo", s));
    assert(!astr_eq_bytes(s, "bar"));


@@ 84,7 84,7 @@ test_astr_eq_bytes(void)
static void
test_astr_eq_bytes_for_NULL_string(void)
{
    char *s = astr_alloc_formatted("foo");
    char *s = astr_alloc_format("foo");

    assert(!astr_eq_bytes(s, NULL));
    assert(!astr_eq_bytes(NULL, s));


@@ 98,10 98,10 @@ int
main(int argc, char *argv[])
{
    test_astr_alloc_empty();
    test_astr_alloc_formatted();
    test_astr_alloc_formatted_for_NULL_format();
    test_astr_alloc_formatted_args();
    test_astr_alloc_formatted_args_for_NULL_format();
    test_astr_alloc_format();
    test_astr_alloc_format_for_NULL_format();
    test_astr_alloc_format_args();
    test_astr_alloc_format_args_for_NULL_format();
    test_astr_eq_bytes();
    test_astr_eq_bytes_for_NULL_string();
    return EXIT_SUCCESS;