Differences From
Artifact [e0c5b0d583]:
25 25 #define _p(str) { sizeof str - 1, str }
26 26
27 27 typedef struct safestr {
28 28 union {
29 29 const char* ptr;
30 30 char* mutptr;
31 31 }; // im sorry okay
32 - bool heap;
32 +# ifndef k_static
33 + bool heap;
34 +# endif
33 35 size_t len;
34 36 } safestr;
35 37
36 -void delstr(safestr s) fn ({
37 - if (s.heap) { free(s.mutptr); }
38 -});
38 +#ifndef k_static
39 + void delstr(safestr s) fn ({
40 + if (s.heap) { free(s.mutptr); }
41 + });
42 +#endif
39 43
40 44 void clrstr(safestr* s) fn ({
41 - delstr(*s);
42 - s->heap = false;
45 +# ifndef k_static
46 + delstr(*s);
47 + s->heap = false;
48 +# endif
43 49 s->ptr = NULL;
44 50 s->len = 0;
45 51 })
46 52
47 -char* compose(pstr* lst,size_t ct, size_t* strsz) fn({
53 +size_t pstrsum(pstr* lst,size_t ct) fn({
48 54 size_t len = 0;
49 55 for (size_t i = 0; i < ct; ++i) {
50 56 if (lst[i].len == 0) {
51 57 if (lst[i].ptr == NULL) continue;
52 58 lst[i].len = strlen(lst[i].ptr);
53 59 }
54 60 len += lst[i].len;
55 61 }
56 - if (strsz != NULL) *strsz = len;
57 - if (len == 0) return NULL;
58 -
59 - char* str = malloc(len + 1);
60 - char* ptr = str;
62 +})
63 +
64 +char* pstrcoll(pstr* lst, size_t ct, char* ptr) fn({
61 65 for (size_t i = 0; i < ct; ++i) {
62 66 if (lst[i].len == 0) continue;
63 67 strncpy(ptr,lst[i].ptr,lst[i].len);
64 68 ptr += lst[i].len;
65 69 }
70 + return ptr;
71 +})
72 +
73 +#ifndef k_static
74 +char* compose(pstr* lst,size_t ct, size_t* strsz) fn({
75 + size_t len = pstrsum(lst,ct)
76 + if (strsz != NULL) *strsz = len;
77 + if (len == 0) return NULL;
78 +
79 + char* str = malloc(len + 1);
80 + char* ptr = pstrcoll(lst, ct, ptr);
66 81 *ptr = 0;
67 82 return str;
68 83 });
84 +#endif
85 +
86 +char* impose(pstr* lst,size_t ct, size_t* strsz, char* buf) fn({
87 + size_t len = pstrsum(lst,ct);
88 + if (strsz != NULL) *strsz = len;
89 + if (len == 0) return NULL;
90 +
91 + char* ptr = pstrcoll(lst, ct, buf);
92 + *ptr = 0;
93 + return ptr;
94 +});
69 95
96 +char* imprint(pstr lst, size_t* strsz, char* buf) fn({
97 + size_t len = pstrsum(&lst,1);
98 + if (strsz != NULL) *strsz = len;
99 + if (len == 0) return NULL;
100 +
101 + char* ptr = pstrcoll(&lst,1,buf);
102 + *ptr = 0;
103 + return ptr;
104 +});
105 +#undef fn