libcstl
Loading...
Searching...
No Matches
_string.h
Go to the documentation of this file.
1/*!
2 * @file
3 */
4
5/*
6 * no include guard. this file is meant to be a template
7 * that can be included multiple times. Prior to including
8 * this file, the following macros must be defined:
9 * - TODO
10 */
11
12/*!
13 * @defgroup string String
14 * @ingroup highlevel
15 * @brief Vector-like memory management of a collection of characters
16 *
17 * Unless otherwise noted, @p %cstl_STRING is a "templatized" parameter
18 * and should be replaced with the type of string object in use, e.g.
19 * @p cstl_string, @p cstl_wstring.
20 *
21 * For example, the equivalent of
22 * @code
23 * // templatized prototype
24 * void cstl_STRING_init(struct cstl_STRING *);
25 * @endcode
26 * is
27 * @code
28 * // narrow string initialization
29 * void cstl_string_init(struct cstl_string *);
30 *
31 * // wide string initialization
32 * void cstl_wstring_init(struct cstl_wstring *);
33 * @endcode
34 */
35/*!
36 * @addtogroup string
37 * @{
38 */
39
40#include "cstl/common.h"
41
42#define STRV(NAME) CSTL_TOKCAT(cstl_STRING, _##NAME)
43#define STRF(NAME, ...) STRV(NAME)(__VA_ARGS__)
44#define STDSTRF(NAME, ...) CSTL_TOKCAT(STDSTRPFX, NAME)(__VA_ARGS__)
45
46#ifndef NO_DOC
47#define cstl_STRING_char_t STRV(char_t)
48#endif
49
50/*!
51 * @brief Type of character in a string
52 */
53typedef STRCHAR STRV(char_t);
54
55/*!
56 * @brief A string object
57 *
58 * The string object holds a "string" of characters in a contiguous
59 * area of memory followed by a nul character. The nul character is
60 * always maintained by the object and not included in the size of
61 * the string.
62 */
64{
65 /*! @privatesection */
66 struct cstl_vector v;
67};
68
69/*!
70 * @brief Initialize a string object
71 *
72 * @param[in,out] s A pointer to the string object to initialize
73 */
74static inline void STRF(init, struct cstl_STRING * const s)
75{
76 cstl_vector_init(&s->v, sizeof(cstl_STRING_char_t));
77}
78
79/*!
80 * @brief Get the number of characters in a string
81 *
82 * @param[in] s A pointer to a string object
83 *
84 * @return The number of characters (not including the
85 * object-maintained nul terminator) in the string
86 */
87static inline size_t STRF(size, const struct cstl_STRING * const s)
88{
89 size_t sz = cstl_vector_size(&s->v);
90 if (sz > 0) {
91 sz--;
92 }
93 return sz;
94}
95
96/*!
97 * @brief Get the number of characters a string can hold
98 *
99 * @param[in] s A pointer to a string object
100 *
101 * @return The number of characters the string object can hold
102 */
103static inline size_t STRF(capacity, const struct cstl_STRING * const s)
104{
105 size_t cap = cstl_vector_capacity(&s->v);
106 if (cap > 0) {
107 cap--;
108 }
109 return cap;
110}
111
112/*!
113 * @brief Request to increase the capacity of the string
114 *
115 * @param[in] s A pointer to the string object
116 * @param[in] sz The number of characters the string should be able to hold
117 *
118 * Requests to decrease the capacity are ignored. Requests to increase
119 * the capacity that fail do so quietly
120 */
121static inline void STRF(reserve, struct cstl_STRING * const s, const size_t sz)
122{
123 cstl_vector_reserve(&s->v, sz + 1);
124}
125
126/*!
127 * @brief Change the number of valid characters in the string
128 *
129 * @param[in] s A pointer to the string
130 * @param[in] sz The number of characters desired in the string
131 *
132 * The function attempts to set the number of valid characters to the
133 * number indicated. If the number is less than or equal to the
134 * current capacity of the string, the function always succeeds. If
135 * the number exceeds the capacity, and the function cannot increase
136 * the capacity, the function will cause an abort.
137 *
138 * During an increase, newly valid characters will be initialized to
139 * the string's nul character
140 */
141void STRF(resize, struct cstl_STRING * s, size_t sz);
142
143/*!
144 * @brief Get a pointer to a character in the string
145 *
146 * @param[in] s A pointer to the string
147 * @param[in] i The 0-based index of the desired character in the string
148 *
149 * @return A pointer to the character indicated by the index
150 *
151 * @note The code will cause an abort if the index is outside the range
152 * of valid character positions it the string
153 */
154cstl_STRING_char_t * STRF(at, struct cstl_STRING * s, size_t i);
155/*!
156 * @brief Get a const pointer to a character from a const string
157 *
158 * @param[in] s A pointer to the string
159 * @param[in] i The 0-based index of the desired character in the string
160 *
161 * @return A pointer to the character indicated by the index
162 *
163 * @note The code will cause an abort if the index is outside the range
164 * of valid character positions it the string
165 */
166const cstl_STRING_char_t * STRF(at_const,
167 const struct cstl_STRING * s, size_t i);
168
169/*!
170 * @brief Get a pointer to the start of the string
171 *
172 * @param[in] s A pointer to the string object
173 *
174 * @note If the string is empty, the function may or may not return NULL
175 *
176 * @return A pointer to the start of the string data
177 * @retval NULL The string is empty
178 */
179static inline cstl_STRING_char_t * STRF(data, struct cstl_STRING * const s)
180{
181 return cstl_vector_data(&s->v);
182}
183
184/*!
185 * @brief Get a const pointer to the start of the string
186 *
187 * @param[in] s A pointer to the string object
188 *
189 * @note This function will always return a pointer to a valid string
190 *
191 * @return A const pointer to the start of the string data
192 */
193const cstl_STRING_char_t * STRF(str, const struct cstl_STRING * s);
194
195/*!
196 * @brief Compare a string object for (in)equality with a "raw" string
197 *
198 * The strings are compared as if by strlen(), and the result, as
199 * per that function is returned.
200 *
201 * @param[in] s A pointer to a string object
202 * @param[in] str A pointer to a "raw" string object
203 *
204 * @return An integer representing the result of the comparison as
205 * defined by strlen() or equivalent
206 */
207static inline int STRF(compare_str,
208 const struct cstl_STRING * const s,
209 const cstl_STRING_char_t * const str)
210{
211 return STDSTRF(cmp, STRF(str, s), str);
212}
213/*!
214 * @brief Compare two string objects for (in)equality
215 *
216 * The strings are compared as if by strlen(), and the result, as
217 * per that function is returned.
218 *
219 * @param[in] s1 A pointer to a string object
220 * @param[in] s2 A pointer to a(nother) string object
221 *
222 * @return An integer representing the result of the comparison as
223 * defined by strlen() or equivalent
224 */
225static inline int STRF(compare,
226 const struct cstl_STRING * const s1,
227 const struct cstl_STRING * const s2)
228{
229 return STRF(compare_str, s1, STRF(str, s2));
230}
231
232/*!
233 * @brief Return a string to its initialized state
234 *
235 * @param[in] s A pointer to a string object
236 */
237static inline void STRF(clear, struct cstl_STRING * const s)
238{
239 cstl_vector_clear(&s->v);
240}
241
242/*!
243 * @brief Insert characters into a string object
244 *
245 * Insert @p cnt copies of the character @p ch into the string @p s
246 * at the position denoted by @p pos. If @p pos > cstl_STRING_size(), the
247 * function abort()s.
248 *
249 * @param[in] s A string into which to insert characters
250 * @param[in] pos The position in @p s at which to insert the characters
251 * @param[in] cnt The number of copies of @p ch to insert
252 * @param[in] ch The character to insert into @p s
253 */
254void STRF(insert_ch,
255 struct cstl_STRING * s, size_t pos, size_t cnt, cstl_STRING_char_t ch);
256/*!
257 * @brief Insert a string into a string object
258 *
259 * Insert the first @p n characters contained in @p str into the string @p s
260 * at the position denoted by @p pos. If @p pos > cstl_STRING_size(), the
261 * function abort()s.
262 *
263 * @param[in] s A string into which to insert characters
264 * @param[in] pos The position in @p s at which to insert the characters
265 * @param[in] str The string from which to draw characters to insert into @p s
266 * @param[in] n The number of characters to draw from @p str
267 */
268void STRF(insert_str_n,
269 struct cstl_STRING * s, size_t pos,
270 const cstl_STRING_char_t * str, size_t n);
271/*!
272 * @brief Insert a string into a string object
273 *
274 * Insert the nul-terminated string pointed to @p str into the string @p s
275 * at the position denoted by @p pos. If @p pos > cstl_STRING_size(), the
276 * function abort()s.
277 *
278 * @param[in] s A string into which to insert characters
279 * @param[in] pos The position in @p s at which to insert the characters
280 * @param[in] str The string from which to draw characters to strert into @p s
281 */
282static inline void STRF(insert_str,
283 struct cstl_STRING * const s, const size_t pos,
284 const cstl_STRING_char_t * const str)
285{
286 STRF(insert_str_n, s, pos, str, STDSTRF(len, str));
287}
288/*!
289 * @brief Insert a string into a string object
290 *
291 * Insert the characters contained in @p ins into the string @p s
292 * at the position denoted by @p pos. If @p pos > cstl_STRING_size(), the
293 * function abort()s.
294 *
295 * @param[in] s A string into which to insert characters
296 * @param[in] pos The position in @p s at which to insert the characters
297 * @param[in] ins The characters to insert into @p s
298 */
299static inline void STRF(insert,
300 struct cstl_STRING * s, size_t pos,
301 const struct cstl_STRING * ins)
302{
303 STRF(insert_str_n, s, pos, STRF(str, ins), STRF(size, ins));
304}
305
306/*!
307 * @brief Append one string object to another
308 *
309 * Equivalent to @code
310 * cstl_STRING_insert(s1, cstl_STRING_size(s1), s2)
311 * @endcode
312 *
313 * @param[in] s1 The string to be extended
314 * @param[in] s2 The string to be appended to @p s1
315 */
316static inline void STRF(append, struct cstl_STRING * const s1,
317 const struct cstl_STRING * const s2)
318{
319 STRF(insert, s1, STRF(size, s1), s2);
320}
321
322/*!
323 * @brief Append a number of copies of a given character to a string
324 *
325 * Equivalent to @code
326 * cstl_STRING_insert_ch(s, cstl_STRING_size(s), cnt, ch)
327 * @endcode
328 *
329 * @param[in] s The string to be extended
330 * @param[in] cnt The number of copies of @p ch to append
331 * @param[in] ch The character to be appended
332 */
333static inline void STRF(append_ch, struct cstl_STRING * const s,
334 const size_t cnt, const cstl_STRING_char_t ch)
335{
336 STRF(insert_ch, s, STRF(size, s), cnt, ch);
337}
338
339/*!
340 * @brief Append characters from a string to a string object
341 *
342 * Equivalent to @code
343 * cstl_STRING_insert_str_n(s, cstl_STRING_size(s), str, len);
344 * @endcode
345 *
346 * @param[in] s The string to be extended
347 * @param[in] str The string from which to draw characters
348 * @param[in] len The number of characters from @p str to append to @p s
349 */
350static inline void STRF(append_str_n, struct cstl_STRING * const s,
351 const cstl_STRING_char_t * const str,
352 const size_t len)
353{
354 STRF(insert_str_n, s, STRF(size, s), str, len);
355}
356
357/*!
358 * @brief Append a string to a string object
359 *
360 * Equivalent to @code
361 * cstl_STRING_insert_str(s, cstl_STRING_size(s), str)
362 * @endcode
363 *
364 * @param[in] s The string to be extended
365 * @param[in] str The string to be appended to @p s1
366 */
367static inline void STRF(append_str, struct cstl_STRING * const s,
368 const cstl_STRING_char_t * const str)
369{
370 STRF(insert_str, s, STRF(size, s), str);
371}
372
373/*!
374 * @brief Set the contents of a string object to a "raw" string
375 *
376 * @param[in] s A pointer to a string object
377 * @param[in] str A pointer to a nul-terminated string
378 */
379static inline void STRF(set_str, struct cstl_STRING * const s,
380 const cstl_STRING_char_t * const str)
381{
382 STRF(resize, s, 0);
383 STRF(append_str, s, str);
384}
385
386/*!
387 * @brief Remove contiguous characters from a string object
388 *
389 * Removes the @p n characters at @p pos from the string, @p n is
390 * truncated, if necessary, to the number of characters between @p pos
391 * and the end of the string.
392 */
393void STRF(erase, struct cstl_STRING * s, size_t pos, size_t n);
394
395/*!
396 * @brief Get a substring from a string object
397 *
398 * Returns an n-character string starting at a specified position. If the
399 * substring indicated is longer than the number of characters possible, the
400 * length of the substring will be truncated.
401 *
402 * @param[in] s A string from which to get the substring
403 * @param[in] pos The starting position of the substring
404 * @param[in] n The number of characters in the substring
405 * @param[out] ss A pointer to an object containing the resulting substring
406 */
407void STRF(substr,
408 const struct cstl_STRING * s, size_t pos, size_t n,
409 struct cstl_STRING * ss);
410
411/*!
412 * @brief Find the first occurrence of a character in a string object
413 *
414 * Finds the first occurrence of the given character starting from
415 * the given position in the string object. If the given position is
416 * out of bounds, the function aborts.
417 *
418 * @param[in] s A pointer to the string object to be searched
419 * @param[in] ch The character sought
420 * @param[in] pos The position in the string at which to start the search
421 */
422ssize_t STRF(find_ch, const struct cstl_STRING * s, cstl_STRING_char_t ch,
423 size_t pos);
424/*!
425 * @brief Find the first occurrence of a string in a string object
426 *
427 * Finds the first occurrence of the given string starting from
428 * the given position in the string object. If the given position is
429 * out of bounds, the function aborts.
430 *
431 * @param[in] hay A pointer to the string object to be searched
432 * @param[in] ndl The string sought
433 * @param[in] pos The position in the string at which to start the search
434 */
435ssize_t STRF(find_str,
436 const struct cstl_STRING * hay,
437 const cstl_STRING_char_t * ndl,
438 size_t pos);
439/*!
440 * @brief Find the first occurrence of a string in a string object
441 *
442 * Finds the first occurrence of the given string starting from
443 * the given position in the string object. If the given position is
444 * out of bounds, the function aborts.
445 *
446 * @param[in] hay A pointer to the string object to be searched
447 * @param[in] ndl The string sought
448 * @param[in] pos The position in the string at which to start the search
449 */
450static inline ssize_t STRF(find, const struct cstl_STRING * const hay,
451 const struct cstl_STRING * const ndl,
452 const size_t pos)
453{
454 return STRF(find_str, hay, STRF(str, ndl), pos);
455}
456
457/*!
458 * @brief Swap the string objects at the two given locations
459 *
460 * @param[in,out] s1 A pointer to a string
461 * @param[in,out] s2 A pointer to a(nother) string
462 *
463 * The strings at the given locations will be swapped such that upon return,
464 * @p a will contain the string previously pointed to by @p b and vice versa.
465 */
466static inline void STRF(
467 swap, struct cstl_STRING * const s1, struct cstl_STRING * const s2)
468{
469 cstl_vector_swap(&s1->v, &s2->v);
470}
471
472/*!
473 * @brief The nul character associated with a string type
474 */
475extern const cstl_STRING_char_t STRV(nul);
476
477#undef cstl_STRING_char_t
478
479#undef STDSTRF
480#undef STRF
481#undef STRV
482
483/*!
484 * @}
485 */
void cstl_vector_reserve(struct cstl_vector *v, size_t sz)
Request to increase the capacity of the vector.
Definition vector.c:59
static size_t cstl_vector_size(const struct cstl_vector *const v)
Get the number of elements in the vector.
Definition vector.h:138
static size_t cstl_vector_capacity(const struct cstl_vector *const v)
Get the number of elements the vector can hold.
Definition vector.h:154
void cstl_vector_swap(struct cstl_vector *a, struct cstl_vector *b)
Swap the vector objects at the two given locations.
Definition vector.c:158
static void * cstl_vector_data(struct cstl_vector *const v)
Get a pointer to the start of the vector data.
Definition vector.h:166
static void cstl_vector_init(struct cstl_vector *const v, const size_t sz)
Initialize a vector object.
Definition vector.h:125
void cstl_vector_clear(struct cstl_vector *v)
Return a vector to its initialized state.
Definition vector.c:107
A string object.
Definition _string.h:64
Vector object.
Definition vector.h:31