~donmcc/astr

aab645b98d64fe00b3c5005048356d474a340c3e — Don McCaughey 1 year, 9 months ago ac9e78d
Added the `astr_eq_bytes()` inline function.

Also added tests for `astr_eq_bytes()` and added documentation for it to
the readme and the man page.
5 files changed, 68 insertions(+), 3 deletions(-)

M README.md
M astr.3
M astr.c
M astr.h
M astr_tests.c
M README.md => README.md +5 -0
@@ 24,6 24,11 @@ Allocate an empty string.

Allocate a formatted string.

    bool
    astr_eq_bytes(char const *s1, char const *s2);

Compare the bytes of two strings for equality; `s1` and `s2` may be `NULL`.

## Dependencies
`astr` requires that `<stdio.h>` defines the [`vasprintf()`][2] function.
Building from repository source requires [GNU Autotools][3].

M astr.3 => astr.3 +18 -1
@@ 5,7 5,8 @@
.Os
.Sh NAME
.Nm astr_alloc_empty ,
.Nm astr_alloc_formatted
.Nm astr_alloc_formatted ,
.Nm astr_eq_bytes
.Nd functions for working with dynamically allocated strings.
.Sh SYNOPSIS
.In astr.h


@@ 13,6 14,8 @@
.Fn astr_alloc_empty "void"
.Ft char *
.Fn astr_alloc_formatted "char const *format" "..."
.Ft bool
.Fn astr_eq_bytes "char const s1" "char const s2"
.Sh DESCRIPTION
The
.Nm astr


@@ 44,6 47,15 @@ allocates an empty string.
.Pp
.Fn astr_alloc_formatted
allocates a formatted string.
.Pp
.Fn astr_eq_bytes
compares two string pointers for equality in an encoding agnostic way.
The arguments
.Fa s1
and
.Fa s2
may be
.Dv NULL .
.Sh RETURN VALUES
.Fn astr_alloc_empty
returns a pointer to a zero-length string on success.


@@ 56,6 68,11 @@ pointer and set
.Va errno
to
.Er ENOMEM .
.Pp
.Fn astr_eq_bytes
returns
.Dv true
if the two string pointers are equal or point to the same sequence of bytes.
.Sh SEE ALSO
.Xr free 3 ,
.Xr printf 3 ,

M astr.c => astr.c +4 -0
@@ 28,3 28,7 @@ astr_alloc_formatted(char const *format, ...)
    return s;
}


extern bool
astr_eq_bytes(char const *s1, char const *s2);


M astr.h => astr.h +13 -0
@@ 2,6 2,10 @@
#define ASTR_H_INCLUDED


#include <stdbool.h>
#include <string.h>


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



@@ 13,5 17,14 @@ astr_alloc_empty(void)
}


inline bool
astr_eq_bytes(char const *s1, char const *s2)
{
    if (s1 == s2) return true;
    if (NULL == s1 || NULL == s2) return false;
    return 0 == strcmp(s1, s2);
}


#endif


M astr_tests.c => astr_tests.c +28 -2
@@ 1,7 1,6 @@
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "astr.h"




@@ 22,7 21,7 @@ test_astr_alloc_formatted(void)
    char *s = astr_alloc_formatted("answer: %i", 42);
    assert(s);
    assert(10 == strlen(s));
    assert(0 == strcmp("answer: 42", s));
    assert(astr_eq_bytes("answer: 42", s));
    free(s);
}



@@ 38,12 37,39 @@ test_astr_alloc_formatted_for_NULL_format(void)
}


static void
test_astr_eq_bytes(void)
{
    char *s = astr_alloc_formatted("foo");

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

    free(s);
}


static void
test_astr_eq_bytes_for_NULL_string(void)
{
    char *s = astr_alloc_formatted("foo");

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

    free(s);
}


int
main(int argc, char *argv[])
{
    test_astr_alloc_empty();
    test_astr_alloc_formatted();
    test_astr_alloc_formatted_for_NULL_format();
    test_astr_eq_bytes();
    test_astr_eq_bytes_for_NULL_string();
    return EXIT_SUCCESS;
}