1 /**
2  * @file lv_string.h
3  *
4  */
5 
6 #ifndef LV_STRING_H
7 #define LV_STRING_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 #include "../misc/lv_types.h"
18 
19 /*********************
20  *      DEFINES
21  *********************/
22 
23 /**********************
24  *      TYPEDEFS
25  **********************/
26 
27 /**********************
28  * GLOBAL PROTOTYPES
29  **********************/
30 
31 /**
32  * @brief Copies a block of memory from a source address to a destination address.
33  * @param dst Pointer to the destination array where the content is to be copied.
34  * @param src Pointer to the source of data to be copied.
35  * @param len Number of bytes to copy.
36  * @return Pointer to the destination array.
37  * @note The function does not check for any overlapping of the source and destination memory blocks.
38  */
39 void * lv_memcpy(void * dst, const void * src, size_t len);
40 
41 /**
42  * @brief Fills a block of memory with a specified value.
43  * @param dst Pointer to the destination array to fill with the specified value.
44  * @param v Value to be set. The value is passed as an int, but the function fills
45  *          the block of memory using the unsigned char conversion of this value.
46  * @param len Number of bytes to be set to the value.
47  */
48 void lv_memset(void * dst, uint8_t v, size_t len);
49 
50 /**
51  * @brief Move a block of memory from source to destination
52  * @param dst Pointer to the destination array where the content is to be copied.
53  * @param src Pointer to the source of data to be copied.
54  * @param len Number of bytes to copy
55  * @return Pointer to the destination array.
56  */
57 void * lv_memmove(void * dst, const void * src, size_t len);
58 
59 /**
60  * @brief This function will compare two memory blocks
61  * @param p1 Pointer to the first memory block
62  * @param p2 Pointer to the second memory block
63  * @param len Number of bytes to compare
64  * @return The difference between the value of the first unmatching byte.
65  */
66 int lv_memcmp(const void * p1, const void * p2, size_t len);
67 
68 /**
69  * Same as `memset(dst, 0x00, len)`.
70  * @param dst pointer to the destination buffer
71  * @param len number of byte to set
72  */
lv_memzero(void * dst,size_t len)73 static inline void lv_memzero(void * dst, size_t len)
74 {
75     lv_memset(dst, 0x00, len);
76 }
77 
78 /**
79  * @brief Computes the length of the string str up to, but not including the terminating null character.
80  * @param str Pointer to the null-terminated byte string to be examined.
81  * @return The length of the string in bytes.
82  */
83 size_t lv_strlen(const char * str);
84 
85 /**
86  * @brief Copies up to dst_size-1 (non-null) characters from src to dst. A null terminator is always added.
87  * @param dst Pointer to the destination array where the content is to be copied.
88  * @param src Pointer to the source of data to be copied.
89  * @param dst_size Maximum number of characters to be copied to dst, including the null character.
90  * @return The length of src. The return value is equivalent to the value returned by lv_strlen(src)
91  */
92 size_t lv_strlcpy(char * dst, const char * src, size_t dst_size);
93 
94 /**
95  * @brief Copies up to dest_size characters from the string pointed to by src to the character array pointed to by dst
96  *        and fills the remaining length with null bytes.
97  * @param dst Pointer to the destination array where the content is to be copied.
98  * @param src Pointer to the source of data to be copied.
99  * @param dest_size Maximum number of characters to be copied to dst.
100  * @return A pointer to the destination array, which is dst.
101  * @note dst will not be null terminated if dest_size bytes were copied from src before the end of src was reached.
102  */
103 char * lv_strncpy(char * dst, const char * src, size_t dest_size);
104 
105 /**
106  * @brief Copies the string pointed to by src, including the terminating null character,
107  *        to the character array pointed to by dst.
108  * @param dst Pointer to the destination array where the content is to be copied.
109  * @param src Pointer to the source of data to be copied.
110  * @return A pointer to the destination array, which is dst.
111  */
112 char * lv_strcpy(char * dst, const char * src);
113 
114 /**
115  * @brief  This function will compare two strings without specified length.
116  * @param s1    pointer to the first string
117  * @param s2    pointer to the second string
118  * @return      the difference between the value of the first unmatching character.
119  */
120 int lv_strcmp(const char * s1, const char * s2);
121 
122 /**
123  * @brief  This function will compare two strings up to the given length.
124  * @param s1    pointer to the first string
125  * @param s2    pointer to the second string
126  * @param len   the maximum amount of characters to compare
127  * @return      the difference between the value of the first unmatching character.
128  */
129 int lv_strncmp(const char * s1, const char * s2, size_t len);
130 
131 /** Returns true if the two strings are equal.
132  * Just a wrapper around strcmp for convenience.
133  * @param s1    pointer to the first string
134  * @param s2    pointer to the second string
135  * @return      true: the strings are equal; false: otherwise
136  */
lv_streq(const char * s1,const char * s2)137 static inline bool lv_streq(const char * s1, const char * s2)
138 {
139     return lv_strcmp(s1, s2) == 0;
140 }
141 
142 /**
143  * @brief Duplicate a string by allocating a new one and copying the content.
144  * @param src Pointer to the source of data to be copied.
145  * @return A pointer to the new allocated string. NULL if failed.
146  */
147 char * lv_strdup(const char * src);
148 
149 /**
150  * @brief Copies the string pointed to by src, including the terminating null character,
151  *        to the end of the string pointed to by dst.
152  * @param dst Pointer to the destination string where the content is to be appended.
153  * @param src Pointer to the source of data to be copied.
154  * @return A pointer to the destination string, which is dst.
155  */
156 char * lv_strcat(char * dst, const char * src);
157 
158 /**
159  * @brief Copies up to src_len characters from the string pointed to by src
160  *        to the end of the string pointed to by dst.
161  *        A terminating null character is appended to dst even if no null character
162  *        was encountered in src after src_len characters were copied.
163  * @param dst Pointer to the destination string where the content is to be appended.
164  * @param src Pointer to the source of data to be copied.
165  * @param src_len Maximum number of characters from src to be copied to the end of dst.
166  * @return A pointer to the destination string, which is dst.
167  */
168 char * lv_strncat(char * dst, const char * src, size_t src_len);
169 
170 /**
171  * @brief Searches for the first occurrence of character c in the string str.
172  * @param str Pointer to the null-terminated byte string to be searched.
173  * @param c The character to be searched for.
174  * @return A pointer to the first occurrence of character c in the string str, or a null pointer if c is not found.
175  */
176 char * lv_strchr(const char * str, int c);
177 
178 /**********************
179  *      MACROS
180  **********************/
181 
182 #ifdef __cplusplus
183 } /*extern "C"*/
184 #endif
185 
186 #endif /*LV_STRING_H*/
187