~donmcc/astr

ref: deccae99dacd10b65888f664ed7bcbd9c67c5a9a astr/astr.c -rw-r--r-- 1.2 KiB
deccae99Don McCaughey Rename astr_eq_bytes() to astr_eq(). 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "astr.h"

#include <errno.h>
#include <stdio.h>


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


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

    char *s;
    va_list arguments_copy;
    va_copy(arguments_copy, arguments);
    int chars_printed = vasprintf(&s, format, arguments_copy);
    if (chars_printed < 0) return NULL;
    return s;
}


size_t
astr_len_format(char const *format, ...)
{
    va_list arguments;
    va_start(arguments, format);
    size_t length = astr_len_format_args(format, arguments);
    va_end(arguments);
    return length;
}


size_t
astr_len_format_args(char const *format, va_list arguments)
{
    if (!format) {
        errno = EINVAL;
        return 0;
    }

    va_list arguments_copy;
    va_copy(arguments_copy, arguments);
    int length = vsnprintf(NULL, 0, format, arguments_copy);
    if (length < 0) return 0;
    return length;
}


extern char *
astr_alloc_empty(void);

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