Flipper Zero Firmware
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <stdbool.h>
8#include <stdint.h>
9#include <stddef.h>
10#include <stdarg.h>
11#include <m-core.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
20#define FURI_STRING_FAILURE ((size_t) - 1)
21
25typedef struct FuriString FuriString;
26
27//---------------------------------------------------------------------------
28// Constructors
29//---------------------------------------------------------------------------
30
36
44
51FuriString* furi_string_alloc_set_str(const char cstr_source[]);
52
60FuriString* furi_string_alloc_printf(const char format[], ...)
61 _ATTRIBUTE((__format__(__printf__, 1, 2)));
62
70FuriString* furi_string_alloc_vprintf(const char format[], va_list args);
71
79
80//---------------------------------------------------------------------------
81// Destructors
82//---------------------------------------------------------------------------
83
88void furi_string_free(FuriString* string);
89
90//---------------------------------------------------------------------------
91// String memory management
92//---------------------------------------------------------------------------
93
100void furi_string_reserve(FuriString* string, size_t size);
101
107void furi_string_reset(FuriString* string);
108
115void furi_string_swap(FuriString* string_1, FuriString* string_2);
116
123void furi_string_move(FuriString* string_1, FuriString* string_2);
124
130size_t furi_string_hash(const FuriString* string);
131
137size_t furi_string_size(const FuriString* string);
138
144bool furi_string_empty(const FuriString* string);
145
146//---------------------------------------------------------------------------
147// Getters
148//---------------------------------------------------------------------------
149
157char furi_string_get_char(const FuriString* string, size_t index);
158
164const char* furi_string_get_cstr(const FuriString* string);
165
166//---------------------------------------------------------------------------
167// Setters
168//---------------------------------------------------------------------------
169
176void furi_string_set(FuriString* string, FuriString* source);
177
184void furi_string_set_str(FuriString* string, const char source[]);
185
192void furi_string_set_strn(FuriString* string, const char source[], size_t length);
193
200void furi_string_set_char(FuriString* string, size_t index, const char c);
201
209void furi_string_set_n(FuriString* string, const FuriString* source, size_t offset, size_t length);
210
218int furi_string_printf(FuriString* string, const char format[], ...)
219 _ATTRIBUTE((__format__(__printf__, 2, 3)));
220
228int furi_string_vprintf(FuriString* string, const char format[], va_list args);
229
230//---------------------------------------------------------------------------
231// Appending
232//---------------------------------------------------------------------------
233
239void furi_string_push_back(FuriString* string, char c);
240
247void furi_string_cat(FuriString* string_1, const FuriString* string_2);
248
255void furi_string_cat_str(FuriString* string_1, const char cstring_2[]);
256
264int furi_string_cat_printf(FuriString* string, const char format[], ...)
265 _ATTRIBUTE((__format__(__printf__, 2, 3)));
266
274int furi_string_cat_vprintf(FuriString* string, const char format[], va_list args);
275
276//---------------------------------------------------------------------------
277// Comparators
278//---------------------------------------------------------------------------
279
286int furi_string_cmp(const FuriString* string_1, const FuriString* string_2);
287
294int furi_string_cmp_str(const FuriString* string_1, const char cstring_2[]);
295
303int furi_string_cmpi(const FuriString* string_1, const FuriString* string_2);
304
312int furi_string_cmpi_str(const FuriString* string_1, const char cstring_2[]);
313
314//---------------------------------------------------------------------------
315// Search
316//---------------------------------------------------------------------------
317
327size_t furi_string_search(const FuriString* string, const FuriString* needle, size_t start);
328
337size_t furi_string_search_str(const FuriString* string, const char needle[], size_t start);
338
348size_t furi_string_search_char(const FuriString* string, char c, size_t start);
349
359size_t furi_string_search_rchar(const FuriString* string, char c, size_t start);
360
361//---------------------------------------------------------------------------
362// Equality
363//---------------------------------------------------------------------------
364
371bool furi_string_equal(const FuriString* string_1, const FuriString* string_2);
372
379bool furi_string_equal_str(const FuriString* string_1, const char cstring_2[]);
380
381//---------------------------------------------------------------------------
382// Replace
383//---------------------------------------------------------------------------
384
392void furi_string_replace_at(FuriString* string, size_t pos, size_t len, const char replace[]);
393
404size_t
405 furi_string_replace(FuriString* string, FuriString* needle, FuriString* replace, size_t start);
406
418 FuriString* string,
419 const char needle[],
420 const char replace[],
421 size_t start);
422
430 FuriString* string,
431 const FuriString* needle,
432 const FuriString* replace);
433
440void furi_string_replace_all_str(FuriString* string, const char needle[], const char replace[]);
441
442//---------------------------------------------------------------------------
443// Start / End tests
444//---------------------------------------------------------------------------
445
452bool furi_string_start_with(const FuriString* string, const FuriString* start);
453
460bool furi_string_start_with_str(const FuriString* string, const char start[]);
461
468bool furi_string_end_with(const FuriString* string, const FuriString* end);
469
476bool furi_string_end_with_str(const FuriString* string, const char end[]);
477
478//---------------------------------------------------------------------------
479// Trim
480//---------------------------------------------------------------------------
481
487void furi_string_left(FuriString* string, size_t index);
488
494void furi_string_right(FuriString* string, size_t index);
495
503void furi_string_mid(FuriString* string, size_t index, size_t size);
504
510void furi_string_trim(FuriString* string, const char chars[]);
511
512//---------------------------------------------------------------------------
513// UTF8
514//---------------------------------------------------------------------------
515
519typedef unsigned int FuriStringUnicodeValue;
520
526size_t furi_string_utf8_length(FuriString* string);
527
534
538typedef enum {
539 FuriStringUTF8StateStarting,
540 FuriStringUTF8StateDecoding1,
541 FuriStringUTF8StateDecoding2,
542 FuriStringUTF8StateDecoding3,
543 FuriStringUTF8StateError
545
556
557//---------------------------------------------------------------------------
558// Lasciate ogne speranza, voi ch’entrate
559//---------------------------------------------------------------------------
560
571#define FURI_STRING_SELECT1(func1, func2, a) \
572 _Generic((a), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
573 a)
574
578#define FURI_STRING_SELECT2(func1, func2, a, b) \
579 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
580 a, b)
581
585#define FURI_STRING_SELECT3(func1, func2, a, b, c) \
586 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
587 a, b, c)
588
592#define FURI_STRING_SELECT4(func1, func2, a, b, c, d) \
593 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
594 a, b, c, d)
595
600#define furi_string_alloc_set(a) \
601 FURI_STRING_SELECT1(furi_string_alloc_set, furi_string_alloc_set_str, a)
602
607#define furi_string_set(a, b) FURI_STRING_SELECT2(furi_string_set, furi_string_set_str, a, b)
608
614#define furi_string_cmp(a, b) FURI_STRING_SELECT2(furi_string_cmp, furi_string_cmp_str, a, b)
615
621#define furi_string_cmpi(a, b) FURI_STRING_SELECT2(furi_string_cmpi, furi_string_cmpi_str, a, b)
622
627#define furi_string_equal(a, b) FURI_STRING_SELECT2(furi_string_equal, furi_string_equal_str, a, b)
628
633#define furi_string_replace_all(a, b, c) \
634 FURI_STRING_SELECT3(furi_string_replace_all, furi_string_replace_all_str, a, b, c)
635
640#define furi_string_search(...) \
641 M_APPLY( \
642 FURI_STRING_SELECT3, \
643 furi_string_search, \
644 furi_string_search_str, \
645 M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
650#define furi_string_search_str(...) furi_string_search_str(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
651
656#define furi_string_start_with(a, b) \
657 FURI_STRING_SELECT2(furi_string_start_with, furi_string_start_with_str, a, b)
658
663#define furi_string_end_with(a, b) \
664 FURI_STRING_SELECT2(furi_string_end_with, furi_string_end_with_str, a, b)
665
670#define furi_string_cat(a, b) FURI_STRING_SELECT2(furi_string_cat, furi_string_cat_str, a, b)
671
676#define furi_string_trim(...) furi_string_trim(M_DEFAULT_ARGS(2, (" \n\r\t"), __VA_ARGS__))
677
682#define furi_string_search_char(...) furi_string_search_char(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
683
688#define furi_string_search_rchar(...) furi_string_search_rchar(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
689
694#define furi_string_replace(...) \
695 M_APPLY( \
696 FURI_STRING_SELECT4, \
697 furi_string_replace, \
698 furi_string_replace_str, \
699 M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
700
705#define furi_string_replace_str(...) furi_string_replace_str(M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
706
710#define F_STR_INIT(a) ((a) = furi_string_alloc())
711
715#define F_STR_INIT_SET(a, b) ((a) = furi_string_alloc_set(b))
716
720#define F_STR_INIT_MOVE(a, b) ((a) = furi_string_alloc_move(b))
721
725#define FURI_STRING_OPLIST \
726 (INIT(F_STR_INIT), \
727 INIT_SET(F_STR_INIT_SET), \
728 SET(furi_string_set), \
729 INIT_MOVE(F_STR_INIT_MOVE), \
730 MOVE(furi_string_move), \
731 SWAP(furi_string_swap), \
732 RESET(furi_string_reset), \
733 EMPTY_P(furi_string_empty), \
734 CLEAR(furi_string_free), \
735 HASH(furi_string_hash), \
736 EQUAL(furi_string_equal), \
737 CMP(furi_string_cmp), \
738 TYPE(FuriString*))
739
740#ifdef __cplusplus
741}
742#endif
#define furi_string_set(a, b)
Set the string content to string (or C string).
Definition string.h:607
FuriString FuriString * furi_string_alloc_vprintf(const char format[], va_list args)
Allocate new FuriString and printf to it.
Definition string.c:51
char furi_string_get_char(const FuriString *string, size_t index)
Get the character at the given index.
Definition string.c:92
void furi_string_replace_all_str(FuriString *string, const char needle[], const char replace[])
Replace all occurrences of 'needle' C string into 'replace' C string.
Definition string.c:201
#define furi_string_cmp(a, b)
Compare string with string (or C string) and return the sort order.
Definition string.h:614
void furi_string_push_back(FuriString *string, char c)
Append a character to the string.
Definition string.c:148
bool furi_string_equal_str(const FuriString *string_1, const char cstring_2[])
Test if the string is equal to the C string.
Definition string.c:144
bool furi_string_end_with_str(const FuriString *string, const char end[])
Test if the string ends with the given C string.
Definition string.c:221
void furi_string_cat_str(FuriString *string_1, const char cstring_2[])
Append a C string to the string.
Definition string.c:253
size_t furi_string_size(const FuriString *string)
Get string size (usually length, but not for UTF-8)
Definition string.c:152
#define furi_string_search(...)
Search for a string (or C string) in a string (string, [c]string[, start=0])
Definition string.h:640
void furi_string_swap(FuriString *string_1, FuriString *string_2)
Swap two strings.
Definition string.c:78
void furi_string_free(FuriString *string)
Free FuriString.
Definition string.c:64
void furi_string_right(FuriString *string, size_t index)
Trim the string right from the 'index' position to the last position.
Definition string.c:237
void furi_string_set_str(FuriString *string, const char source[])
Set the string to the other C string.
Definition string.c:104
void furi_string_set_n(FuriString *string, const FuriString *source, size_t offset, size_t length)
Set the string to the n first characters of other one.
Definition string.c:257
#define furi_string_cat(a, b)
Append a string (or C string) to the string.
Definition string.h:670
FuriString * furi_string_alloc_set_str(const char cstr_source[])
Allocate new FuriString and set it to C string.
Definition string.c:37
#define furi_string_search_char(...)
Search for a character in a string.
Definition string.h:682
FuriStringUTF8State
State of the UTF8 decoding machine state.
Definition string.h:538
#define furi_string_trim(...)
Trim a string from the given set of characters (default is " \n\r\t").
Definition string.h:676
int int furi_string_vprintf(FuriString *string, const char format[], va_list args)
Format in the string the given printf format.
Definition string.c:164
const char * furi_string_get_cstr(const FuriString *string)
Return the string view a classic C string.
Definition string.c:96
bool furi_string_empty(const FuriString *string)
Check that string is empty or not.
Definition string.c:184
unsigned int FuriStringUnicodeValue
An unicode value.
Definition string.h:519
#define furi_string_search_str(...)
Search for a C string in a string (string, cstring[, start=0])
Definition string.h:650
int furi_string_cmp_str(const FuriString *string_1, const char cstring_2[])
Compare string with C string and return the sort order.
Definition string.c:120
void furi_string_move(FuriString *string_1, FuriString *string_2)
Move string_2 content to string_1.
Definition string.c:82
FuriString * furi_string_alloc_printf(const char format[],...) _ATTRIBUTE((__format__(__printf__
Allocate new FuriString and printf to it.
void furi_string_utf8_decode(char c, FuriStringUTF8State *state, FuriStringUnicodeValue *unicode)
Main generic UTF8 decoder.
Definition string.c:299
int furi_string_cat_printf(FuriString *string, const char format[],...) _ATTRIBUTE((__format__(__printf__
Append to the string the formatted string of the given printf format.
#define furi_string_replace_all(a, b, c)
Replace all occurrences of string into string (or C string to another C string) in a string.
Definition string.h:633
void furi_string_reset(FuriString *string)
Reset string.
Definition string.c:73
int int furi_string_cat_vprintf(FuriString *string, const char format[], va_list args)
Append to the string the formatted string of the given printf format.
Definition string.c:176
#define furi_string_search_rchar(...)
Reverse Search for a character in a string.
Definition string.h:688
#define furi_string_alloc_set(a)
Allocate new FuriString and set it content to string (or C string).
Definition string.h:600
size_t furi_string_utf8_length(FuriString *string)
Compute the length in UTF8 characters in the string.
Definition string.c:261
FuriString * furi_string_alloc(void)
Allocate new FuriString.
Definition string.c:25
int furi_string_cmpi_str(const FuriString *string_1, const char cstring_2[])
Compare string with C string (case insensitive according to the current locale) and return the sort o...
Definition string.c:128
#define furi_string_end_with(a, b)
Test if the string ends with the given string (or C string).
Definition string.h:663
int furi_string_printf(FuriString *string, const char format[],...) _ATTRIBUTE((__format__(__printf__
Format in the string the given printf format.
#define furi_string_start_with(a, b)
Test if the string starts with the given string (or C string).
Definition string.h:656
void furi_string_reserve(FuriString *string, size_t size)
Reserve memory for string.
Definition string.c:69
#define furi_string_replace_str(...)
Replace a C string to another C string in a string.
Definition string.h:705
#define furi_string_equal(a, b)
Test if the string is equal to the string (or C string).
Definition string.h:627
void furi_string_left(FuriString *string, size_t index)
Trim the string left to the first 'index' bytes.
Definition string.c:233
void furi_string_mid(FuriString *string, size_t index, size_t size)
Trim the string from position index to size bytes.
Definition string.c:241
void furi_string_replace_at(FuriString *string, size_t pos, size_t len, const char replace[])
Replace in the string the sub-string at position 'pos' for 'len' bytes into the C string 'replace'.
Definition string.c:188
#define furi_string_replace(...)
Replace a string to another string (or C string to another C string) in a string.
Definition string.h:694
void furi_string_set_char(FuriString *string, size_t index, const char c)
Set the character at the given index.
Definition string.c:112
void furi_string_utf8_push(FuriString *string, FuriStringUnicodeValue unicode)
Push unicode into string, encoding it in UTF8.
Definition string.c:265
bool furi_string_start_with_str(const FuriString *string, const char start[])
Test if the string starts with the given C string.
Definition string.c:213
size_t furi_string_hash(const FuriString *string)
Compute a hash for the string.
Definition string.c:88
#define furi_string_cmpi(a, b)
Compare string with string (or C string) (case insensitive according to the current locale) and retur...
Definition string.h:621
void furi_string_set_strn(FuriString *string, const char source[], size_t length)
Set the string to the n first characters of the C string.
Definition string.c:108
FuriString * furi_string_alloc_move(FuriString *source)
Allocate new FuriString and move source string content to it.
Definition string.c:57
Definition string.c:4