2024-07-29 16:06:08 +03:00
|
|
|
|
/** Furi string container
|
|
|
|
|
*
|
|
|
|
|
* And various method to manipulate strings
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* @file string.h
|
|
|
|
|
*/
|
2024-07-29 16:06:08 +03:00
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <m-core.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Furi string failure constant. */
|
2024-07-05 20:27:21 +03:00
|
|
|
|
#define FURI_STRING_FAILURE ((size_t) - 1)
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Furi string primitive. */
|
2022-10-05 18:15:23 +03:00
|
|
|
|
typedef struct FuriString FuriString;
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Constructors
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString.
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
2024-03-19 17:43:52 +03:00
|
|
|
|
FuriString* furi_string_alloc(void);
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and set it to string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Allocate & Set the string a to the string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param source The source FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the new instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
FuriString* furi_string_alloc_set(const FuriString* source);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and set it to C string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Allocate & Set the string a to the C string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param cstr_source The C-string instance
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the new instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
FuriString* furi_string_alloc_set_str(const char cstr_source[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and printf to it.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Initialize and set a string to the given formatted value.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param format The printf format
|
|
|
|
|
* @param[in] ... args to format
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the new instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
2022-10-07 16:35:15 +03:00
|
|
|
|
FuriString* furi_string_alloc_printf(const char format[], ...)
|
|
|
|
|
_ATTRIBUTE((__format__(__printf__, 1, 2)));
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and printf to it.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Initialize and set a string to the given formatted value.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param format The printf format
|
|
|
|
|
* @param args The format arguments
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the new instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
FuriString* furi_string_alloc_vprintf(const char format[], va_list args);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and move source string content to it.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Allocate the string, set it to the other one, and destroy the other one.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param source The source FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return pointer to the new instance of FuriString
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
FuriString* furi_string_alloc_move(FuriString* source);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Destructors
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Free FuriString.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance to free
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_free(FuriString* string);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// String memory management
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Reserve memory for string.
|
|
|
|
|
*
|
|
|
|
|
* Modify the string capacity to be able to handle at least 'alloc' characters
|
|
|
|
|
* (including final null char).
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param size The size to reserve
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_reserve(FuriString* string, size_t size);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Reset string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Make the string empty.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_reset(FuriString* string);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Swap two strings.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Swap the two strings string_1 and string_2.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The FuriString instance 1
|
|
|
|
|
* @param string_2 The FuriString instance 2
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_swap(FuriString* string_1, FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Move string_2 content to string_1.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Set the string to the other one, and destroy the other one.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The FuriString instance 1
|
|
|
|
|
* @param string_2 The FuriString instance 2
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_move(FuriString* string_1, FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compute a hash for the string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return hash value
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_hash(const FuriString* string);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Get string size (usually length, but not for UTF-8)
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return size of the string
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_size(const FuriString* string);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Check that string is empty or not
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return true if empty otherwise false
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_empty(const FuriString* string);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Getters
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Get the character at the given index.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Return the selected character of the string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param index The index
|
|
|
|
|
*
|
|
|
|
|
* @return character at index
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
char furi_string_get_char(const FuriString* string, size_t index);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Return the string view a classic C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return const C-string, usable till first container change
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
const char* furi_string_get_cstr(const FuriString* string);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Setters
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the string to the other string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Set the string to the source string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param source The source
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_set(FuriString* string, FuriString* source);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the string to the other C string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Set the string to the source C string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param source The source
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_set_str(FuriString* string, const char source[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the string to the n first characters of the C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param source The source
|
|
|
|
|
* @param length The length
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_set_strn(FuriString* string, const char source[], size_t length);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the character at the given index.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param index The index
|
|
|
|
|
* @param c The character
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_set_char(FuriString* string, size_t index, const char c);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the string to the n first characters of other one.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param source The source
|
|
|
|
|
* @param offset The offset
|
|
|
|
|
* @param length The length
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_set_n(FuriString* string, const FuriString* source, size_t offset, size_t length);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Format in the string the given printf format
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param format The format
|
|
|
|
|
* @param[in] ... The args
|
|
|
|
|
*
|
|
|
|
|
* @return number of characters printed or negative value on error
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
2022-10-07 16:35:15 +03:00
|
|
|
|
int furi_string_printf(FuriString* string, const char format[], ...)
|
|
|
|
|
_ATTRIBUTE((__format__(__printf__, 2, 3)));
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Format in the string the given printf format
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param format The format
|
|
|
|
|
* @param args The arguments
|
|
|
|
|
*
|
|
|
|
|
* @return number of characters printed or negative value on error
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_vprintf(FuriString* string, const char format[], va_list args);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Appending
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append a character to the string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param c The character
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_push_back(FuriString* string, char c);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append a string to the string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Concatenate the string with the other string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param string_2 The string 2
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_cat(FuriString* string_1, const FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append a C string to the string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Concatenate the string with the C string.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param cstring_2 The cstring 2
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_cat_str(FuriString* string_1, const char cstring_2[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append to the string the formatted string of the given printf format.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param format The format
|
|
|
|
|
* @param[in] ... The args
|
|
|
|
|
*
|
|
|
|
|
* @return number of characters printed or negative value on error
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
2022-10-07 16:35:15 +03:00
|
|
|
|
int furi_string_cat_printf(FuriString* string, const char format[], ...)
|
|
|
|
|
_ATTRIBUTE((__format__(__printf__, 2, 3)));
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append to the string the formatted string of the given printf format.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param format The format
|
|
|
|
|
* @param args The arguments
|
|
|
|
|
*
|
|
|
|
|
* @return number of characters printed or negative value on error
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_cat_vprintf(FuriString* string, const char format[], va_list args);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Comparators
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare two strings and return the sort order.
|
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param string_2 The string 2
|
|
|
|
|
*
|
|
|
|
|
* @return zero if equal
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_cmp(const FuriString* string_1, const FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare string with C string and return the sort order.
|
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param cstring_2 The cstring 2
|
|
|
|
|
*
|
|
|
|
|
* @return zero if equal
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_cmp_str(const FuriString* string_1, const char cstring_2[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare two strings (case insensitive according to the current locale) and
|
|
|
|
|
* return the sort order.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Note: doesn't work with UTF-8 strings.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param string_2 The string 2
|
|
|
|
|
*
|
|
|
|
|
* @return zero if equal
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_cmpi(const FuriString* string_1, const FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare string with C string (case insensitive according to the current
|
|
|
|
|
* locale) and return the sort order.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Note: doesn't work with UTF-8 strings.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param cstring_2 The cstring 2
|
|
|
|
|
*
|
|
|
|
|
* @return zero if equal
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
int furi_string_cmpi_str(const FuriString* string_1, const char cstring_2[]);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Search
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search the first occurrence of the needle in the string from the position
|
|
|
|
|
* start.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return position or FURI_STRING_FAILURE if not found
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_search(const FuriString* string, const FuriString* needle, size_t start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search the first occurrence of the needle in the string from the position
|
|
|
|
|
* start.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return position or FURI_STRING_FAILURE if not found
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_search_str(const FuriString* string, const char needle[], size_t start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search for the position of the character c from the position start (include)
|
|
|
|
|
* in the string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param c The character
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return position or FURI_STRING_FAILURE if not found
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_search_char(const FuriString* string, char c, size_t start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Reverse search for the position of the character c from the position start
|
|
|
|
|
* (include) in the string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param c The character
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return position or FURI_STRING_FAILURE if not found
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_search_rchar(const FuriString* string, char c, size_t start);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Equality
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if two strings are equal.
|
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param string_2 The string 2
|
|
|
|
|
*
|
|
|
|
|
* @return true if equal false otherwise
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_equal(const FuriString* string_1, const FuriString* string_2);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string is equal to the C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string_1 The string 1
|
|
|
|
|
* @param cstring_2 The cstring 2
|
|
|
|
|
*
|
|
|
|
|
* @return true if equal false otherwise
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_equal_str(const FuriString* string_1, const char cstring_2[]);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Replace
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace in the string the sub-string at position 'pos' for 'len' bytes into
|
|
|
|
|
* the C string 'replace'.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param pos The position
|
|
|
|
|
* @param len The length
|
|
|
|
|
* @param replace The replace
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_replace_at(FuriString* string, size_t pos, size_t len, const char replace[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace a string 'needle' to string 'replace' in a string from 'start'
|
|
|
|
|
* position.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param replace The replace
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return Return FURI_STRING_FAILURE if 'needle' not found or replace position.
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t
|
|
|
|
|
furi_string_replace(FuriString* string, FuriString* needle, FuriString* replace, size_t start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace a C string 'needle' to C string 'replace' in a string from 'start'
|
|
|
|
|
* position.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param replace The replace
|
|
|
|
|
* @param start The start (By default, start is zero)
|
|
|
|
|
*
|
|
|
|
|
* @return Return FURI_STRING_FAILURE if 'needle' not found or replace position.
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_replace_str(
|
|
|
|
|
FuriString* string,
|
|
|
|
|
const char needle[],
|
|
|
|
|
const char replace[],
|
|
|
|
|
size_t start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace all occurrences of 'needle' string into 'replace' string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param replace The replace
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_replace_all(
|
|
|
|
|
FuriString* string,
|
|
|
|
|
const FuriString* needle,
|
|
|
|
|
const FuriString* replace);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace all occurrences of 'needle' C string into 'replace' C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param needle The needle
|
|
|
|
|
* @param replace The replace
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_replace_all_str(FuriString* string, const char needle[], const char replace[]);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Start / End tests
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string starts with the given string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param start The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return true if string starts with
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_start_with(const FuriString* string, const FuriString* start);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string starts with the given C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param start The start
|
|
|
|
|
*
|
|
|
|
|
* @return true if string starts with
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_start_with_str(const FuriString* string, const char start[]);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string ends with the given string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param end The end
|
|
|
|
|
*
|
|
|
|
|
* @return true if string ends with
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_end_with(const FuriString* string, const FuriString* end);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string ends with the given C string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param end The end
|
|
|
|
|
*
|
|
|
|
|
* @return true if string ends with
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
bool furi_string_end_with_str(const FuriString* string, const char end[]);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Trim
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Trim the string left to the first 'index' bytes.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param index The index
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_left(FuriString* string, size_t index);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Trim the string right from the 'index' position to the last position.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param index The index
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_right(FuriString* string, size_t index);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Trim the string from position index to size bytes.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* See also furi_string_set_n.
|
2024-07-29 16:06:08 +03:00
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param index The index
|
|
|
|
|
* @param size The size
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_mid(FuriString* string, size_t index, size_t size);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Trim a string from the given set of characters (default is " \n\r\t").
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
* @param chars The characters
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_trim(FuriString* string, const char chars[]);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// UTF8
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** An unicode value */
|
2022-10-05 18:15:23 +03:00
|
|
|
|
typedef unsigned int FuriStringUnicodeValue;
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compute the length in UTF8 characters in the string.
|
|
|
|
|
*
|
|
|
|
|
* @param string The FuriString instance
|
|
|
|
|
*
|
|
|
|
|
* @return strings size
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
size_t furi_string_utf8_length(FuriString* string);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Push unicode into string, encoding it in UTF8.
|
|
|
|
|
*
|
|
|
|
|
* @param string The string
|
|
|
|
|
* @param unicode The unicode
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_utf8_push(FuriString* string, FuriStringUnicodeValue unicode);
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** State of the UTF8 decoding machine state */
|
2022-10-05 18:15:23 +03:00
|
|
|
|
typedef enum {
|
|
|
|
|
FuriStringUTF8StateStarting,
|
|
|
|
|
FuriStringUTF8StateDecoding1,
|
|
|
|
|
FuriStringUTF8StateDecoding2,
|
|
|
|
|
FuriStringUTF8StateDecoding3,
|
|
|
|
|
FuriStringUTF8StateError
|
|
|
|
|
} FuriStringUTF8State;
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Main generic UTF8 decoder
|
|
|
|
|
*
|
|
|
|
|
* It takes a character, and the previous state and the previous value of the
|
|
|
|
|
* unicode value. It updates the state and the decoded unicode value. A decoded
|
|
|
|
|
* unicode encoded value is valid only when the state is
|
|
|
|
|
* FuriStringUTF8StateStarting.
|
|
|
|
|
*
|
|
|
|
|
* @param c The character
|
|
|
|
|
* @param state The state
|
|
|
|
|
* @param unicode The unicode
|
2022-10-05 18:15:23 +03:00
|
|
|
|
*/
|
|
|
|
|
void furi_string_utf8_decode(char c, FuriStringUTF8State* state, FuriStringUnicodeValue* unicode);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
// Lasciate ogne speranza, voi ch’entrate
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Select either the string function or the str function depending on
|
|
|
|
|
* the b operand to the function.
|
|
|
|
|
* func1 is the string function / func2 is the str function.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Select for 1 argument */
|
2024-02-12 05:04:12 +03:00
|
|
|
|
#define FURI_STRING_SELECT1(func1, func2, a) \
|
|
|
|
|
_Generic((a), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
|
|
|
|
|
a)
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Select for 2 arguments */
|
2024-02-12 05:04:12 +03:00
|
|
|
|
#define FURI_STRING_SELECT2(func1, func2, a, b) \
|
|
|
|
|
_Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
|
|
|
|
|
a, b)
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Select for 3 arguments */
|
2024-02-12 05:04:12 +03:00
|
|
|
|
#define FURI_STRING_SELECT3(func1, func2, a, b, c) \
|
|
|
|
|
_Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
|
|
|
|
|
a, b, c)
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Select for 4 arguments */
|
2024-02-12 05:04:12 +03:00
|
|
|
|
#define FURI_STRING_SELECT4(func1, func2, a, b, c, d) \
|
|
|
|
|
_Generic((b), char*: func2, const char*: func2, FuriString*: func1, const FuriString*: func1)( \
|
|
|
|
|
a, b, c, d)
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Allocate new FuriString and set it content to string (or C string).
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* ([c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_alloc_set(a) \
|
|
|
|
|
FURI_STRING_SELECT1(furi_string_alloc_set, furi_string_alloc_set_str, a)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Set the string content to string (or C string).
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_set(a, b) FURI_STRING_SELECT2(furi_string_set, furi_string_set_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare string with string (or C string) and return the sort order.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Note: doesn't work with UTF-8 strings.
|
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_cmp(a, b) FURI_STRING_SELECT2(furi_string_cmp, furi_string_cmp_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Compare string with string (or C string) (case insensitive according to the current locale) and return the sort order.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* Note: doesn't work with UTF-8 strings.
|
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_cmpi(a, b) FURI_STRING_SELECT2(furi_string_cmpi, furi_string_cmpi_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string is equal to the string (or C string).
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_equal(a, b) FURI_STRING_SELECT2(furi_string_equal, furi_string_equal_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace all occurrences of string into string (or C string to another C string) in a string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_replace_all(a, b, c) \
|
|
|
|
|
FURI_STRING_SELECT3(furi_string_replace_all, furi_string_replace_all_str, a, b, c)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search for a string (or C string) in a string
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_search(...) \
|
|
|
|
|
M_APPLY( \
|
|
|
|
|
FURI_STRING_SELECT3, \
|
|
|
|
|
furi_string_search, \
|
|
|
|
|
furi_string_search_str, \
|
|
|
|
|
M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search for a C string in a string
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, cstring[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_search_str(...) furi_string_search_str(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string starts with the given string (or C string).
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_start_with(a, b) \
|
|
|
|
|
FURI_STRING_SELECT2(furi_string_start_with, furi_string_start_with_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Test if the string ends with the given string (or C string).
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_end_with(a, b) \
|
|
|
|
|
FURI_STRING_SELECT2(furi_string_end_with, furi_string_end_with_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Append a string (or C string) to the string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string)
|
|
|
|
|
*/
|
|
|
|
|
#define furi_string_cat(a, b) FURI_STRING_SELECT2(furi_string_cat, furi_string_cat_str, a, b)
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Trim a string from the given set of characters (default is " \n\r\t").
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string[, set=" \n\r\t"])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_trim(...) furi_string_trim(M_DEFAULT_ARGS(2, (" \n\r\t"), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Search for a character in a string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, character[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_search_char(...) furi_string_search_char(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Reverse Search for a character in a string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, character[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_search_rchar(...) furi_string_search_rchar(M_DEFAULT_ARGS(3, (0), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace a string to another string (or C string to another C string) in a string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, [c]string, [c]string[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_replace(...) \
|
|
|
|
|
M_APPLY( \
|
|
|
|
|
FURI_STRING_SELECT4, \
|
|
|
|
|
furi_string_replace, \
|
|
|
|
|
furi_string_replace_str, \
|
|
|
|
|
M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** Replace a C string to another C string in a string.
|
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
|
* (string, cstring, cstring[, start=0])
|
|
|
|
|
*/
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#define furi_string_replace_str(...) furi_string_replace_str(M_DEFAULT_ARGS(4, (0), __VA_ARGS__))
|
2022-10-05 18:15:23 +03:00
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** INIT OPLIST for FuriString */
|
2022-10-05 18:15:23 +03:00
|
|
|
|
#define F_STR_INIT(a) ((a) = furi_string_alloc())
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** INIT SET OPLIST for FuriString */
|
2022-10-05 18:15:23 +03:00
|
|
|
|
#define F_STR_INIT_SET(a, b) ((a) = furi_string_alloc_set(b))
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** INIT MOVE OPLIST for FuriString */
|
2022-10-20 13:58:11 +03:00
|
|
|
|
#define F_STR_INIT_MOVE(a, b) ((a) = furi_string_alloc_move(b))
|
|
|
|
|
|
2024-07-29 16:06:08 +03:00
|
|
|
|
/** OPLIST for FuriString */
|
2022-10-20 13:58:11 +03:00
|
|
|
|
#define FURI_STRING_OPLIST \
|
|
|
|
|
(INIT(F_STR_INIT), \
|
|
|
|
|
INIT_SET(F_STR_INIT_SET), \
|
|
|
|
|
SET(furi_string_set), \
|
|
|
|
|
INIT_MOVE(F_STR_INIT_MOVE), \
|
|
|
|
|
MOVE(furi_string_move), \
|
|
|
|
|
SWAP(furi_string_swap), \
|
|
|
|
|
RESET(furi_string_reset), \
|
|
|
|
|
EMPTY_P(furi_string_empty), \
|
|
|
|
|
CLEAR(furi_string_free), \
|
|
|
|
|
HASH(furi_string_hash), \
|
|
|
|
|
EQUAL(furi_string_equal), \
|
|
|
|
|
CMP(furi_string_cmp), \
|
2022-10-05 18:15:23 +03:00
|
|
|
|
TYPE(FuriString*))
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2023-06-27 13:50:09 +03:00
|
|
|
|
#endif
|