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.
|
psb_t * | psb_new (void) |
| Setup a new string builder. More...
|
|
int | psb_destroy (psb_t *self) |
| Destroys the given string builder. More...
|
|
int | psb_add_str (psb_t *self, const char *str) |
| Append the given string to the buffered string of the given string builder. More...
|
|
int | psb_add_char (psb_t *self, char c) |
| Append the given character to the buffered string of the given string builder. More...
|
|
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 . More...
|
|
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' . More...
|
|
size_t | psb_len (psb_t *self) |
| Get the length of the buffered string of the given string builder. More...
|
|
char * | psb_dump (psb_t *self) |
| Get a copy of the buffered string of the given string builder. More...
|
|
char * | psb_str (psb_t *self) |
| Get a reference to the buffered string. More...
|
|
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.
◆ psb_add_char()
int psb_add_char |
( |
psb_t * |
self, |
|
|
char |
c |
|
) |
| |
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.
◆ psb_add_str()
int psb_add_str |
( |
psb_t * |
self, |
|
|
const char * |
str |
|
) |
| |
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.
◆ psb_clear()
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
.
- Parameters
-
self | String builder to free. |
- Returns
0
on success, a number > 0 otherwise.
◆ psb_destroy()
int psb_destroy |
( |
psb_t * |
self | ) |
|
Destroys the given string builder.
- Parameters
-
self | String builder to destroy. |
- Returns
0
.
◆ psb_dump()
char* psb_dump |
( |
psb_t * |
self | ) |
|
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.
◆ psb_len()
size_t psb_len |
( |
psb_t * |
self | ) |
|
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.
◆ psb_new()
Setup a new string builder.
- Returns
- A new string builder or
NULL
on error.
◆ psb_str()
char* psb_str |
( |
psb_t * |
self | ) |
|
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.
◆ psb_truncate()
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'
.
- 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.