aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_ElasticString.h
blob: c6e39eb195fce1a723abe478554c5cb6c959fc9e (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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_ElasticString.h
 * @ingroup memory
 * @brief An elastic C string.
 *
 * An elastic string is a dynamic array of characters which are readily expressed as a
 * nul-terminated C string.
 *
 */
#ifndef libparc_parc_ElasticString_h
#define libparc_parc_ElasticString_h

#include <stdarg.h>

#include <parc/algol/parc_Buffer.h>

struct parc_elastic_string;
typedef struct parc_elastic_string PARCElasticString;

/**
 * Perform validation on a pointer to a `PARCElasticString`.
 *
 * If invalid, this function will abort the running process.
 *
 * @param *  string A pointer to a `PARCElasticString` to validate.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     parcElasticString_AssertValid(string);
 * }
 * @endcode
 */
void parcElasticString_AssertValid(const PARCElasticString *string);

/**
 * Create an empty `PARCElasticString` instance.
 *
 *   The instance will be empty upon initialization (i.e., `parcElasticString_ToString()` will
 *   return an empty string), but characters and strings may be inserted/appended to
 *   the instance to produce usable content,
 *
 * @return A pointer to an allocated `PARCElasticString` that must be freed with `parcElasticString_Release()`.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     // use the string as necessary
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
PARCElasticString *parcElasticString_Create(void);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param *  string A pointer to a pointer to the instance to release.
 *
 * @return The number of remaining references to the object.
 *
 * Example:
 * @code
 * {
 *     PARCElasticString *buffer = parcElasticString_Create();
 *
 *     parcElasticString_Release(&pathName);
 * }
 * @endcode
 */
void parcElasticString_Release(PARCElasticString **string);

/**
 * Retrieve the number of remaining bytes between the current position
 * in the string and its (flexible) limit.
 *
 * @param *  string A pointer to an `PARCElasticString` instance.
 *
 * @return The non-negative number of characters remaining between the position and limit.
 *
 * Example:
 * @code
 * {
 *     char *inputString = "Hello World";
 *     size_t inputLength = strlen(input);
 *     PARElasticString *string = parcElasticString_Create();
 *     parcElasticString_PutString(string, inputString);
 *     parcElasticString_Flip(string);
 *     size_t numRemaining = parcElasticString_Remaining(string);
 *
 *     // numRemaining == inputLength
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
size_t parcElasticString_Remaining(const PARCElasticString *string);

/**
 * Set the limit to the current position, then the position to zero.
 * If the mark is defined, it is invalidated,
 * and any subsequent operation that requires the mark will abort until the mark
 * is set again via `parcElasticString_Mark`.
 *
 * @param *  string A pointer to an `PARCElasticString` instance.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     char *inputString = "Hello World";
 *     size_t inputLength = strlen(input);
 *     PARElasticString *string = parcElasticString_Create();
 *     parcElasticString_PutString(string, inputString);
 *     parcElasticString_Flip(string);
 *     size_t numRemaining = parcElasticString_Remaining(string);
 *
 *     // numRemaining == inputLength
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
PARCElasticString *parcElasticString_Flip(PARCElasticString *string);

/**
 * Return the given `PARCElasticString`'s position.
 *
 * A buffer's position is the index of the next element to be read or written.
 * A buffer's position is never negative and is never greater than its limit.
 *
 * @param *  string A pointer to a `PARCElasticString` instance.
 *
 * @return The given `PARCElasticString`'s position.
 *
 * Example:
 * @code
 * {
 *     size_t currentPosition = parcBuffer_Position(buffer);
 * }
 * @endcode
 *
 * @see parcElasticString_SetPosition
 */
size_t parcElasticString_Position(const PARCElasticString *string);

/**
 * Set the position in the given `PARCElasticString`.
 *
 * If the mark is defined and larger than the new position then it is invalidated.
 *
 * @param *  string A pointer to a `PARCElasticString` instance.
 * @param *  newPosition The new position for the `PARCElasticString`.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *     parcElasticString_PutString(string, "Hello World");
 *
 *     parcElasticString_SetPosition(string, 0);
 *
 *     // position is now at 0, instead of at the end of "Hello World"
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_Position
 */
PARCElasticString *parcElasticString_SetPosition(PARCElasticString *string, size_t newPosition);

/**
 * Append an array with the specified number of bytes to the end of this `PARCElasticString` instance.
 *
 * The position of the string is advanced by the length of the array.
 *
 * @param *  string A pointer to a `PARCElasticString` instance.
 * @param *  array A pointer to the array containing the bytes to append.
 * @param *  length The length of the input array.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     uint8_t * appendArray = { 0x00, 0x01, 0x02, 0x03, 0x04 };
 *     parcElasticString_PutArray(string, appendArray, 5);
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_PutString
 */
PARCElasticString *parcElasticString_PutArray(PARCElasticString *string, const char *array, size_t length);

/**
 * Append a C-string to the end of this `PARCElasticString` instance.
 *
 * The position of the string is advanced by the length of the string.
 *
 * @param *  string A pointer to a `PARCElasticString` instance.
 * @param *  cString A pointer to a nul-terminated C string to append to this `PARCElasticString`.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     parcElasticString_PutString(string, "Hello World");
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_PutArray
 */
PARCElasticString *parcElasticString_PutString(PARCElasticString *string, const char *cString);

/**
 * Append the contents of a `PARCBuffer` instance to the end of the `PARCElasticString` instance.
 *
 * The position of the string is advanced by the length of the buffer.
 *
 * @param *  string A pointer to a `PARCElasticString` instance.
 * @param *  buffer A pointer to a `PARCBuffer` instance.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     uint8_t * array = { 'H', 'e', 'l', 'l', 'o' };
 *     PARCBuffer *endBuffer = parcBuffer_Allocate(10);
 *     parcBuffer_PutArray(endBuffer, 5, array);
 *     parcElasticString_PutBuffer(string, endBuffer);
 *
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_PutString
 */
PARCElasticString *parcElasticString_PutBuffer(PARCElasticString *string, PARCBuffer *buffer);

/**
 * Append a single character (byte) to the end of this string.
 *
 * The position of the string is advanced by one (1).
 *
 * @param *  string A pointer to `PARCElasticString`
 * @param *  character A `char` value to append.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     parcElasticString_PutChar(string, 'H');
 *     parcElasticString_PutChar(string, 'e');
 *     parcElasticString_PutChar(string, 'l');
 *     parcElasticString_PutChar(string, 'l');
 *     parcElasticString_PutChar(string, 'o');
 *
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_PutString
 */
PARCElasticString *parcElasticString_PutChar(PARCElasticString *string, const char character);

/**
 * Put a variable number of characters into the `PARCElasticString`.
 *
 * @param *  string The `PARCElasticString` to receive the characters.
 * @param *  count The number of characters to insert into the `PARCElasticString`
 * @param *  ...  The characters to insert into the `PARCElasticString`.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     parcElasticString_PutChar(string, 5, 'H', 'e', 'l', 'l', 'o');
 *
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 *
 * @see parcElasticString_PutString
 */
PARCElasticString *parcElasticString_PutChars(PARCElasticString *string, unsigned int count, ...);

/**
 * Append an arbitrary number of C-style strings to the given `PARCElasticString` instance.
 *
 * @param *  string A pointer to `PARCElasticString`
 * @param *  ... The nul-terminated, C-style strings to append to the given `PARCElasticString`.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *baseString = parcElasticString_Create();
 *     parcElasticString_PutString(baseString, "Hello");
 *     uint8_t * string1 = {' ', '\0'};
 *     uint8_t * string2 = {'W', 'o', 'r', 'l', 'd', '\0'};
 *
 *     parcElasticString_PutStrings(baseString, string1, string2);
 *
 *     printf("String = %s\n", parcElasticString_ToString(baseString));
 *
 *     parcElasticString_Release(&baseString);
 * }
 * @endcode
 */
PARCElasticString *parcElasticString_PutStrings(PARCElasticString *string, ...);

/**
 * Append a formatted nul-terminated, C-style string string to the given `PARCElasticString` instance.
 *
 * @param *  string A pointer to `PARCElasticString`.
 * @param *  format The format string
 * @param *  ... Remaining parameters used to format the string.
 *
 * @return The same pointer as the `string` parameter.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *
 *     parcElasticString_Format(string, "Hello %s\n", "World");
 *
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
PARCElasticString *parcElasticString_Format(PARCElasticString *string, const char *format, ...) \
    __attribute__((format(printf, 2, 3)));

/**
 * Retrieve a handle to the `PARCBuffer` instance. The reference count is not increased.
 *
 * @param *  string A pointer to `PARCElasticString`.
 *
 * @return The `PARCBuffer` instance used to encapsulate the `PARCElasticString` contents.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *     parcElasticString_PutString(string, "Hello World");
 *
 *     PARCBuffer *buffer = parcElasticString_ToBuffer(string);
 *     printf("String in hex = %s\n", parcBuffer_ToHexString(buffer));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
PARCBuffer *parcElasticString_ToBuffer(const PARCElasticString *string);

/**
 * Produce a C string representation of the given `PARCElasticString`.
 *
 * Produce an allocated, nul-terminated string representation of the given `PARCElasticString`.
 * The result must be freed by the caller via the `parcMemory_Deallocate()` function.
 *
 * @param *  string A pointer to `PARCElasticString`.
 *
 * @return A pointer to an allocated array containing a nul-terminated string the must be freed via `parcMemory_Deallocate()`.
 *
 * Example:
 * @code
 * {
 *     PARElasticString *string = parcElasticString_Create();
 *     parcElasticString_PutString(string, "Hello World");
 *
 *     printf("String = %s\n", parcElasticString_ToString(string));
 *
 *     parcElasticString_Release(&string);
 * }
 * @endcode
 */
char *parcElasticString_ToString(const PARCElasticString *string);
#endif // libparc_parc_ElasticString_h