aboutsummaryrefslogtreecommitdiffstats
path: root/src/fstring.h
blob: 616287fe493bda7869b4227b715245aa723b5470 (plain)
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
/*
 * Functions for handling with fixed size strings
 */

#ifndef FSTRING_H
#define FSTRING_H

#include "config.h"
#include "mem_pool.h"

#define update_buf_size(x) (x)->free = (x)->buf->size - ((x)->pos - (x)->buf->begin); (x)->buf->len = (x)->pos - (x)->buf->begin

typedef struct f_str_s {
	gchar *begin;
	size_t len;
	size_t size;
} f_str_t;

typedef struct f_str_buf_s {
	f_str_t *buf;
	gchar *pos;
	size_t free;
} f_str_buf_t;

typedef struct f_tok_s {
	f_str_t word;
	size_t pos;
} f_tok_t;

/*
 * Search first occurence of character in string
 */
ssize_t fstrchr (f_str_t *src, gchar c);

/*
 * Search last occurence of character in string
 */
ssize_t fstrrchr (f_str_t *src, gchar c);

/*
 * Search for pattern in orig
 */
ssize_t fstrstr (f_str_t *orig, f_str_t *pattern);

/*
 * Search for pattern in orig ignoring case
 */
ssize_t fstrstri (f_str_t *orig, f_str_t *pattern);

/*
 * Split string by tokens
 * word contains parsed word
 */
gint fstrtok (f_str_t *text, const gchar *sep, f_tok_t *state);

/*
 * Copy one string into other
 */
size_t fstrcpy (f_str_t *dest, f_str_t *src);

/*
 * Concatenate two strings
 */
size_t fstrcat (f_str_t *dest, f_str_t *src);

/*
 * Push one character to fstr
 */
gint fstrpush (f_str_t *dest, gchar c);

/*
 * Allocate memory for f_str_t
 */
f_str_t* fstralloc (memory_pool_t *pool, size_t len);

/*
 * Truncate string to its len
 */
f_str_t* fstrtruncate (memory_pool_t *pool, f_str_t *orig);

/*
 * Enlarge string to new size
 */
f_str_t* fstrgrow (memory_pool_t *pool, f_str_t *orig, size_t newlen);

/*
 * Return specified character
 */
#define fstridx(str, pos) *((str)->begin + (pos))

/*
 * Return fast hash value for fixed string
 */
guint32 fstrhash (f_str_t *str);

/*
 * Make copy of string to 0-terminated string
 */
gchar* fstrcstr (f_str_t *str, memory_pool_t *pool);

/*
 * Strip fstr string from space symbols
 */
void fstrstrip (f_str_t *str);

#endif