util  Diff

Differences From Artifact [e0c5b0d583]:

To Artifact [d981619adf]:


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




















#define _p(str) { sizeof str - 1, str }

typedef struct safestr {
	union {
		const char* ptr;
		char* mutptr;
	}; // im sorry okay

	bool heap;

	size_t len;
} safestr;


void delstr(safestr s) fn ({
	if (s.heap) { free(s.mutptr); }
});


void clrstr(safestr* s) fn ({

	delstr(*s);
	s->heap = false;

	s->ptr = NULL;
	s->len = 0;
})

char* compose(pstr* lst,size_t ct, size_t* strsz) fn({
	size_t len = 0;
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) {
			if (lst[i].ptr == NULL) continue;
			lst[i].len = strlen(lst[i].ptr);
		}
		len += lst[i].len;
	}
	if (strsz != NULL) *strsz = len;
	if (len == 0) return NULL;

	
	char* str = malloc(len + 1);
	char* ptr = str;

	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) continue;
		strncpy(ptr,lst[i].ptr,lst[i].len);
		ptr += lst[i].len;
	}











	*ptr = 0;
	return str;
});





























>
|
>



>
|
|
|
>


>
|
|
>




|








<
<
>
|
<
<
>





>
>
>
>
>
>
>
>
>
>
>



>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
98
99
100
101
102
103
104
105
#define _p(str) { sizeof str - 1, str }

typedef struct safestr {
	union {
		const char* ptr;
		char* mutptr;
	}; // im sorry okay
#	ifndef k_static
		bool heap;
#	endif
	size_t len;
} safestr;

#ifndef k_static
	void delstr(safestr s) fn ({
		if (s.heap) { free(s.mutptr); }
	});
#endif

void clrstr(safestr* s) fn ({
#	ifndef k_static
		delstr(*s);
		s->heap = false;
#	endif
	s->ptr = NULL;
	s->len = 0;
})

size_t pstrsum(pstr* lst,size_t ct) fn({
	size_t len = 0;
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) {
			if (lst[i].ptr == NULL) continue;
			lst[i].len = strlen(lst[i].ptr);
		}
		len += lst[i].len;
	}


})



char* pstrcoll(pstr* lst, size_t ct, char* ptr) fn({
	for (size_t i = 0; i < ct; ++i) {
		if (lst[i].len == 0) continue;
		strncpy(ptr,lst[i].ptr,lst[i].len);
		ptr += lst[i].len;
	}
	return ptr;
})

#ifndef k_static
char* compose(pstr* lst,size_t ct, size_t* strsz) fn({
	size_t len = pstrsum(lst,ct)
	if (strsz != NULL) *strsz = len;
	if (len == 0) return NULL;
	
	char* str = malloc(len + 1);
	char* ptr = pstrcoll(lst, ct, ptr);
	*ptr = 0;
	return str;
});
#endif

char* impose(pstr* lst,size_t ct, size_t* strsz, char* buf) fn({
	size_t len = pstrsum(lst,ct);
	if (strsz != NULL) *strsz = len;
	if (len == 0) return NULL;
	
	char* ptr = pstrcoll(lst, ct, buf);
	*ptr = 0;
	return ptr;
});

char* imprint(pstr lst, size_t* strsz, char* buf) fn({
	size_t len = pstrsum(&lst,1);
	if (strsz != NULL) *strsz = len;
	if (len == 0) return NULL;
	
	char* ptr = pstrcoll(&lst,1,buf);
	*ptr = 0;
	return ptr;
});
#undef fn