1 /* $MirOS: contrib/code/jupp/vs.h,v 1.6 2016/10/30 00:19:58 tg Exp $ */
3 * Dynamic string library
5 * (C) 1992 Joseph H. Allen
7 * This file is part of JOE (Joe's Own Editor)
18 * This is a dynamic string library which supports strings which automatically
19 * resize themselves when needed. The strings know their own size, so getting
20 * the length of a string is a fast operation and storing zeroes in the
21 * strings is permissable.
23 * The strings are stored in malloc blocks and have the following format:
25 * <bksize><length><string><zero>
27 * 'bksize' and 'length' are ints which give the size of the malloc block
28 * and the length of the string. A zero character always follows the string
29 * for compatibility with normal C zero-terminated strings. The zero is not
30 * counted in the string length.
32 * To further the compatibility with C strings, the address of a dynamic string
33 * is at <string> above, not at <bksize> (whose address is the start of the
34 * malloc block). Thus, dynamic strings can be passed as arguments to UNIX
35 * operating system functions and C library function, but they can not be freed
36 * with free()- a special function is provided in this library for freeing
39 * The primary dynamic string function is:
41 * char *vsncpy(char *d, int off, char *s, int len);
42 * Copy a block of characters at address 's' of
43 * length 'len' onto the dynamic string 'd' at
44 * offset 'off'. The dynamic string is expanded
45 * to handle any values of 'len' and 'off' which
46 * might be given. If 'off' is greater than the
47 * length of the string, SPACEs are placed in the
48 * gap. If 'd' is NULL, a string is created. If
49 * 'len' is 0, no copying or string expansion
52 * Three important macros are provided for helping with vsncpy():
54 * sc("Hello") Gives --> "Hello",sizeof("Hello")-1
55 * sz(s) Gives --> s,strlen(s)
56 * sv(d) Gives --> d,sLEN(d)
58 * These are used to build arguments for vsncpy(). Many functions
59 * can be created with combinations of sc/sz/sv with vsncpy:
61 * s=vsncpy(NULL,0,NULL,0); Create an empty dynamic string
63 * s=vsncpy(NULL,0,sc("Hello")); Create a dynamic string initialized
66 * d=vsncpy(NULL,0,sv(s)); Duplicate a dynamic string
68 * d=vsncpy(NULL,0,sz(s)); Convert a C string into a dynamic
71 * d=vsncpy(sv(d),sv(s)); Append dynamic string s onto d.
73 * d=vsncpy(sv(d),sc(".c")); Append a ".c" extension to d.
76 * These lesser functions are also provided:
78 * void vsrm(char *s); Free a string. Do nothing if 's' is NULL.
80 * int sLEN(char *s); Return the length of the string 's'. If 's'
83 * char *vstrunc(char *d,int len);
84 * Set the length of a string. Expand the string
85 * with blanks if necessary.
87 * char *vsensure(char *d,int len);
88 * Expand the malloc block for the string if
89 * necessary so that a string of 'len' chars can
92 * sLen(s)=10; Set the length indicator of the string to 10.
94 * char *vsins(char *d,int off,int len);
95 * Insert a gap into a string.
97 * char *vsdel(char *d,int off,int len);
98 * Delete characters from a string.
100 * Other function are provided as well. Look through the rest of the header
101 * file. The header file is kind of weird looking because it is intended to
102 * handle dynamic arrays of any type with only a few changes.
105 /* Functions and global variable you have to define. Replace these with
106 * macros or defines here if they are not to be actual functions
109 /* An element with name 'a' */
110 typedef unsigned char sELEMENT;
112 /* Duplicate an element */
113 /* sELEMENT sdup(); */
116 /* Delete an element */
117 /* sELEMENT sdel(); */
118 #define sdel(a) do {} while(0) /* effectively do nothing ;-) */
120 /* Compare a single element */
122 #define scmp(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
124 /* A blank element */
125 /* extern sELEMENT sblank; */
128 /* A termination element */
129 /* extern sELEMENT sterm; */
132 /************************/
133 /* Creation/Destruction */
134 /************************/
136 /* sELEMENT *vsmk(int len);
137 * Create a variable length array. Space for 'len' elements is preallocated.
139 sELEMENT *vsmk PARAMS((int len));
141 /* void vsrm(sELEMENT *vary);
142 * Free an array and everything which is in it. Does nothing if 'vary' is
145 void vsrm PARAMS((sELEMENT *vary));
147 /********************/
148 /* Space management */
149 /********************/
151 /* int sSIZ(sELEMENT *vary);
152 * int sSiz(sELEMENT *vary);
153 * Access size part of array. This int indicates the number of elements which
154 * can fit in the array before realloc needs to be called. It does not include
155 * the extra space needed for the terminator and the header.
157 * sSIZ returns 0 if you pass it 0. sSiz does not do this checking,
158 * but can be used as an lvalue.
160 #define sSIZ(a) ((a) ? *((int *)(a) - 2) : 0)
161 #define sSiz(a) (*((int *)(a) - 2))
163 /* int sLEN(sELEMENT *vary);
164 * int sLen(sELEMENT *vary);
165 * Access length part of array. This int indicates the number of elements
166 * currently in the array (not including the terminator). This should be
167 * used primarily for reading the size of the array. It can be used for
168 * setting the size of the array, but it must be used with care since it
169 * does not eliminate elements (if the size decreases) or make sure there's
170 * enough room (if the size increases). See vensure and vtrunc.
172 * sLEN return a length of zero if 'vary' is 0.
173 * sLen doesn't do this checking, but can be used as an lvalue
175 #define sLEN(a) ((a) ? *((int *)(a) - 1) : 0)
176 #define sLen(a) (*((int *)(a) - 1))
178 /* int slen(const sELEMENT *ary);
179 * Compute length of char or variable length array by searching for termination
180 * element. Returns 0 if 'vary' is 0.
182 int slen PARAMS((const sELEMENT *ary));
184 /* sELEMENT *vsensure(sELEMENT *vary, int len);
185 * Make sure there's enough space in the array for 'len' elements. Whenever
186 * vsensure reallocs the array, it allocates 25% more than the necessary
187 * minimum space in anticipation of future expansion. If 'vary' is 0,
188 * it creates a new array.
190 sELEMENT *vsensure PARAMS((sELEMENT *vary, int len));
192 /* sELEMENT *vstrunc(sELEMENT *vary, int len));
193 * Truncate array to indicated size. This zaps or expands with blank elements
194 * and sets the LEN() of the array. A new array is created if 'vary' is 0.
196 sELEMENT *vstrunc PARAMS((sELEMENT *vary, int len));
198 /************************************/
199 /* Function which write to an array */
200 /************************************/
202 /* sELEMENT *vsfill(sELEMENT *vary, int pos, sELEMENT el, int len);
203 * Set 'len' element of 'vary' beginning at 'pos' to duplications of 'el'.
204 * Ok, if pos/len are past end of array. If 'vary' is 0, a new array is
207 * This does not zap previous values. If you need that to happen, call
208 * vszap first. It does move the terminator around properly though.
210 sELEMENT *vsfill PARAMS((sELEMENT *vary, int pos, sELEMENT el, int len));
212 /* sELEMENT *vsncpy(sELEMENT *vary, int pos, const sELEMENT *array, int len));
213 * Copy 'len' elements from 'array' onto 'vary' beginning at position 'pos'.
214 * 'array' can be a normal char array since the length is passed seperately. The
215 * elements are copied, not duplicated. A new array is created if 'vary' is
216 * 0. This does not zap previous elements.
218 sELEMENT *vsncpy PARAMS((sELEMENT *vary, int pos, const sELEMENT *array, int len));
220 /* sELEMENT *vsndup(sELEMENT *vary, int pos, sELEMENT *array, int len));
221 * Duplicate 'len' elements from 'array' onto 'vary' beginning at position
222 * 'pos'. 'array' can be a char array since its length is passed seperately. A
223 * new array is created if 'vary' is 0.
225 sELEMENT *vsndup PARAMS((sELEMENT *vary, int pos, sELEMENT *array, int len));
227 /* sELEMENT *vsdup(sELEMENT *vary));
228 * Duplicate array. This is just a functionalized version of:
230 * vsndup(NULL, 0, vary, sLEN(vary));
232 * but since you need to be able to refer to this particular function by
233 * address often it's given here.
235 * (actually, there's bazillions of these simple combinations of the above
236 * functions and the macros of the next section. You'll probably want to make
237 * functionalized instances of the ones you use most often - especially since
238 * the macros aren't safe).
240 sELEMENT *vsdup PARAMS((sELEMENT *vary));
242 /* sELEMENT *vsset(sELEMENT *vary, int pos, sELEMENT element);
243 * Set an element in an array. Any value of 'pos' is valid. A new array
244 * is created if 'vary' is 0. The previous contents of the position is
245 * deleted. This does not duplicate 'element'. If you need 'element'
246 * duplicated, call: vsset(vary,pos,sdup(element));
248 sELEMENT *_vsset PARAMS((sELEMENT *vary, int pos, sELEMENT el));
250 #define vsset(v, p, el) \
251 (!(v) || (p) > sLen(v) || (p) >= sSiz(v) ? \
252 _vsset((v), (p), (el)) \
255 ((v)[(p) + 1] = 0, sLen(v) = (p) + 1, (v)[p] = (el), (v)) \
257 ((v)[p] = (el), (v)) \
261 /* sELEMENT *vsadd(sELEMENT *vary, sELEMENT element);
262 * Concatenate a single element to the end of 'vary'. A new array is created
263 * if 'vary' is 0. This does not duplicate element: call
264 * vsadd(vary,sdup(element)); If you need it duplicated.
266 #define vsadd(v, el) \
267 (!(v) || sLen(v) == sSiz(v) ? \
268 _vsset((v), sLEN(v), (el)) \
270 ((v)[sLen(v) + 1] = 0, (v)[sLen(v)] = (el), sLen(v) = sLen(v) + 1, (v)) \
273 /**************************************/
274 /* Functions which read from an array */
275 /**************************************/
277 /* These macros are used to generate the address/size pairs which get
278 * passed to the functions of the previous section.
281 /* { sELEMENT *, int } sv(sELEMENT *array);
282 * Return array, size pair. Uses sLEN to get size.
284 #define sv(a) (a), sLEN(a)
286 /* { sELEMENT *, int } sz(sELEMENT *array);
287 * Return array, size pair. Uses slen to get size.
289 #define sz(a) (a), slen(a)
291 /* { sELEMENT *, int } sc(sELEMENT *array);
292 * Return array, size pair. Uses 'sizeof' to get size.
294 #define sc(a) (unsigned char *)(a), (sizeof(a) / sizeof(sELEMENT) - 1)
296 /* { sELEMENT *, int } srest(sELEMENT *vary, int pos);
297 * Return array, size pair of rest of array beginning at pos. If
298 * pos is past end of array, gives size of 0.
300 #define srest(a, p) ((a) + (p)), (((p) > sLEN(a)) ? 0 : sLen(a) - (p))
302 /* { sELEMENT *, int } spart(sELEMENT *vary, int pos, int len);
303 * Return array,size pair of 'len' elements of array beginning with pos. If
304 * pos is past end of array, gives size of 0. If pos+len is past end of array,
305 * returns number of elements to end of array.
307 #define spart(a, p, l) \
308 ((a) + (p)), ((p) >= sLEN(a) ? 0 : ((p) + (l) > sLen(a) ? sLen(a) - (p) : (l)))
310 /* sELEMENT vsget(sELEMENT *vary, int pos);
311 * Get an element from an array. Any value of pos is valid; if it's past the
312 * end of the array or if 'vary' is 0, the terminator is returned. This
313 * does not make a duplicate of the returned element. If you want that, pass
314 * the return value of this to sdup.
316 #define vsget(a, p) ((p) >= sLEN(a) ? sterm : (a)[p])
318 /*************************/
319 /* Searching and Sorting */
320 /*************************/
322 /* int vsbsearch(const sELEMENT *ary, int len, sELEMENT element);
323 * Do a binary search on a sorted variable length or char array. Returns position
324 * of matching element or the position where the element should be if it was
325 * not found. (You should test with scmp to find out which).
327 * Hmm... this should really indicate whether or not the element was found.
329 int vsbsearch PARAMS((const sELEMENT *ary, int len, sELEMENT el));
331 /* int vscmpn(sELEMENT *a, int alen, sELEMENT *b, int blen);
333 * Compare two arrays using scmp. If 'a' > 'b', return 1. If 'a' == 'b',
334 * return 0. If 'a' < 'b', return -1. Longer strings are > shorter ones if
335 * their beginning match.
337 int vscmpn PARAMS((sELEMENT *a, int alen, sELEMENT *b, int blen));
339 /* int vscmp(sELEMENT *a, sELEMENT *b);
341 * Functionalized version of: vscmpn(sv(a), sv(b));
343 int vscmp PARAMS((sELEMENT *a, sELEMENT *b));
345 /* int vsscan(const sELEMENT *a, int alen, const sELEMENT *b, int blen);
346 * Find offset of first matching element in 'a' which matches any
347 * of the elements passed in 'b'. Array 'b' must be sorted.
349 * Hmm... this really needs to return what the found element is.
351 int vsscan PARAMS((const sELEMENT *a, int alen, const sELEMENT *b, int blen));
353 /* int vsspan(sELEMENT *a, int alen, sELEMENT *b, int blen);
354 * Find offset of first matching element in 'a' which does not match any
355 * of the elements passed in 'b'. Array 'b' must be sorted.
357 int vsspan PARAMS((sELEMENT *a, int alen, sELEMENT *b, int blen));