Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <stdbool.h>
11#include <stdint.h>
12#include <stddef.h>
13#include <stdarg.h>
14#include <m-core.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
21#define FURI_STRING_FAILURE ((size_t) - 1)
22
24typedef struct FuriString FuriString;
25
26//---------------------------------------------------------------------------
27// Constructors
28//---------------------------------------------------------------------------
29
35
45
54FuriString* furi_string_alloc_set_str(const char cstr_source[]);
55
65FuriString* furi_string_alloc_printf(const char format[], ...)
66 _ATTRIBUTE((__format__(__printf__, 1, 2)));
67
77FuriString* furi_string_alloc_vprintf(const char format[], va_list args);
78
88
89//---------------------------------------------------------------------------
90// Destructors
91//---------------------------------------------------------------------------
92
97void furi_string_free(FuriString* string);
98
99//---------------------------------------------------------------------------
100// String memory management
101//---------------------------------------------------------------------------
102
111void furi_string_reserve(FuriString* string, size_t size);
112
119void furi_string_reset(FuriString* string);
120
128void furi_string_swap(FuriString* string_1, FuriString* string_2);
129
137void furi_string_move(FuriString* string_1, FuriString* string_2);
138
145size_t furi_string_hash(const FuriString* string);
146
153size_t furi_string_size(const FuriString* string);
154
161bool furi_string_empty(const FuriString* string);
162
163//---------------------------------------------------------------------------
164// Getters
165//---------------------------------------------------------------------------
166
176char furi_string_get_char(const FuriString* string, size_t index);
177
184const char* furi_string_get_cstr(const FuriString* string);
185
186//---------------------------------------------------------------------------
187// Setters
188//---------------------------------------------------------------------------
189
197void furi_string_set(FuriString* string, FuriString* source);
198
206void furi_string_set_str(FuriString* string, const char source[]);
207
214void furi_string_set_strn(FuriString* string, const char source[], size_t length);
215
222void furi_string_set_char(FuriString* string, size_t index, const char c);
223
231void furi_string_set_n(FuriString* string, const FuriString* source, size_t offset, size_t length);
232
241int furi_string_printf(FuriString* string, const char format[], ...)
242 _ATTRIBUTE((__format__(__printf__, 2, 3)));
243
252int furi_string_vprintf(FuriString* string, const char format[], va_list args);
253
254//---------------------------------------------------------------------------
255// Appending
256//---------------------------------------------------------------------------
257
263void furi_string_push_back(FuriString* string, char c);
264
272void furi_string_cat(FuriString* string_1, const FuriString* string_2);
273
281void furi_string_cat_str(FuriString* string_1, const char cstring_2[]);
282
291int furi_string_cat_printf(FuriString* string, const char format[], ...)
292 _ATTRIBUTE((__format__(__printf__, 2, 3)));
293
302int furi_string_cat_vprintf(FuriString* string, const char format[], va_list args);
303
304//---------------------------------------------------------------------------
305// Comparators
306//---------------------------------------------------------------------------
307
315int furi_string_cmp(const FuriString* string_1, const FuriString* string_2);
316
324int furi_string_cmp_str(const FuriString* string_1, const char cstring_2[]);
325
336int furi_string_cmpi(const FuriString* string_1, const FuriString* string_2);
337
348int furi_string_cmpi_str(const FuriString* string_1, const char cstring_2[]);
349
350//---------------------------------------------------------------------------
351// Search
352//---------------------------------------------------------------------------
353
363size_t furi_string_search(const FuriString* string, const FuriString* needle, size_t start);
364
374size_t furi_string_search_str(const FuriString* string, const char needle[], size_t start);
375
385size_t furi_string_search_char(const FuriString* string, char c, size_t start);
386
396size_t furi_string_search_rchar(const FuriString* string, char c, size_t start);
397
398//---------------------------------------------------------------------------
399// Equality
400//---------------------------------------------------------------------------
401
409bool furi_string_equal(const FuriString* string_1, const FuriString* string_2);
410
418bool furi_string_equal_str(const FuriString* string_1, const char cstring_2[]);
419
420//---------------------------------------------------------------------------
421// Replace
422//---------------------------------------------------------------------------
423
432void furi_string_replace_at(FuriString* string, size_t pos, size_t len, const char replace[]);
433
444size_t
445 furi_string_replace(FuriString* string, FuriString* needle, FuriString* replace, size_t start);
446
458 FuriString* string,
459 const char needle[],
460 const char replace[],
461 size_t start);
462
470 FuriString* string,
471 const FuriString* needle,
472 const FuriString* replace);
473
480void furi_string_replace_all_str(FuriString* string, const char needle[], const char replace[]);
481
482//---------------------------------------------------------------------------
483// Start / End tests
484//---------------------------------------------------------------------------
485
493bool furi_string_start_with(const FuriString* string, const FuriString* start);
494
502bool furi_string_start_with_str(const FuriString* string, const char start[]);
503
511bool furi_string_end_with(const FuriString* string, const FuriString* end);
512
520bool furi_string_end_withi(const FuriString* string, const FuriString* end);
521
529bool furi_string_end_with_str(const FuriString* string, const char end[]);
530
538bool furi_string_end_withi_str(const FuriString* string, const char end[]);
539
540//---------------------------------------------------------------------------
541// Trim
542//---------------------------------------------------------------------------
543
549void furi_string_left(FuriString* string, size_t index);
550
556void furi_string_right(FuriString* string, size_t index);
557
566void furi_string_mid(FuriString* string, size_t index, size_t size);
567
573void furi_string_trim(FuriString* string, const char chars[]);
574
575//---------------------------------------------------------------------------
576// UTF8
577//---------------------------------------------------------------------------
578
580typedef unsigned int FuriStringUnicodeValue;
581
588size_t furi_string_utf8_length(FuriString* string);
589
596
598typedef enum {
599 FuriStringUTF8StateStarting,
600 FuriStringUTF8StateDecoding1,
601 FuriStringUTF8StateDecoding2,
602 FuriStringUTF8StateDecoding3,
603 FuriStringUTF8StateError
605
618
619//---------------------------------------------------------------------------
620// Lasciate ogne speranza, voi ch’entrate
621//---------------------------------------------------------------------------
622
631#define FURI_STRING_SELECT1(func1, func2, a) \
632 _Generic((a), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
633 a)
634
636#define FURI_STRING_SELECT2(func1, func2, a, b) \
637 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
638 a, b)
639
641#define FURI_STRING_SELECT3(func1, func2, a, b, c) \
642 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
643 a, b, c)
644
646#define FURI_STRING_SELECT4(func1, func2, a, b, c, d) \
647 _Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
648 a, b, c, d)
649
654#define furi_string_alloc_set(a) \
655 FURI_STRING_SELECT1(furi_string_alloc_set, furi_string_alloc_set_str, a)
656
661#define furi_string_set(a, b) FURI_STRING_SELECT2(furi_string_set, furi_string_set_str, a, b)
662
668#define furi_string_cmp(a, b) FURI_STRING_SELECT2(furi_string_cmp, furi_string_cmp_str, a, b)
669
675#define furi_string_cmpi(a, b) FURI_STRING_SELECT2(furi_string_cmpi, furi_string_cmpi_str, a, b)
676
681#define furi_string_equal(a, b) FURI_STRING_SELECT2(furi_string_equal, furi_string_equal_str, a, b)
682
687#define furi_string_replace_all(a, b, c) \
688 FURI_STRING_SELECT3(furi_string_replace_all, furi_string_replace_all_str, a, b, c)
689
694#define furi_string_search(...) \
695 M_APPLY( \
696 FURI_STRING_SELECT3, \
697 furi_string_search, \
698 furi_string_search_str, \
699 M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
704#define furi_string_search_str(...) furi_string_search_str(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
705
710#define furi_string_start_with(a, b) \
711 FURI_STRING_SELECT2(furi_string_start_with, furi_string_start_with_str, a, b)
712
717#define furi_string_end_with(a, b) \
718 FURI_STRING_SELECT2(furi_string_end_with, furi_string_end_with_str, a, b)
719
724#define furi_string_end_withi(a, b) \
725 FURI_STRING_SELECT2(furi_string_end_withi, furi_string_end_withi_str, a, b)
726
731#define furi_string_cat(a, b) FURI_STRING_SELECT2(furi_string_cat, furi_string_cat_str, a, b)
732
737#define furi_string_trim(...) furi_string_trim(M_DEFAULT_ARGS(2, (" \n\r\t"), __VA_ARGS__))
738
743#define furi_string_search_char(...) furi_string_search_char(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
744
749#define furi_string_search_rchar(...) furi_string_search_rchar(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
750
755#define furi_string_replace(...) \
756 M_APPLY( \
757 FURI_STRING_SELECT4, \
758 furi_string_replace, \
759 furi_string_replace_str, \
760 M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
761
766#define furi_string_replace_str(...) furi_string_replace_str(M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
767
769#define F_STR_INIT(a) ((a) = furi_string_alloc())
770
772#define F_STR_INIT_SET(a, b) ((a) = furi_string_alloc_set(b))
773
775#define F_STR_INIT_MOVE(a, b) ((a) = furi_string_alloc_move(b))
776
778#define FURI_STRING_OPLIST \
779 (INIT(F_STR_INIT), \
780 INIT_SET(F_STR_INIT_SET), \
781 SET(furi_string_set), \
782 INIT_MOVE(F_STR_INIT_MOVE), \
783 MOVE(furi_string_move), \
784 SWAP(furi_string_swap), \
785 RESET(furi_string_reset), \
786 EMPTY_P(furi_string_empty), \
787 CLEAR(furi_string_free), \
788 HASH(furi_string_hash), \
789 EQUAL(furi_string_equal), \
790 CMP(furi_string_cmp), \
791 TYPE(FuriString*))
792
793#ifdef __cplusplus
794}
795#endif
#define furi_string_set(a, b)
Set the string content to string (or C string).
Definition string.h:661
FuriString FuriString * furi_string_alloc_vprintf(const char format[], va_list args)
Allocate new FuriString and printf to it.
Definition string.c:52
char furi_string_get_char(const FuriString *string, size_t index)
Get the character at the given index.
Definition string.c:93
bool furi_string_end_withi_str(const FuriString *string, const char end[])
Test if the string ends with the given C string (case insensitive according to the current locale).
Definition string.c:230
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:202
#define furi_string_cmp(a, b)
Compare string with string (or C string) and return the sort order.
Definition string.h:668
void furi_string_push_back(FuriString *string, char c)
Append a character to the string.
Definition string.c:149
#define furi_string_end_withi(a, b)
Test if the string ends with the given string (or C string) (case insensitive according to the curren...
Definition string.h:724
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:145
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:226
void furi_string_cat_str(FuriString *string_1, const char cstring_2[])
Append a C string to the string.
Definition string.c:272
size_t furi_string_size(const FuriString *string)
Get string size (usually length, but not for UTF-8)
Definition string.c:153
#define furi_string_search(...)
Search for a string (or C string) in a string.
Definition string.h:694
void furi_string_swap(FuriString *string_1, FuriString *string_2)
Swap two strings.
Definition string.c:79
void furi_string_free(FuriString *string)
Free FuriString.
Definition string.c:65
void furi_string_right(FuriString *string, size_t index)
Trim the string right from the 'index' position to the last position.
Definition string.c:256
void furi_string_set_str(FuriString *string, const char source[])
Set the string to the other C string.
Definition string.c:105
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:276
#define furi_string_cat(a, b)
Append a string (or C string) to the string.
Definition string.h:731
FuriString * furi_string_alloc_set_str(const char cstr_source[])
Allocate new FuriString and set it to C string.
Definition string.c:38
#define furi_string_search_char(...)
Search for a character in a string.
Definition string.h:743
FuriStringUTF8State
State of the UTF8 decoding machine state.
Definition string.h:598
#define furi_string_trim(...)
Trim a string from the given set of characters (default is " \n\r\t").
Definition string.h:737
int int furi_string_vprintf(FuriString *string, const char format[], va_list args)
Format in the string the given printf format.
Definition string.c:165
const char * furi_string_get_cstr(const FuriString *string)
Return the string view a classic C string.
Definition string.c:97
bool furi_string_empty(const FuriString *string)
Check that string is empty or not.
Definition string.c:185
unsigned int FuriStringUnicodeValue
An unicode value.
Definition string.h:580
#define furi_string_search_str(...)
Search for a C string in a string.
Definition string.h:704
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:121
void furi_string_move(FuriString *string_1, FuriString *string_2)
Move string_2 content to string_1.
Definition string.c:83
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:318
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:687
void furi_string_reset(FuriString *string)
Reset string.
Definition string.c:74
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:177
#define furi_string_search_rchar(...)
Reverse Search for a character in a string.
Definition string.h:749
#define furi_string_alloc_set(a)
Allocate new FuriString and set it content to string (or C string).
Definition string.h:654
size_t furi_string_utf8_length(FuriString *string)
Compute the length in UTF8 characters in the string.
Definition string.c:280
FuriString * furi_string_alloc(void)
Allocate new FuriString.
Definition string.c:26
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:129
#define furi_string_end_with(a, b)
Test if the string ends with the given string (or C string).
Definition string.h:717
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:710
void furi_string_reserve(FuriString *string, size_t size)
Reserve memory for string.
Definition string.c:70
#define furi_string_replace_str(...)
Replace a C string to another C string in a string.
Definition string.h:766
#define furi_string_equal(a, b)
Test if the string is equal to the string (or C string).
Definition string.h:681
void furi_string_left(FuriString *string, size_t index)
Trim the string left to the first 'index' bytes.
Definition string.c:252
void furi_string_mid(FuriString *string, size_t index, size_t size)
Trim the string from position index to size bytes.
Definition string.c:260
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:189
#define furi_string_replace(...)
Replace a string to another string (or C string to another C string) in a string.
Definition string.h:755
void furi_string_set_char(FuriString *string, size_t index, const char c)
Set the character at the given index.
Definition string.c:113
void furi_string_utf8_push(FuriString *string, FuriStringUnicodeValue unicode)
Push unicode into string, encoding it in UTF8.
Definition string.c:284
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:214
size_t furi_string_hash(const FuriString *string)
Compute a hash for the string.
Definition string.c:89
#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:675
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:109
FuriString * furi_string_alloc_move(FuriString *source)
Allocate new FuriString and move source string content to it.
Definition string.c:58
Definition string.c:4