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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* Length/String string handling -- header
* Copyright (C) 2009 Chris Hall (GMCH), Highwayman
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef _ZEBRA_ELSTRING_H
#define _ZEBRA_ELSTRING_H
#include "misc.h"
/*==============================================================================
* This is some very simple support for strings which are Length/Body
* objects.
*
* NB: this object knows NOTHING about memory allocation etc. The objective
* is the simplest possible encapsulation of strings which are NOT '\0'
* terminated.
*
* NB: this object knows NOTHING about whether there is a '\0' beyond the
* 'len' of the string.
*/
struct elstring
{
union
{
void* v ; /* may be NULL iff len == 0 */
const void* cv ;
} body ;
ulen len ;
} ;
typedef struct elstring elstring_t[1] ;
typedef struct elstring* elstring ;
/* Setting an elstring object to all zeros is enough to initialise it to
* an empty string.
*/
enum { ELSTRING_INIT_ALL_ZEROS = true } ;
#define ELSTRING_INIT { { { NULL }, 0 } }
/*==============================================================================
* Pointer pair and unsigned pointer pair and const versions.
*/
struct pp
{
char* p ;
char* e ;
} ;
typedef struct pp pp_t[1] ;
typedef struct pp* pp ;
struct cpp
{
const char* p ;
const char* e ;
} ;
typedef struct cpp cpp_t[1] ;
typedef struct cpp* cpp ;
/*==============================================================================
* NULLs for all types of pp and for els
*/
Inline void pp_null(pp p) { p->p = p->e = NULL ; } ;
Inline void cpp_null(cpp p) { p->p = p->e = NULL ; } ;
Inline void els_null(elstring els) { els->body.v = NULL ; els->len = 0 ; } ;
/*==============================================================================
* Access functions.
*/
Inline void* els_body(elstring els) ;
Inline void* els_body_nn(elstring els) ;
Inline ulen els_len(elstring els) ;
Inline ulen els_len_nn(elstring els) ;
Inline void* els_end(elstring els) ;
Inline void* els_end_nn(elstring els) ;
Inline void els_pp(pp p, elstring els) ;
Inline void els_pp_nn(pp p, elstring els) ;
Inline void els_cpp(cpp p, elstring els) ;
Inline void els_cpp_nn(cpp p, elstring els) ;
Inline void*
els_body(elstring els)
{
return (els != NULL) ? els_body_nn(els) : NULL ;
} ;
Inline void*
els_body_nn(elstring els)
{
return els->body.v ;
} ;
Inline ulen
els_len(elstring els)
{
return (els != NULL) ? els_len_nn(els) : 0 ;
} ;
Inline ulen
els_len_nn(elstring els)
{
return els->len ;
} ;
Inline void*
els_end(elstring els)
{
return (els != NULL) ? els_end_nn(els) : NULL ;
} ;
Inline void*
els_end_nn(elstring els)
{
return (void*)((char*)els->body.v + els->len);
} ;
Inline void
els_pp(pp p, elstring els)
{
if (els != NULL)
els_pp_nn(p, els) ;
else
pp_null(p) ;
} ;
Inline void
els_pp_nn(pp p, elstring els)
{
p->p = els->body.v ;
p->e = p->p + els->len ;
} ;
Inline void
els_cpp(cpp p, elstring els)
{
if (els != NULL)
els_cpp_nn(p, els) ;
else
cpp_null(p) ;
} ;
Inline void
els_cpp_nn(cpp p, elstring els)
{
p->p = els->body.cv ;
p->e = p->p + els->len ;
} ;
/*==============================================================================
* All so simple that most is implemented as Inline
*/
extern elstring els_init_new(elstring els) ;
extern elstring els_new(void) ;
extern elstring els_free(elstring els) ;
extern int els_cmp(elstring a, elstring b) ;
extern int els_nn_cmp(const void* ap, ulen al, const void* bp, ulen bl) ;
extern int els_cmp_word(elstring a, elstring w) ;
extern int els_cmp_sig(elstring a, elstring b) ;
extern bool els_equal(elstring a, elstring b) ;
extern bool els_substring(elstring a, elstring b) ;
/*------------------------------------------------------------------------------
* Set elstring value from ordinary string.
*
* NB: elstring MUST NOT be NULL.
*
* NB: treats str == NULL as a zero length string.
*
* NB: sets "term" unless str == NULL.
*/
Inline void
els_set_nn(elstring els, const void* str)
{
els->body.cv = str ;
els->len = (str != NULL) ? strlen(str) : 0 ;
} ;
/*------------------------------------------------------------------------------
* Set elstring value from ordinary string.
*
* Creates elstring object if required (ie if els == NULL).
*
* See: els_set_nn.
*/
Inline elstring
els_set(elstring els, const void* str)
{
if (els == NULL)
els = els_new() ;
els_set_nn(els, str) ;
return els ;
} ;
/*------------------------------------------------------------------------------
* Set elstring value from body + length.
*
* NB: sets term = false.
*
* NB: elstring MUST NOT be NULL.
*
* NB: treats str == NULL as a zero length string.
*/
Inline void
els_set_n_nn(elstring els, const void* body, ulen len)
{
els->body.cv = body ;
els->len = len ;
} ;
/*------------------------------------------------------------------------------
* Set elstring value from body + length.
*
* Creates elstring object if required (ie if els == NULL).
*
* See els_set_n_nn.
*/
Inline elstring
els_set_n(elstring els, const void* body, ulen len)
{
if (els == NULL)
els = els_new() ;
els_set_n_nn(els, body, len) ;
return els ;
} ;
/*------------------------------------------------------------------------------
* Clear contents of an elstring (if any)
*
* NB: it is the callers responsibility to free the contents of the elstring.
* if that is required, before freeing the elstring itself.
*/
Inline void
els_clear(elstring els)
{
if (els != NULL)
{
els->body.v = NULL ;
els->len = 0 ;
} ;
} ;
/*------------------------------------------------------------------------------
* Set elstring 'len'.
*
* NB: it is the caller's responsibility to set a valid body !!
*
* NB: elstring MUST NOT be NULL.
*/
Inline void
els_set_len_nn(elstring els, ulen len)
{
els->len = len ;
} ;
/*------------------------------------------------------------------------------
* Set elstring body.
*
* NB: it is the caller's responsibility to set a valid body !!
*
* NB: it is the caller's responsibility to set a valid 'len'.
*
* NB: elstring MUST NOT be NULL.
*/
Inline void
els_set_body_nn(elstring els, const void* body)
{
els->body.cv = body ;
} ;
#endif /* _ZEBRA_ELSTRING_H */
|