toolbox

Check-in [d6005d936d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:lib: new str_ methods and single linked list library
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d6005d936d1f0fc6fa0e5df08f962ac914595929a0b05fb48d774b7953a6b83e
User & Date: jef 2020-04-29 11:06:06
Context
2020-04-29
11:07
lib: new str_ methods and single linked list library check-in: 2a94a61c30 user: jef tags: trunk
11:06
lib: new str_ methods and single linked list library check-in: d6005d936d user: jef tags: trunk
2020-04-28
10:01
rrdreel: initial import check-in: 677c67fdd7 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/include/jt.h.

1
2
3
4

5
6
7
8
9
10
11




12
13
14
15
16
17
18
..
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
..
57
58
59
60
61
62
63

























































64
#ifndef _JT_H
#define _JT_H

#include <stddef.h>


/*
** macros
*/

#define ARRAY_SIZE(a) (sizeof((a))/sizeof(*(a)))





/*
** messages
*/ 

void warning(const char *fmt, ...);
void die(const char *fmt, ...);

................................................................................
void *xcalloc(size_t number, size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *str);
char *xstrndup(const char *str, size_t len);
void xfree(void *ptr);
int xasprintf(char **ret, const char *format, ...);

/*
** str
*/ 

char *str_trim_left(char *str);
char *str_trim_right(char *str);
char *str_trim(char *str);
char *str_replace(const char *str, const char *pattern, const char *repl);
char *str_join(const char *items[], size_t size, const char *sep);
char *str_cpy(char *dst, const char *src, const char *end);

/*
** jtbuf
*/ 

typedef char *jtbuf;

jtbuf jtbuf_new();
................................................................................
jtbuf jtbuf_append_str(jtbuf buffer, const char *str);
jtbuf jtbuf_append_sprintf(jtbuf buffer, const char *format, ...);

jtbuf jtbuf_trim_left(jtbuf buffer);
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);


























































#endif




>







>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
..
28
29
30
31
32
33
34











35
36
37
38
39
40
41
..
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
106
107
108
109
110
111
112
113
114
115
#ifndef _JT_H
#define _JT_H

#include <stddef.h>
#include <sys/queue.h>

/*
** macros
*/

#define ARRAY_SIZE(a) (sizeof((a))/sizeof(*(a)))

#define JTCONCAT_(a, b) a##b
#define JTCONCAT(a, b) JTCONCAT_(a, b)
#define JTGENSYM() JTCONCAT(__gensym, __LINE__)

/*
** messages
*/ 

void warning(const char *fmt, ...);
void die(const char *fmt, ...);

................................................................................
void *xcalloc(size_t number, size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *str);
char *xstrndup(const char *str, size_t len);
void xfree(void *ptr);
int xasprintf(char **ret, const char *format, ...);












/*
** jtbuf
*/ 

typedef char *jtbuf;

jtbuf jtbuf_new();
................................................................................
jtbuf jtbuf_append_str(jtbuf buffer, const char *str);
jtbuf jtbuf_append_sprintf(jtbuf buffer, const char *format, ...);

jtbuf jtbuf_trim_left(jtbuf buffer);
jtbuf jtbuf_trim_right(jtbuf buffer);
jtbuf jtbuf_trim(jtbuf buffer);

/*
** jtlst
*/

typedef struct jtlstnode {
	void *data;
	STAILQ_ENTRY(jtlstnode) next;
} jtlstnode;

STAILQ_HEAD(jt_tailq_list, jtlstnode);

typedef struct jtlst {
	size_t size;
	struct jt_tailq_list list;
} jtlst;

jtlst *jtlst_new();
jtlst *jtlst_clone(jtlst *arr);
void jtlst_free(jtlst *arr);
void jtlst_free_all(jtlst *arr);

void jtlst_prepend(jtlst *arr, void *data);
void jtlst_append(jtlst *arr, void *data);

int jtlst_pop(jtlst *arr, void **data);
jtlstnode *jtlst_iter(jtlst *arr);
int jtlst_next(jtlstnode **node, void **data);

size_t jtlst_size(jtlst *arr);

// scary macro to walk a jtlst list in a
// convenient manner from the user perspective
#define __jtlstnode JTGENSYM()
#define JTLST_FOREACH(value, arr) \
         struct jtlstnode * __jtlstnode; \
         for (__jtlstnode = STAILQ_FIRST((&arr->list)); \
                 (void) (__jtlstnode && ((value) = __jtlstnode->data)), __jtlstnode; \
                 __jtlstnode = STAILQ_NEXT(__jtlstnode, next))

/*
** str
*/ 

char *str_del_prefix(char *str, const char *prefix);
char *str_del_suffix(char *str, const char *suffix);

char *str_trim_left(char *str);
char *str_trim_right(char *str);
char *str_trim(char *str);

char *str_replace(const char *str, const char *pattern, const char *repl);
char *str_cpy(char *dst, const char *src, const char *end);
char *str_repeat(char *pattern, size_t n);

char *str_join(const char *items[], size_t size, const char *sep);
jtlst *str_split(const char *src, const char *sep);

#endif

Added src/lib/jtlst.c.

















































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "jt.h"

jtlst *
jtlst_new()
{
	jtlst *arr;

	arr = xcalloc(1, sizeof(jtlst));
	STAILQ_INIT(&arr->list);

	return arr;
}

jtlst *
jtlst_clone(jtlst *arr)
{
	struct jtlst *clone;
	void *data;

	clone = jtlst_new();
	JTLST_FOREACH(data, arr)
		jtlst_append(clone, data);

	return clone;
}

void
jtlst_free(jtlst *arr)
{
	struct jtlstnode *e;

	if (arr == NULL)
		return;

	while (! STAILQ_EMPTY(&arr->list)) {
		e = STAILQ_FIRST(&arr->list);
		STAILQ_REMOVE_HEAD(&arr->list, next);
		xfree(e);
	}
	xfree(arr);
}

void
jtlst_free_all(jtlst *arr)
{
	struct jtlstnode *e;

	if (arr == NULL)
		return;

	while (! STAILQ_EMPTY(&arr->list)) {
		e = STAILQ_FIRST(&arr->list);
		STAILQ_REMOVE_HEAD(&arr->list, next);
		xfree(e->data);
		xfree(e);
	}
	xfree(arr);
}

void
jtlst_prepend(jtlst *arr, void *data)
{

	struct jtlstnode *e;

	e = xcalloc(1, sizeof(struct jtlstnode));
	e->data = data;
	STAILQ_INSERT_HEAD(&arr->list, e, next);
	arr->size++;
}

void
jtlst_append(jtlst *arr, void *data)
{
	struct jtlstnode *e;

	e = xcalloc(1, sizeof(struct jtlstnode));
	e->data = data;
	STAILQ_INSERT_TAIL(&arr->list, e, next);
	arr->size++;
}

int
jtlst_pop(jtlst *arr, void **data)
{
	jtlstnode *e;

	if (STAILQ_EMPTY(&arr->list))
		return 0;

	e = STAILQ_FIRST(&arr->list);
	*data = e->data;
	STAILQ_REMOVE_HEAD(&arr->list, next);
	xfree(e);

	return 1;
}

size_t
jtlst_size(jtlst *arr)
{
	return arr->size;
}

jtlstnode *
jtlst_iter(jtlst *arr)
{
	return STAILQ_FIRST(&arr->list);
}

int
jtlst_next(jtlstnode **node, void **data)
{
	if (*node == NULL)
		return 0;
	*data = (*node)->data;
	*node = STAILQ_NEXT(*node, next);

	return 1;
}

Changes to src/lib/jtstr.c.

1
2

3




























4
5
6
7
8
9
10
..
92
93
94
95
96
97
98



































#include <string.h>
#include <ctype.h>

#include "jt.h"





























char *
str_trim_left(char *str)
{
	char *pos = str;

	while (isspace(*pos))
................................................................................

	while (*src && dst < end - 1)
		*dst++ = *src++;
	*dst = '\0';

	return dst;
}





































>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "jt.h"

char *
str_del_prefix(char *str, const char *prefix)
{
	size_t nchars, len_prefix;

	len_prefix = strlen(prefix);
	if (strncmp(str, prefix, len_prefix) == 0) {
		nchars = strlen(str) - len_prefix;
		memmove(str, str + len_prefix, nchars);
		str[nchars] = '\0';
	}

	return str;
}

char *
str_del_suffix(char *str, const char *suffix)
{
	size_t nchars, len_suffix;

	len_suffix = strlen(suffix);
	nchars = strlen(str) - len_suffix;
	if (strncmp(str + nchars, suffix, len_suffix) == 0)
		str[nchars] = '\0';

	return str;
}

char *
str_trim_left(char *str)
{
	char *pos = str;

	while (isspace(*pos))
................................................................................

	while (*src && dst < end - 1)
		*dst++ = *src++;
	*dst = '\0';

	return dst;
}

char *
str_repeat(char *pattern, size_t n)
{
	char *res, *p;
	size_t len = strlen(pattern);

	res = xcalloc(n * strlen(pattern) + 1, sizeof(char));
	p = res;
	while (n-- > 0) {
		memcpy(p, pattern, len);
		p += len;
	}

	return res;
}

jtlst *
str_split(const char *str, const char *sep)
{
	jtlst *arr;
	char *field;
	const char *p = str;

	arr = jtlst_new();
	while ((p = strstr(str, sep)) != NULL) {
		field = xstrndup(str, p - str);
		jtlst_append(arr, field);
		str = p + strlen(sep);
	}
	jtlst_append(arr, xstrdup(str));

	return arr;
}