libamxc  1.10.3
C Generic Data Containers
amxc_string.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #if !defined(__AMXC_STRING_H__)
56 #define __AMXC_STRING_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <stddef.h>
64 #include <stdbool.h>
65 #include <stdint.h>
66 #include <stdarg.h>
67 
68 #include <amxc/amxc_common.h>
69 #include <amxc/amxc_llist.h>
70 
71 #define AMXC_STRING_MAX SIZE_MAX
72 
73 
95 #define amxc_string_from_llist_it(ll_it) \
96  amxc_container_of(ll_it, amxc_string_t, it)
97 
103 typedef struct _amxc_string {
104  char* buffer;
105  size_t length;
106  size_t last_used;
111 
120 typedef enum _amxc_string_flags {
123  amxc_string_overwrite = 0x01
125 
145 typedef int (* amxc_string_is_char_fn_t) (int c);
146 
158 typedef bool (* amxc_string_is_safe_cb_t) (const char* replacement);
159 
188 int amxc_string_new(amxc_string_t** string, const size_t length);
189 
204 void amxc_string_delete(amxc_string_t** string);
205 
232 int amxc_string_init(amxc_string_t* const string, const size_t length);
233 
241 void amxc_string_clean(amxc_string_t* const string);
242 
250 void amxc_string_reset(amxc_string_t* const string);
251 
264 int amxc_string_copy(amxc_string_t* const dest,
265  const amxc_string_t* const src);
266 
283 int amxc_string_grow(amxc_string_t* const string, const size_t length);
284 
303 int amxc_string_shrink(amxc_string_t* const string,
304  const size_t length);
305 
333 int amxc_string_set_at(amxc_string_t* const string,
334  const size_t pos,
335  const char* const text,
336  const size_t length,
337  const amxc_string_flags_t flags);
338 
360 int amxc_string_remove_at(amxc_string_t* const string,
361  const size_t pos,
362  size_t length);
363 
382 const char* amxc_string_get(const amxc_string_t* const string,
383  const size_t offset);
384 
405 char* amxc_string_take_buffer(amxc_string_t* const string);
406 
429 int amxc_string_push_buffer(amxc_string_t* const string,
430  char* buffer,
431  size_t length);
432 
459 char* amxc_string_dup(const amxc_string_t* const string,
460  const size_t start,
461  size_t length);
462 
482 
502 
523 
543 int amxc_string_vsetf(amxc_string_t* const string, const char* fmt, va_list ap);
544 
565 int amxc_string_setf(amxc_string_t* const string, const char* fmt, ...) \
566  __attribute__ ((format(printf, 2, 3)));
567 
575 int amxc_string_vsetf_checked(amxc_string_t* const string,
576  amxc_string_is_safe_cb_t is_safe_cb,
577  const char* fmt,
578  va_list args);
579 
635  amxc_string_is_safe_cb_t is_safe_cb,
636  const char* fmt, ...) \
637  __attribute__ ((format(printf, 3, 4)));
659 int amxc_string_vappendf(amxc_string_t* const string,
660  const char* fmt,
661  va_list ap);
662 
683 int amxc_string_appendf(amxc_string_t* const string, const char* fmt, ...) \
684  __attribute__ ((format(printf, 2, 3)));
685 
694  amxc_string_is_safe_cb_t is_safe_cb,
695  const char* fmt, va_list args);
696 
752  amxc_string_is_safe_cb_t is_safe_cb,
753  const char* fmt, ...) \
754  __attribute__ ((format(printf, 3, 4)));
755 
777 int amxc_string_vprependf(amxc_string_t* const string,
778  const char* fmt,
779  va_list ap);
780 
801 int amxc_string_prependf(amxc_string_t* const string, const char* fmt, ...) \
802  __attribute__ ((format(printf, 2, 3)));
803 
816 bool amxc_string_is_numeric(const amxc_string_t* const string);
817 
836 int amxc_string_search(const amxc_string_t* const string,
837  const char* needle,
838  uint32_t start_pos);
839 
861 int amxc_string_replace(amxc_string_t* const string,
862  const char* needle,
863  const char* newstr,
864  uint32_t max);
865 
879 int amxc_string_to_upper(amxc_string_t* const string);
880 
894 int amxc_string_to_lower(amxc_string_t* const string);
895 
921  const char* const text,
922  const size_t length) {
923  return amxc_string_set_at(string,
924  string != NULL ? string->last_used : 0,
925  text,
926  length,
928 }
929 
954  const char* const text,
955  const size_t length) {
956  return amxc_string_set_at(string, 0, text, length, amxc_string_no_flags);
957 }
958 
976 size_t amxc_string_buffer_length(const amxc_string_t* const string) {
977  return string != NULL ? string->length : 0;
978 }
979 
997 size_t amxc_string_text_length(const amxc_string_t* const string) {
998  return string != NULL ? string->last_used : 0;
999 }
1000 
1015 bool amxc_string_is_empty(const amxc_string_t* const string) {
1016  return string != NULL ? (string->last_used == 0) : true;
1017 }
1018 
1036  const size_t pos,
1037  const char* text,
1038  size_t length) {
1039  return amxc_string_set_at(string, pos, text, length, amxc_string_insert);
1040 }
1041 
1056 size_t amxc_string_set(amxc_string_t* const string, const char* text);
1057 
1075  const char bytes[],
1076  const uint32_t len,
1077  const char* sep);
1078 
1101 int amxc_string_hex_binary_2_bytes(const amxc_string_t* const string,
1102  char** bytes,
1103  uint32_t* len,
1104  const char* sep);
1105 #ifdef __cplusplus
1106 }
1107 #endif
1108 
1109 #endif // __AMXC_STRING_H__
#define AMXC_INLINE
Definition: amxc_common.h:64
Ambiorix linked list API header file.
void amxc_string_delete(amxc_string_t **string)
Frees the previously allocated string.
Definition: amxc_string.c:150
const char * amxc_string_get(const amxc_string_t *const string, const size_t offset)
Gets the content of the string buffer.
Definition: amxc_string.c:339
int amxc_string_to_lower(amxc_string_t *const string)
Converts all upper case characters to lower case.
Definition: amxc_string.c:856
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.
Definition: amxc_string.c:870
int amxc_string_setf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Sets the content of the string using printf like formatting.
AMXC_INLINE size_t amxc_string_buffer_length(const amxc_string_t *const string)
Gets the current size of the allocate string buffer.
Definition: amxc_string.h:976
int 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
int amxc_string_push_buffer(amxc_string_t *const string, char *buffer, size_t length)
Sets the string buffer.
Definition: amxc_string.c:372
int amxc_string_vsetf(amxc_string_t *const string, const char *fmt, va_list ap)
Sets the content of the string using printf like formatting.
Definition: amxc_string.c:476
size_t amxc_string_set(amxc_string_t *const string, const char *text)
Sets a 0 terminated string in the string buffer.
Definition: amxc_string.c:826
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
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.
Definition: amxc_string.c:789
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.
Definition: amxc_string.c:400
_amxc_string_flags
amxc_string_set_at possible flags
Definition: amxc_string.h:120
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
Definition: amxc_string.c:203
int amxc_string_to_upper(amxc_string_t *const string)
Converts all lower case characters to upper case.
Definition: amxc_string.c:842
int amxc_string_copy(amxc_string_t *const dest, const amxc_string_t *const src)
Copies the content.
Definition: amxc_string.c:215
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.
Definition: amxc_string.h:1035
int amxc_string_new(amxc_string_t **string, const size_t length)
Allocates a string.
Definition: amxc_string.c:118
AMXC_INLINE size_t amxc_string_text_length(const amxc_string_t *const string)
Gets the current size of the used string buffer.
Definition: amxc_string.h:997
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.
Definition: amxc_string.h:158
int amxc_string_search(const amxc_string_t *const string, const char *needle, uint32_t start_pos)
Searches a sub-string in a string.
Definition: amxc_string.c:765
int int amxc_string_vappendf(amxc_string_t *const string, const char *fmt, va_list ap)
Appends a formatted string to a string.
Definition: amxc_string.c:537
AMXC_INLINE int amxc_string_prepend(amxc_string_t *const string, const char *const text, const size_t length)
Inserts text at the beginning of the current content of the string buffer.
Definition: amxc_string.h:953
int int amxc_string_vappendf_checked(amxc_string_t *target_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
int amxc_string_appendf_checked(amxc_string_t *string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...) __attribute__((format(printf
Appends a formatted string while performing safety checks on the replacements.
int amxc_string_grow(amxc_string_t *const string, const size_t length)
Grows the string buffer.
Definition: amxc_string.c:240
int int amxc_string_vprependf(amxc_string_t *const string, const char *fmt, va_list ap)
Prepends a formatted string to a string.
Definition: amxc_string.c:692
int amxc_string_shrink(amxc_string_t *const string, const size_t length)
Shrinks the string buffer.
Definition: amxc_string.c:260
int(* amxc_string_is_char_fn_t)(int c)
Definition of the signature of the "is char" callback function.
Definition: amxc_string.h:145
void amxc_string_trim(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim.
Definition: amxc_string.c:471
int amxc_string_init(amxc_string_t *const string, const size_t length)
Initializes a string.
Definition: amxc_string.c:163
int amxc_string_setf_checked(amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...) __attribute__((format(printf
Sets the content of a string using printf like formatting while performing safety checks on the repla...
enum _amxc_string_flags amxc_string_flags_t
amxc_string_set_at possible flags
int amxc_string_appendf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Appends a formatted string to a string.
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.
Definition: amxc_string.c:314
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.
Definition: amxc_string.c:276
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.
Definition: amxc_string.c:901
void amxc_string_triml(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim left.
Definition: amxc_string.c:423
void amxc_string_trimr(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim right.
Definition: amxc_string.c:453
int bool amxc_string_is_numeric(const amxc_string_t *const string)
Checks if a string is fully numeric.
Definition: amxc_string.c:746
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
Definition: amxc_string.h:1015
int amxc_string_prependf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Prepends a formatted string to a string.
struct _amxc_string amxc_string_t
The string structure.
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
Definition: amxc_string.c:189
char * amxc_string_take_buffer(amxc_string_t *const string)
Takes the string buffer.
Definition: amxc_string.c:356
@ amxc_string_overwrite
Definition: amxc_string.h:123
@ amxc_string_no_flags
Definition: amxc_string.h:121
@ amxc_string_insert
Definition: amxc_string.h:122
The linked list iterator structure.
Definition: amxc_llist.h:215
The string structure.
Definition: amxc_string.h:103
char * buffer
Definition: amxc_string.h:104
amxc_llist_it_t it
Definition: amxc_string.h:108
size_t last_used
Definition: amxc_string.h:106
size_t length
Definition: amxc_string.h:105