73 "%lli",
"%li",
"%u",
"%llu",
"%lu",
"%x",
"%llx",
"%lx",
"%%",
"%c",
"%f",
"%F",
"%X"};
76 char* new_buffer = NULL;
81 string->buffer = NULL;
84 }
else if(string->
buffer != NULL) {
85 new_buffer = (
char*) realloc(string->
buffer,
sizeof(
char) * length + 1);
87 new_buffer = (
char*) calloc(length + 1,
sizeof(
char));
89 if(new_buffer != NULL) {
90 string->buffer = new_buffer;
91 string->length = length;
92 string->last_used =
string->last_used >= length ? length - 1 :
string->last_used;
93 string->buffer[
string->last_used] = 0;
105 size_t nb_supported = 0;
108 for(
size_t i = 0; i < nb_supported; i++) {
110 if(0 == strncmp(candidate, input, strlen(candidate))) {
127 (*string)->length = length;
128 (*string)->last_used = 0;
143 if((retval != 0) && (
string != NULL)) {
155 free((*string)->buffer);
167 string->buffer = NULL;
169 string->last_used = 0;
181 string->length = length;
195 string->buffer = NULL;
196 string->last_used = 0;
207 string->buffer[0] = 0;
209 string->last_used = 0;
242 size_t old_length = 0;
250 old_length =
string->length;
253 memset(string->
buffer + old_length, 0, length);
278 const char*
const text,
288 if(pos + length > string->
length) {
290 pos + length), exit);
293 string->last_used = pos + length >
string->last_used ? pos + length :
string->last_used;
300 memmove(string->
buffer + pos + length,
303 string->last_used += length;
306 memcpy(string->
buffer + pos, text, length);
307 string->buffer[
string->last_used] = 0;
318 size_t bytes_to_move = 0;
324 if((length == SIZE_MAX) ||
326 length =
string->last_used - pos;
329 bytes_to_move =
string->last_used - (pos + length);
330 memmove(string->
buffer + pos, string->
buffer + pos + length, bytes_to_move);
331 string->last_used -= length;
332 string->buffer[
string->last_used] = 0;
340 const size_t offset) {
341 const char* text = NULL;
345 if(string->
buffer == NULL) {
348 string->buffer[
string->last_used] = 0;
349 text =
string->buffer + offset;
360 if(string->
buffer != NULL) {
361 string->buffer[
string->last_used] = 0;
363 buffer =
string->buffer;
364 string->buffer = NULL;
365 string->last_used = 0;
376 char* original = NULL;
379 original =
string->buffer;
382 when_true(length < strlen(buffer) + 1, exit);
383 string->buffer = buffer;
384 string->last_used = strlen(buffer);
385 string->length = length;
387 string->buffer = NULL;
388 string->last_used = 0;
409 if((length == SIZE_MAX) ||
411 length =
string->last_used - start;
414 text = (
char*) calloc(length + 1,
sizeof(
char));
416 memcpy(text, string->
buffer + start, length);
432 while(pos <= string->last_used &&
433 fn(string->
buffer[pos]) != 0) {
438 string->last_used = 0;
439 string->buffer[0] = 0;
445 string->last_used -= pos;
447 string->buffer[
string->last_used] = 0;
465 string->buffer[
string->last_used] = 0;
523 const char* fmt, ...) {
549 size_needed = vsnprintf(NULL, 0, fmt, args) + 1;
552 size_t grow = (
string->last_used + size_needed -
string->length);
560 string->buffer[
string->length] = 0;
562 string->last_used += size_needed;
586 if(0 == strcmp(actual_placeholder,
"%%")) {
595 #define REPLACE_PLACEHOLDER(string, actual_placeholder, status, args, searched_placeholder, type) \
596 (0 == strcmp(actual_placeholder, searched_placeholder) \
597 ? status = amxc_string_appendf(string, actual_placeholder, va_arg(args, type)), true \
601 const char* fmt, va_list args) {
603 const char* pos = fmt;
607 while(*pos !=
'\0') {
608 size_t len_pos_old = 0;
609 size_t len_new_fixed = 0;
610 const char* placeholder = NULL;
611 const char* placeholder_in_pos = strchr(pos,
'%');
612 bool placeholder_handled =
false;
615 if(placeholder_in_pos == NULL) {
622 len_new_fixed = placeholder_in_pos - pos;
623 if(len_new_fixed != 0) {
630 if(placeholder == NULL) {
640 placeholder_handled =
REPLACE_PLACEHOLDER(
string, placeholder, status, args,
"%s",
const char*)
664 if((is_safe_cb != NULL) && !is_safe_cb(
amxc_string_get(
string, len_pos_old))) {
668 pos += len_new_fixed + strlen(placeholder);
678 const char* fmt, ...) {
705 size_needed = vsnprintf(NULL, 0, fmt, args) + 1;
708 size_t grow = (
string->last_used + size_needed -
string->length);
711 first_char =
string->buffer[0];
714 memmove(string->
buffer + size_needed - 1,
719 size_needed = vsnprintf(string->
buffer,
723 string->buffer[size_needed] = first_char;
724 string->last_used += size_needed;
752 data =
string->buffer;
753 while((*
data) !=
'\0') {
754 if(isdigit(*
data) == 0) {
767 uint32_t start_pos) {
769 size_t needle_len = 0;
770 const char* needle_loc = NULL;
775 when_true(needle == NULL || *needle == 0, exit);
777 needle_len = strlen(needle);
780 needle_loc = strstr(string->
buffer + start_pos, needle);
781 if(needle_loc != NULL) {
782 retval = needle_loc -
string->buffer;
795 size_t needle_len = 0;
796 size_t newstr_len = 0;
801 when_true(needle == NULL || *needle == 0, exit);
804 needle_len = strlen(needle);
805 newstr_len = strlen(newstr);
810 while(pos != -1 && max > 0) {
812 if(newstr_len != 0) {
816 if(max != UINT32_MAX) {
833 retval = strlen(
data);
847 for(uint32_t i = 0; i <=
string->last_used; i++) {
848 string->buffer[i] = toupper(string->
buffer[i]);
861 for(uint32_t i = 0; i <=
string->last_used; i++) {
862 string->buffer[i] = tolower(string->
buffer[i]);
884 for(uint32_t i = 0; i < len; i++) {
906 uint32_t str_len = 0;
908 uint32_t sep_count = 0;
909 const char* hex_binary = NULL;
910 uint32_t sep_len = sep == NULL ? 0 : strlen(sep);
918 *len = (str_len + 1) >> 1;
919 buffer = (
char*) calloc(1, *len);
924 for(uint32_t i = 0; i < str_len; i++) {
926 if((sep != NULL) && (sep_len != 0) &&
927 ( strncmp(hex_binary + i, sep, sep_len) == 0)) {
931 if(((i - sep_count) % 2 == 0) && ((i + 1) != str_len)) {
934 if((hex_binary[i] >=
'0') && (hex_binary[i] <=
'9')) {
935 buffer[pos] |= (hex_binary[i] -
'0') << shift;
936 }
else if((hex_binary[i] >=
'A') && (hex_binary[i] <=
'F')) {
937 buffer[pos] |= (hex_binary[i] -
'A' + 10) << shift;
938 }
else if((hex_binary[i] >=
'a') && (hex_binary[i] <=
'f')) {
939 buffer[pos] |= (hex_binary[i] -
'a' + 10) << shift;
944 if((i - sep_count) % 2 != 0) {
951 *len -= (((sep_len * sep_count) + 1) >> 1);
953 tmp = (
char*) realloc(buffer, *len);
#define when_failed(x, l)
int amxc_string_prependf(amxc_string_t *const string, const char *fmt,...)
int amxc_string_setf_checked(amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...)
static bool s_replace_percentage(amxc_string_t *string, const char *actual_placeholder, int *status)
static int amxc_string_realloc(amxc_string_t *string, const size_t length)
int amxc_string_appendf(amxc_string_t *const string, const char *fmt,...)
int amxc_string_appendf_checked(amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...)
static const char * s_get_format_placeholder(const char *input)
static const char *const s_supported_format_placeholders[]
int amxc_string_setf(amxc_string_t *const string, const char *fmt,...)
#define REPLACE_PLACEHOLDER(string, actual_placeholder, status, args, searched_placeholder, type)
Ambiorix string API header file.
Ambiorix variant API header file.
int amxc_llist_it_init(amxc_llist_it_t *const it)
Initializes a linked list iterator.
void amxc_llist_it_take(amxc_llist_it_t *const it)
Removes the iterator from the list.
void amxc_string_delete(amxc_string_t **string)
Frees the previously allocated string.
const char * amxc_string_get(const amxc_string_t *const string, const size_t offset)
Gets the content of the string buffer.
int amxc_string_to_lower(amxc_string_t *const string)
Converts all upper case characters to lower case.
int amxc_string_bytes_2_hex_binary(amxc_string_t *const string, const char bytes[], const uint32_t len, const char *sep)
Creates a hexbinary string from an array of bytes.
int amxc_string_vsetf_checked(amxc_string_t *const string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt, va_list args)
va_list version of amxc_string_setf_checked
int amxc_string_push_buffer(amxc_string_t *const string, char *buffer, size_t length)
Sets the string buffer.
int amxc_string_vsetf(amxc_string_t *const string, const char *fmt, va_list args)
Sets the content of the string using printf like formatting.
size_t amxc_string_set(amxc_string_t *const string, const char *data)
Sets a 0 terminated string in the string buffer.
AMXC_INLINE int amxc_string_append(amxc_string_t *const string, const char *const text, const size_t length)
Appends text to the end of the current content of the string buffer.
int amxc_string_replace(amxc_string_t *const string, const char *needle, const char *newstr, uint32_t max)
Replaces a number of sub-string occurrences in a string.
char * amxc_string_dup(const amxc_string_t *const string, const size_t start, size_t length)
Creates a full or partial copy of the text in the string buffer.
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
int amxc_string_to_upper(amxc_string_t *const string)
Converts all lower case characters to upper case.
int amxc_string_copy(amxc_string_t *const dest, const amxc_string_t *const src)
Copies the content.
AMXC_INLINE int amxc_string_insert_at(amxc_string_t *const string, const size_t pos, const char *text, size_t length)
Inserts a string of the given length into a string at a certain position.
int amxc_string_new(amxc_string_t **string, const size_t length)
Allocates a string.
AMXC_INLINE size_t amxc_string_text_length(const amxc_string_t *const string)
Gets the current size of the used string buffer.
bool(* amxc_string_is_safe_cb_t)(const char *replacement)
Checks if given replacement is safe to be included in a bigger string in a particular language.
int amxc_string_search(const amxc_string_t *const string, const char *needle, uint32_t start_pos)
Searches a sub-string in a string.
int amxc_string_vappendf(amxc_string_t *const string, const char *fmt, va_list args)
Appends a formatted string to a string.
int amxc_string_vappendf_checked(amxc_string_t *string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt, va_list args)
va_list version of amxc_string_appendf_checked
int amxc_string_grow(amxc_string_t *const string, const size_t length)
Grows the string buffer.
int amxc_string_vprependf(amxc_string_t *const string, const char *fmt, va_list args)
Prepends a formatted string to a string.
int amxc_string_shrink(amxc_string_t *const string, const size_t length)
Shrinks the string buffer.
int(* amxc_string_is_char_fn_t)(int c)
Definition of the signature of the "is char" callback function.
void amxc_string_trim(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim.
int amxc_string_init(amxc_string_t *const string, const size_t length)
Initializes a string.
enum _amxc_string_flags amxc_string_flags_t
amxc_string_set_at possible flags
int amxc_string_remove_at(amxc_string_t *const string, const size_t pos, size_t length)
Removes part of the text in the string buffer.
int amxc_string_set_at(amxc_string_t *const string, const size_t pos, const char *const text, const size_t length, const amxc_string_flags_t flags)
Set text in the string buffer at a certain position.
int amxc_string_hex_binary_2_bytes(const amxc_string_t *const string, char **bytes, uint32_t *len, const char *sep)
Creates an array of bytes from a hex binary string.
void amxc_string_triml(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim left.
void amxc_string_trimr(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim right.
bool amxc_string_is_numeric(const amxc_string_t *const string)
Checks if a string is fully numeric.
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
char * amxc_string_take_buffer(amxc_string_t *const string)
Takes the string buffer.