ucx/string.h

changeset 0
1f419bd32da1
child 124
80609f9675f1
equal deleted inserted replaced
-1:000000000000 0:1f419bd32da1
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * Bounded string implementation.
30 *
31 * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings.
32 * The main difference to C strings is, that <code>sstr_t</code> does <b>not
33 * need to be <code>NULL</code>-terminated</b>. Instead the length is stored
34 * within the structure.
35 *
36 * When using <code>sstr_t</code>, developers must be full aware of what type
37 * of string (<code>NULL</code>-terminated) or not) they are using, when
38 * accessing the <code>char* ptr</code> directly.
39 *
40 * The UCX string module provides some common string functions, known from
41 * standard libc, working with <code>sstr_t</code>.
42 *
43 * @file string.h
44 * @author Mike Becker
45 * @author Olaf Wintermann
46 */
47
48 #ifndef UCX_STRING_H
49 #define UCX_STRING_H
50
51 #include "ucx.h"
52 #include "allocator.h"
53 #include <stddef.h>
54
55 /** Shortcut for a <code>sstr_t struct</code> literal. */
56 #define ST(s) { (char*)s, sizeof(s)-1 }
57
58 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
59 #define S(s) sstrn((char*)s, sizeof(s)-1)
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 /**
66 * The UCX string structure.
67 */
68 typedef struct {
69 /** A reference to the string (<b>not necessarily <code>NULL</code>
70 * -terminated</b>) */
71 char *ptr;
72 /** The length of the string */
73 size_t length;
74 } sstr_t;
75
76 /**
77 * Creates a new sstr_t based on a C string.
78 *
79 * The length is implicitly inferred by using a call to <code>strlen()</code>.
80 *
81 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
82 * do want a copy, use sstrdup() on the return value of this function.
83 *
84 * @param cstring the C string to wrap
85 * @return a new sstr_t containing the C string
86 *
87 * @see sstrn()
88 */
89 sstr_t sstr(char *cstring);
90
91 /**
92 * Creates a new sstr_t of the specified length based on a C string.
93 *
94 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
95 * do want a copy, use sstrdup() on the return value of this function.
96 *
97 * @param cstring the C string to wrap
98 * @param length the length of the string
99 * @return a new sstr_t containing the C string
100 *
101 * @see sstr()
102 * @see S()
103 */
104 sstr_t sstrn(char *cstring, size_t length);
105
106
107 /**
108 * Returns the cumulated length of all specified strings.
109 *
110 * At least one string must be specified.
111 *
112 * <b>Attention:</b> if the count argument does not match the count of the
113 * specified strings, the behavior is undefined.
114 *
115 * @param count the total number of specified strings (so at least 1)
116 * @param string the first string
117 * @param ... all other strings
118 * @return the cumulated length of all strings
119 */
120 size_t sstrnlen(size_t count, sstr_t string, ...);
121
122
123 /**
124 * Concatenates strings.
125 *
126 * At least one string must be specified and there must be enough memory
127 * available referenced by the destination sstr_t.ptr for this function to
128 * successfully concatenate all specified strings.
129 *
130 * The sstr_t.length of the destination string specifies the capacity and
131 * should match the total memory available referenced by the destination
132 * sstr_t.ptr. This function <i>never</i> copies data beyond the capacity and
133 * does not modify any of the source strings.
134 *
135 * <b>Attention:</b>
136 * <ul>
137 * <li>Any content in the destination string will be overwritten</li>
138 * <li>The destination sstr_t.ptr is <b>NOT</b>
139 * <code>NULL</code>-terminated</li>
140 * <li>The destination sstr_t.length is set to the total length of the
141 * concatenated strings</li>
142 * <li><i>Hint:</i> get a <code>NULL</code>-terminated string by performing
143 * <code>mystring.ptr[mystring.length]='\0'</code> after calling this
144 * function</li>
145 * </ul>
146 *
147 * @param dest new sstr_t with capacity information and allocated memory
148 * @param count the total number of strings to concatenate
149 * @param src the first string
150 * @param ... all other strings
151 * @return the argument for <code>dest</code> is returned
152 */
153 sstr_t sstrncat(sstr_t dest, size_t count, sstr_t src, ...);
154
155
156 /**
157 * Returns a substring starting at the specified location.
158 *
159 * <b>Attention:</b> the new string references the same memory area as the
160 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
161 * Use sstrdup() to get a copy.
162 *
163 * @param string input string
164 * @param start start location of the substring
165 * @return a substring of <code>string</code> starting at <code>start</code>
166 *
167 * @see sstrsubsl()
168 * @see sstrchr()
169 */
170 sstr_t sstrsubs(sstr_t string, size_t start);
171
172 /**
173 * Returns a substring with a maximum length starting at the specified location.
174 *
175 * <b>Attention:</b> the new string references the same memory area as the
176 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
177 * Use sstrdup() to get a copy.
178 *
179 * @param string input string
180 * @param start start location of the substring
181 * @param length the maximum length of the substring
182 * @return a substring of <code>string</code> starting at <code>start</code>
183 * with a maximum length of <code>length</code>
184 *
185 * @see sstrsubs()
186 * @see sstrchr()
187 */
188 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
189
190 /**
191 * Returns a substring starting at the location of the first occurrence of the
192 * specified character.
193 *
194 * If the string does not contain the character, an empty string is returned.
195 *
196 * @param string the string where to locate the character
197 * @param chr the character to locate
198 * @return a substring starting at the first location of <code>chr</code>
199 *
200 * @see sstrsubs()
201 */
202 sstr_t sstrchr(sstr_t string, int chr);
203
204 /**
205 * Returns a substring starting at the location of the last occurrence of the
206 * specified character.
207 *
208 * If the string does not contain the character, an empty string is returned.
209 *
210 * @param string the string where to locate the character
211 * @param chr the character to locate
212 * @return a substring starting at the last location of <code>chr</code>
213 *
214 * @see sstrsubs()
215 */
216 sstr_t sstrrchr(sstr_t string, int chr);
217
218 /**
219 * Splits a string into parts by using a delimiter string.
220 *
221 * This function will return <code>NULL</code>, if one of the following happens:
222 * <ul>
223 * <li>the string length is zero</li>
224 * <li>the delimeter length is zero</li>
225 * <li>the string equals the delimeter</li>
226 * <li>memory allocation fails</li>
227 * </ul>
228 *
229 * The integer referenced by <code>count</code> is used as input and determines
230 * the maximum size of the resulting list, i.e. the maximum count of splits to
231 * perform + 1.
232 *
233 * The integer referenced by <code>count</code> is also used as output and is
234 * set to
235 * <ul>
236 * <li>-2, on memory allocation errors</li>
237 * <li>-1, if either the string or the delimiter is an empty string</li>
238 * <li>0, if the string equals the delimiter</li>
239 * <li>1, if the string does not contain the delimiter</li>
240 * <li>the count of list items, otherwise</li>
241 * </ul>
242 *
243 * If the string starts with the delimiter, the first item of the resulting
244 * list will be an empty string.
245 *
246 * If the string ends with the delimiter and the maximum list size is not
247 * exceeded, the last list item will be an empty string.
248 *
249 * <b>Attention:</b> All list items <b>AND</b> all sstr_t.ptr of the list
250 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
251 * an allocator to managed memory, to avoid this.
252 *
253 * @param string the string to split
254 * @param delim the delimiter string
255 * @param count IN: the maximum size of the resulting list (0 for an
256 * unbounded list), OUT: the actual size of the list
257 * @return a list of the split strings as sstr_t array or
258 * <code>NULL</code> on error
259 *
260 * @see sstrsplit_a()
261 */
262 sstr_t* sstrsplit(sstr_t string, sstr_t delim, size_t *count);
263
264 /**
265 * Performing sstrsplit() using an UcxAllocator.
266 *
267 * <i>Read the description of sstrsplit() for details.</i>
268 *
269 * The memory for the sstr_t.ptr pointers of the list items and the memory for
270 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
271 * function.
272 *
273 * <b>Note:</b> the allocator is not used for memory that is freed within the
274 * same call of this function (locally scoped variables).
275 *
276 * @param allocator the UcxAllocator used for allocating memory
277 * @param string the string to split
278 * @param delim the delimiter string
279 * @param count IN: the maximum size of the resulting list (0 for an
280 * unbounded list), OUT: the actual size of the list
281 * @return a list of the split strings as sstr_t array or
282 * <code>NULL</code> on error
283 *
284 * @see sstrsplit()
285 */
286 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim,
287 size_t *count);
288
289 /**
290 * Compares two UCX strings with standard <code>memcmp()</code>.
291 *
292 * At first it compares the sstr_t.length attribute of the two strings. The
293 * <code>memcmp()</code> function is called, if and only if the lengths match.
294 *
295 * @param s1 the first string
296 * @param s2 the second string
297 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
298 * length of s1 is greater than the length of s2 or the result of
299 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
300 */
301 int sstrcmp(sstr_t s1, sstr_t s2);
302
303 /**
304 * Compares two UCX strings ignoring the case.
305 *
306 * At first it compares the sstr_t.length attribute of the two strings. If and
307 * only if the lengths match, both strings are compared char by char ignoring
308 * the case.
309 *
310 * @param s1 the first string
311 * @param s2 the second string
312 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
313 * length of s1 is greater than the length of s2 or the difference between the
314 * first two differing characters otherwise (i.e. 0 if the strings match and
315 * no characters differ)
316 */
317 int sstrcasecmp(sstr_t s1, sstr_t s2);
318
319 /**
320 * Creates a duplicate of the specified string.
321 *
322 * The new sstr_t will contain a copy allocated by standard
323 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
324 * <code>free()</code>.
325 *
326 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
327 * terminated.
328 *
329 * @param string the string to duplicate
330 * @return a duplicate of the string
331 * @see sstrdup_a()
332 */
333 sstr_t sstrdup(sstr_t string);
334
335 /**
336 * Creates a duplicate of the specified string using an UcxAllocator.
337 *
338 * The new sstr_t will contain a copy allocated by the allocators
339 * ucx_allocator_malloc function. So it is implementation depended, whether the
340 * returned sstr_t.ptr pointer must be passed to the allocators
341 * ucx_allocator_free function manually.
342 *
343 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
344 * terminated.
345 *
346 * @param allocator a valid instance of an UcxAllocator
347 * @param string the string to duplicate
348 * @return a duplicate of the string
349 * @see sstrdup()
350 */
351 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string);
352
353 /**
354 * Omits leading and trailing spaces.
355 *
356 * This function returns a new sstr_t containing a trimmed version of the
357 * specified string.
358 *
359 * <b>Note:</b> the new sstr_t references the same memory, thus you
360 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
361 * <code>free()</code>. It is also highly recommended to avoid assignments like
362 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
363 * source string. Assignments of this type are only permitted, if the
364 * sstr_t.ptr of the source string does not need to be freed or if another
365 * reference to the source string exists.
366 *
367 * @param string the string that shall be trimmed
368 * @return a new sstr_t containing the trimmed string
369 */
370 sstr_t sstrtrim(sstr_t string);
371
372 /**
373 * Checks, if a string has a specific prefix.
374 * @param string the string to check
375 * @param prefix the prefix the string should have
376 * @return 1, if and only if the string has the specified prefix, 0 otherwise
377 */
378 int sstrprefix(sstr_t string, sstr_t prefix);
379
380 /**
381 * Checks, if a string has a specific suffix.
382 * @param string the string to check
383 * @param suffix the suffix the string should have
384 * @return 1, if and only if the string has the specified suffix, 0 otherwise
385 */
386 int sstrsuffix(sstr_t string, sstr_t suffix);
387
388 #ifdef __cplusplus
389 }
390 #endif
391
392 #endif /* UCX_STRING_H */

mercurial