libamxc  1.10.3
C Generic Data Containers
amxc_string.c File Reference

Ambiorix string API implementation. More...

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <amxc/amxc_variant.h>
#include <amxc/amxc_string.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Macros

#define REPLACE_PLACEHOLDER(string, actual_placeholder, status, args, searched_placeholder, type)
 

Functions

static int amxc_string_realloc (amxc_string_t *string, const size_t length)
 
static const char * s_get_format_placeholder (const char *input)
 
int amxc_string_new (amxc_string_t **string, const size_t length)
 Allocates a string. More...
 
void amxc_string_delete (amxc_string_t **string)
 Frees the previously allocated string. More...
 
int amxc_string_init (amxc_string_t *const string, const size_t length)
 Initializes a string. More...
 
void amxc_string_clean (amxc_string_t *const string)
 Frees the string buffer and reset length attributes. More...
 
void amxc_string_reset (amxc_string_t *const string)
 Resets the buffer, reset the content to all 0. More...
 
int amxc_string_copy (amxc_string_t *const dest, const amxc_string_t *const src)
 Copies the content. More...
 
int amxc_string_grow (amxc_string_t *const string, const size_t length)
 Grows the string buffer. More...
 
int amxc_string_shrink (amxc_string_t *const string, const size_t length)
 Shrinks the string buffer. More...
 
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. More...
 
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. More...
 
const char * amxc_string_get (const amxc_string_t *const string, const size_t offset)
 Gets the content of the string buffer. More...
 
char * amxc_string_take_buffer (amxc_string_t *const string)
 Takes the string buffer. More...
 
int amxc_string_push_buffer (amxc_string_t *const string, char *buffer, size_t length)
 Sets the string buffer. More...
 
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. More...
 
void amxc_string_triml (amxc_string_t *const string, amxc_string_is_char_fn_t fn)
 Trim left. More...
 
void amxc_string_trimr (amxc_string_t *const string, amxc_string_is_char_fn_t fn)
 Trim right. More...
 
void amxc_string_trim (amxc_string_t *const string, amxc_string_is_char_fn_t fn)
 Trim. More...
 
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. More...
 
int amxc_string_setf (amxc_string_t *const string, const char *fmt,...)
 
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 More...
 
int amxc_string_setf_checked (amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...)
 
int amxc_string_vappendf (amxc_string_t *const string, const char *fmt, va_list args)
 Appends a formatted string to a string. More...
 
int amxc_string_appendf (amxc_string_t *const string, const char *fmt,...)
 
static bool s_replace_percentage (amxc_string_t *string, const char *actual_placeholder, int *status)
 
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 More...
 
int amxc_string_appendf_checked (amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...)
 
int amxc_string_vprependf (amxc_string_t *const string, const char *fmt, va_list args)
 Prepends a formatted string to a string. More...
 
int amxc_string_prependf (amxc_string_t *const string, const char *fmt,...)
 
bool amxc_string_is_numeric (const amxc_string_t *const string)
 Checks if a string is fully numeric. More...
 
int amxc_string_search (const amxc_string_t *const string, const char *needle, uint32_t start_pos)
 Searches a sub-string in a string. More...
 
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. More...
 
size_t amxc_string_set (amxc_string_t *const string, const char *data)
 Sets a 0 terminated string in the string buffer. More...
 
int amxc_string_to_upper (amxc_string_t *const string)
 Converts all lower case characters to upper case. More...
 
int amxc_string_to_lower (amxc_string_t *const string)
 Converts all upper case characters to lower case. More...
 
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. More...
 
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. More...
 

Variables

static const char *const s_supported_format_placeholders []
 

Detailed Description

Ambiorix string API implementation.

Definition in file amxc_string.c.

Macro Definition Documentation

◆ REPLACE_PLACEHOLDER

#define REPLACE_PLACEHOLDER (   string,
  actual_placeholder,
  status,
  args,
  searched_placeholder,
  type 
)
Value:
(0 == strcmp(actual_placeholder, searched_placeholder) \
? status = amxc_string_appendf(string, actual_placeholder, va_arg(args, type)), true \
: false)
int amxc_string_appendf(amxc_string_t *const string, const char *fmt,...)
Definition: amxc_string.c:570
Returns
whether the actual placeholder was the searched placeholder

Definition at line 595 of file amxc_string.c.

Function Documentation

◆ amxc_string_appendf()

int amxc_string_appendf ( amxc_string_t *const  string,
const char *  fmt,
  ... 
)

Definition at line 570 of file amxc_string.c.

570  {
571  va_list args;
572  int retval = -1;
573  when_null(string, exit);
574  when_null(fmt, exit);
575 
576  va_start(args, fmt);
577  retval = amxc_string_vappendf(string, fmt, args);
578  va_end(args);
579 
580 exit:
581  return retval;
582 }
#define when_null(x, l)
Definition: amxc_macros.h:126
int amxc_string_vappendf(amxc_string_t *const string, const char *fmt, va_list args)
Appends a formatted string to a string.
Definition: amxc_string.c:537

◆ amxc_string_appendf_checked()

int amxc_string_appendf_checked ( amxc_string_t target_string,
amxc_string_is_safe_cb_t  is_safe_cb,
const char *  fmt,
  ... 
)

Definition at line 677 of file amxc_string.c.

678  {
679  va_list args;
680  int retval = -1;
681  when_null(target_string, exit);
682  when_null(fmt, exit);
683 
684  va_start(args, fmt);
685  retval = amxc_string_vappendf_checked(target_string, is_safe_cb, fmt, args);
686  va_end(args);
687 
688 exit:
689  return retval;
690 }
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
Definition: amxc_string.c:600

◆ amxc_string_prependf()

int amxc_string_prependf ( amxc_string_t *const  string,
const char *  fmt,
  ... 
)

Definition at line 732 of file amxc_string.c.

732  {
733  va_list args;
734  int retval = -1;
735  when_null(string, exit);
736  when_null(fmt, exit);
737 
738  va_start(args, fmt);
739  retval = amxc_string_vprependf(string, fmt, args);
740  va_end(args);
741 
742 exit:
743  return retval;
744 }
int amxc_string_vprependf(amxc_string_t *const string, const char *fmt, va_list args)
Prepends a formatted string to a string.
Definition: amxc_string.c:692

◆ amxc_string_realloc()

static int amxc_string_realloc ( amxc_string_t string,
const size_t  length 
)
static

Definition at line 75 of file amxc_string.c.

75  {
76  char* new_buffer = NULL;
77  int retval = -1;
78 
79  if(length == 0) {
80  free(string->buffer);
81  string->buffer = NULL;
82  string->length = 0;
83  retval = 0;
84  } else if(string->buffer != NULL) {
85  new_buffer = (char*) realloc(string->buffer, sizeof(char) * length + 1);
86  } else {
87  new_buffer = (char*) calloc(length + 1, sizeof(char));
88  }
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;
94  retval = 0;
95  }
96 
97  return retval;
98 }
char * buffer
Definition: amxc_string.h:104

◆ amxc_string_setf()

int amxc_string_setf ( amxc_string_t *const  string,
const char *  fmt,
  ... 
)

Definition at line 491 of file amxc_string.c.

491  {
492  va_list args;
493  int retval = -1;
494  when_null(string, exit);
495  when_null(fmt, exit);
496 
497  va_start(args, fmt);
498  retval = amxc_string_vsetf(string, fmt, args);
499  va_end(args);
500 
501 exit:
502  return retval;
503 }
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.
Definition: amxc_string.c:476

◆ amxc_string_setf_checked()

int amxc_string_setf_checked ( amxc_string_t target_string,
amxc_string_is_safe_cb_t  is_safe_cb,
const char *  fmt,
  ... 
)

Definition at line 521 of file amxc_string.c.

523  {
524  va_list args;
525  int retval = -1;
526  when_null(target_string, exit);
527  when_null(fmt, exit);
528 
529  va_start(args, fmt);
530  retval = amxc_string_vsetf_checked(target_string, is_safe_cb, fmt, args);
531  va_end(args);
532 
533 exit:
534  return retval;
535 }
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
Definition: amxc_string.c:505

◆ s_get_format_placeholder()

static const char* s_get_format_placeholder ( const char *  input)
static

If input starts with a placeholder (e.g. "%ihi"), return null-terminated placeholder (e.g. "%i"). Otherwise return NULL.

Definition at line 104 of file amxc_string.c.

104  {
105  size_t nb_supported = 0;
106  when_null(input, error);
107  nb_supported = sizeof(s_supported_format_placeholders) / sizeof(s_supported_format_placeholders[0]);
108  for(size_t i = 0; i < nb_supported; i++) {
109  const char* const candidate = s_supported_format_placeholders[i];
110  if(0 == strncmp(candidate, input, strlen(candidate))) {
111  return candidate;
112  }
113  }
114 error:
115  return NULL;
116 }
static const char *const s_supported_format_placeholders[]
Definition: amxc_string.c:72

◆ s_replace_percentage()

static bool s_replace_percentage ( amxc_string_t string,
const char *  actual_placeholder,
int *  status 
)
static
Returns
whether the actual placeholder was "%%""

Definition at line 585 of file amxc_string.c.

585  {
586  if(0 == strcmp(actual_placeholder, "%%")) {
587  *status = amxc_string_append(string, "%", 1);
588  return true;
589  } else {
590  return false;
591  }
592 }
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.
Definition: amxc_string.h:920

Variable Documentation

◆ s_supported_format_placeholders

const char* const s_supported_format_placeholders[]
static
Initial value:
= { "%s", "%d", "%lld", "%ld", "%i",
"%lli", "%li", "%u", "%llu", "%lu", "%x", "%llx", "%lx", "%%", "%c", "%f", "%F", "%X"}

Definition at line 72 of file amxc_string.c.