-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.c
More file actions
63 lines (54 loc) · 1.07 KB
/
Copy pathstr.c
File metadata and controls
63 lines (54 loc) · 1.07 KB
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
#include "str.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static void *xmalloc(size_t n)
{
void *p = malloc(n);
if (!p)
{
fputs("strlib::str: out of memory\n", stderr);
abort();
}
return p;
}
Str str_from_parts(const char *data, size_t len)
{
char *buf = xmalloc(len + 1);
memcpy(buf, data, len);
buf[len] = '\0';
return (Str){.data = buf, .len = len};
}
Str str_from_cstr(const char *cstr)
{
return str_from_parts(cstr, strlen(cstr));
}
Str str_from_sv(SV sv)
{
return str_from_parts(sv.data, sv.len);
}
Str str_clone(Str s)
{
return str_from_parts(s.data, s.len);
}
void str_free(Str *s)
{
if (!s)
return;
free(s->data);
s->data = NULL;
s->len = 0;
}
const char *str_cstr(Str s)
{
return s.data; /* always null-terminated */
}
Str str_concat_sv(SV a, SV b)
{
size_t total = a.len + b.len;
char *buf = xmalloc(total + 1);
memcpy(buf, a.data, a.len);
memcpy(buf + a.len, b.data, b.len);
buf[total] = '\0';
return (Str){.data = buf, .len = total};
}