summaryrefslogtreecommitdiff
path: root/src/lib/sv.c
blob: 33881d2e38ff57a57c094a361aae36cf81c29cc6 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <lib/sv.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct string_view sv_create(const char *buf, const size_t len)
{
	return (struct string_view){.buf = buf, .len = len}; 
}

int sv_init(struct string_view *sv, const char *b)
{
	assert(sv != NULL);

	size_t len = strlen(b);
	void *ptr = malloc(len);

	memcpy(ptr, b, len);

	sv->buf = ptr;
	sv->len = len;
	
	return 0;	       
}

void sv_free(struct string_view *sv)
{
	assert(sv != NULL);       
	free(sv->buf);
}

/**
 * @warning User's responsibility to cleanup memory.
 */
struct string_view sv_copy(struct string_view *sv)
{
    assert(sv != NULL);
    struct string_view copy = { 0 };
    copy.buf = malloc(sv->len);
    copy.len = sv->len;
    memcpy(copy.buf, sv->buf, sv->len);
    return copy;
}

int sv_equal(struct string_view *a, struct string_view *b)
{
    assert(a != NULL);
    assert(b != NULL);

    if (a->buf == NULL || b->buf == NULL)
        return 0;

    if (a->len != b->len)
        return 0;

    if (memcmp(a->buf, b->buf, a->len) != 0)
        return 0;

    return 1;
}

/**
 *  @warning User's responsibility to cleanup memory.
 */
struct string_view sv_concat(struct string_view *a, struct string_view *b)
{
    assert(a != NULL);
    assert(b != NULL);
    size_t nlen = a->len + b->len;
    char *ptr = malloc(nlen);
    return (struct string_view){.buf = ptr, .len = nlen};
}

int sv_concat_cstr(struct string_view *a, const char *cstr)
{
	assert(a != NULL);

	if (a->len > 0 && a->buf[a->len - 1] == '\0')
		a->len--;

	size_t lenb = strlen(cstr);
	size_t nlen = lenb + a->len + 1;

        char *tmp = realloc(a->buf, nlen);

	if (tmp == NULL)
		return -1;	

	a->buf = tmp;
	
	memcpy(a->buf + a->len, cstr, lenb);
	a->len = nlen - 1;
	a->buf[a->len] = '\0';
	
	return 0;
}