A StringBuilder: uses an internal buffer to append strings and characters as needed and keeps track of the constructed string, grows the buffer by factor 2 automagically.
More...
#include <stddef.h>
Go to the source code of this file.
Typedefs |
|
typedef struct psb | psb_t |
Functions |
| psb_t * | psb_new (void) |
| | Setup a new string builder.
|
| int | psb_destroy (psb_t *self) |
| | Destroys the given string builder.
|
| int | psb_add_str (psb_t *self, const char *str) |
| | Append the given string to the buffered string of the given string builder.
|
| int | psb_add_char (psb_t *self, char c) |
| | Append the given character to the buffered string of the given string builder.
|
| int | psb_clear (psb_t *self) |
| | Free the allocated string buffer area of the given string builder, allocate a new one with a default initial size and set its length to 0.
|
| int | psb_truncate (psb_t *self, size_t len) |
| | Set the length of the buffered string of the given string builder to the given length and append '\0'.
|
| size_t | psb_len (psb_t *self) |
| | Get the length of the buffered string of the given string builder.
|
| char * | psb_dump (psb_t *self) |
| | Get a copy of the buffered string of the given string builder.
|
| char * | psb_str (psb_t *self) |
| | Get a reference to the buffered string.
|
Detailed Description
A StringBuilder: uses an internal buffer to append strings and characters as needed and keeps track of the constructed string, grows the buffer by factor 2 automagically.
- Note:
- All
psb_t parameter in the related functions are expected to be != NULL, otherwise be prepared for core dumps.
Function Documentation
Append the given character to the buffered string of the given string builder.
- Parameters:
-
| self | Where to append the character. |
| c | Character to append. |
- Returns:
0 on success, a number > 0 otherwise.
Append the given string to the buffered string of the given string builder.
- Parameters:
-
| self | Where to append the string. |
| str | String to append. |
- Returns:
0 on success, a number > 0 otherwise.
Free the allocated string buffer area of the given string builder, allocate a new one with a default initial size and set its length to 0.
- Parameters:
-
| self | String builder to free. |
- Returns:
0 on success, a number > 0 otherwise.
Destroys the given string builder.
- Parameters:
-
| self | String builder to destroy. |
- Returns:
0 .
Get a copy of the buffered string of the given string builder.
- Parameters:
-
| self | String builder to ask. |
- Returns:
- Metric as string in Prometheus exposition format.
- Note:
- The returned string must be freed when no longer needed.
Get the length of the buffered string of the given string builder.
- Parameters:
-
| self | String builder to query. |
- Returns:
- The length of the buffered string.
Setup a new string builder.
- Returns:
- A new string builder or
NULL on error.
Get a reference to the buffered string.
One should **NOT** modify the string, treat it as a read-only and never ever free() it, otherwise be prepared for buffer overflows and core dumps. It has been made public solely to avoid the strdup() overhead of psb_dump().
- Returns:
- a string, or
NULL if not initialized yet.
Set the length of the buffered string of the given string builder to the given length and append '\0'.
- Parameters:
-
| self | String builder to truncate. |
| len | The new length of the buffered string. If it is bigger than its current length, this function is a no-op. |
- Returns:
0 on success, a number > 0 otherwise.