-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmstring.c
More file actions
52 lines (43 loc) · 845 Bytes
/
Copy pathmstring.c
File metadata and controls
52 lines (43 loc) · 845 Bytes
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
//
// Created by lisanhu on 5/8/19.
//
#include "mmstring.h"
#include <string.h>
#define FREE free
#define STRNDUP strndup
#define STRLEN strlen
mmstring ms_own(const char *cstr, size_t l) {
mmstring ms;
ms.s = STRNDUP(cstr, l);
ms.cap = l + 1;
ms.len = l;
return ms;
}
mmstring ms_borrow(char *cstr, size_t l) {
mmstring ms;
ms.s = cstr;
ms.cap = 0;
ms.len = l;
return ms;
}
void ms_destroy(mmstring *ms) {
if (ms->cap) {
FREE(ms->s);
}
ms->len = 0;
ms->cap = 0;
ms->s = NULL;
}
mmstring ms_from(char *s, bool own) {
mmstring ms;
size_t len = STRLEN(s);
if (own) {
return ms_own(s, len);
}
return ms_borrow(s, len);
}
size_t ms_to_cstr(mmstring ms, char *buf) {
strncpy(buf, ms.s, ms.len);
buf[ms.len] = '\0';
return ms.len;
}