7 #include "eina_types.h"
10 * @page tutorial_eina_string Eina String example
11 * @dontinclude eina_str_01.c
13 * Whenever using eina we need to include it:
17 * In our main function we declare(and initialize) some variables and initialize
21 * It's frequently necessary to split a string into its constituent parts,
22 * eina_str_split() make's it easy to do so:
25 * Another common need is to make a string uppercase or lowercase, so let's
26 * create a string and make it uppercase and then make it lowercase again:
30 * Next we use eina to check if our @p names string starts or ends with some
34 * When strings will be used in a terminal(or a number of other places) it
35 * necessary to escape certain characters that appear in them:
38 * Much as we previously split a string we will now join two strings:
41 * With strlcpy() we can copy what portion of the @p prologue fits in @p str and
42 * be sure that it's still NULL terminated:
45 * Since we are done with @p prologue and @p str we should free them:
48 * Finally we see strlcat in action:
51 * And then shut eina down and exit:
53 * @example eina_str_01.c
56 * @addtogroup Eina_String_Group String
58 * @brief Provide useful functions for C string manipulation.
60 * This group of functions allow you to more easily manipulate strings, they
61 * provide functionality not available through string.h.
63 * @warning Since these functions modify the strings they can't be used with
64 * shared strings(eina_stringshare).
66 * See an example @ref tutorial_eina_string "here".
70 * @addtogroup Eina_Tools_Group Tools
72 * For more information refer to the @ref tutorial_eina_string "string example".
78 * @defgroup Eina_String_Group String
83 /* strlcpy implementation for libc's lacking it */
86 * @brief Copy a c-string to another.
88 * @param dst The destination string.
89 * @param src The source string.
90 * @param siz The size of the destination string.
91 * @return The length of the source string.
93 * This function copies up to @p siz - 1 characters from the
94 * NULL-terminated string @p src to @p dst, NULL-terminating the result
95 * (unless @p siz is equal to 0). The returned value is the length of
96 * @p src. If the returned value is greater than @p siz, truncation
99 * @note The main difference between eina_strlcpy and strncpy is that this
100 * ensures @p dst is NULL-terminated even if no @c NULL byte is found in the first
101 * @p siz bytes of src.
103 EAPI size_t eina_strlcpy(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1, 2);
106 * @brief Append a c-string.
108 * @param dst The destination string.
109 * @param src The source string.
110 * @param siz The size of the destination string.
111 * @return The length of the source string plus MIN(siz, strlen(initial dst))
113 * This function appends @p src to @p dst of size @p siz (unlike
114 * strncat, @p siz is the full size of @p dst, not space left). At
115 * most @p siz - 1 characters will be copied. Always NULL-terminates
116 * (unless @p siz <= strlen(dst)). This function returns strlen(src) +
117 * MIN(siz, strlen(initial dst)). If the returned value is greater or
118 * equal than @p siz, truncation occurred.
120 EAPI size_t eina_strlcat(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1, 2);
124 * @brief Check if the given string has the given prefix.
126 * @param str The string to work with.
127 * @param prefix The prefix to check for.
128 * @return #EINA_TRUE if the string has the given prefix, #EINA_FALSE otherwise.
130 * This function returns #EINA_TRUE if @p str has the prefix
131 * @p prefix, #EINA_FALSE otherwise. If the length of @p prefix is
132 * greater than @p str, #EINA_FALSE is returned.
134 EAPI Eina_Bool eina_str_has_prefix(const char *str, const char *prefix) EINA_PURE EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
137 * @brief Check if the given string has the given suffix.
139 * @param str The string to work with.
140 * @param suffix The suffix to check for.
141 * @return #EINA_TRUE if the string has the given suffix, #EINA_FALSE otherwise.
143 * This function returns #EINA_TRUE if @p str has the suffix
144 * @p suffix, #EINA_FALSE otherwise. If the length of @p suffix is
145 * greater than @p str, #EINA_FALSE is returned.
147 EAPI Eina_Bool eina_str_has_suffix(const char *str, const char *suffix) EINA_PURE EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
150 * @brief Check if the given string has the given extension.
152 * @param str The string to work with.
153 * @param ext The extension to check for.
154 * @return #EINA_TRUE if the string has the given extension, #EINA_FALSE otherwise.
156 * This function does the same as eina_str_has_suffix(), except it's case
159 EAPI Eina_Bool eina_str_has_extension(const char *str, const char *ext) EINA_PURE EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
162 * @brief Split a string using a delimiter.
164 * @param string The string to split.
165 * @param delimiter The string which specifies the places at which to split the string.
166 * @param max_tokens The maximum number of strings to split string into, or a number less
167 * than 1 to split as many times as possible. This parameter
168 * IGNORES the added @c NULL terminator.
169 * @return A newly-allocated NULL-terminated array of strings or @c NULL if it
170 * fails to allocate the array.
172 * This function splits @p string into a maximum of @p max_tokens pieces,
173 * using the given delimiter @p delimiter. @p delimiter is not included in any
174 * of the resulting strings, unless @p max_tokens is reached. If
175 * @p max_tokens is less than @c 1, the string is splitted as many times as possible. If
176 * @p max_tokens is reached, the last string in the returned string
177 * array contains the remainder of string. The returned value is a
178 * newly allocated NULL-terminated array of strings or @c NULL if it fails to
179 * allocate the array. To free it, free the first element of the array and the
182 * @note If you need the number of elements in the returned array see
183 * eina_str_split_full().
185 EAPI char **eina_str_split(const char *string, const char *delimiter, int max_tokens) EINA_ARG_NONNULL(1, 2) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
188 * @brief Split a string using a delimiter and returns number of elements.
190 * @param string The string to split.
191 * @param delimiter The string which specifies the places at which to split the string.
192 * @param max_tokens The maximum number of strings to split string into, or a number less
193 * than 1 to split as many times as possible. This parameter
194 * IGNORES the added @c NULL terminator.
195 * @param elements Where to return the number of elements in returned
196 * array. This array is guaranteed to be no greater than @p max_tokens, and
197 * it will NOT count the @c NULL terminator element.
198 * @return A newly-allocated NULL-terminated array of strings or @c NULL if it
199 * fails to allocate the array.
201 * This function splits @p string into a maximum of @p max_tokens pieces,
202 * using the given delimiter @p delimiter. @p delimiter is not included in any
203 * of the resulting strings, unless @p max_tokens is reached. If
204 * @p max_tokens is less than @c 1, the string is splitted as many times as possible. If
205 * @p max_tokens is reached, the last string in the returned string
206 * array contains the remainder of string. The returned value is a
207 * newly allocated NULL-terminated array of strings or @c NULL if it fails to
208 * allocate the array. To free it, free the first element of the array and the
211 * @note The actual size of the returned array, when @p elements returns greater than zero,
212 * will always be @p elements + 1. This is due to the @c NULL terminator element that
213 * is added to the array for safety. If it returns @c 6, the number of split strings returned
214 * will be 6, but the size of the array (including the @c NULL element) will actually be 7.
216 * @see eina_str_split()
218 EAPI char **eina_str_split_full(const char *string, const char *delimiter, int max_tokens, unsigned int *elements) EINA_ARG_NONNULL(1, 2, 4) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
222 * @brief Join two strings of known length.
224 * @param dst The buffer to store the result.
225 * @param size Size (in byte) of the buffer.
226 * @param sep The separator character to use.
227 * @param a First string to use, before @p sep.
228 * @param a_len length of @p a.
229 * @param b Second string to use, after @p sep.
230 * @param b_len length of @p b.
231 * @return The number of characters printed.
233 * This function joins the strings @p a and @p b (in that order) and
234 * separate them with @p sep. The result is stored in the buffer
235 * @p dst and at most @p size - 1 characters will be written and the
236 * string is NULL-terminated. @p a_len is the length of @p a (not
237 * including '\\0') and @p b_len is the length of @p b (not including
238 * '\\0'). This function returns the number of characters printed (not
239 * including the trailing '\\0' used to end output to strings). Just
240 * like snprintf(), it will not write more than @p size bytes, thus a
241 * returned value of @p size or more means that the output was
244 * @see eina_str_join()
245 * @see eina_str_join_static()
247 EAPI size_t eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len) EINA_ARG_NONNULL(1, 4, 6);
251 * @brief Use Iconv to convert a text string from one encoding to another.
253 * @param enc_from Encoding to convert from.
254 * @param enc_to Encoding to convert to.
255 * @param text The text to convert.
256 * @return The converted text.
258 * This function converts @p text, encoded in @p enc_from. On success,
259 * the converted text is returned and is encoded in @p enc_to. On
260 * failure, @c NULL is returned. Iconv is used to convert @p text. If
261 * Iconv is not available, @c NULL is returned. When not used anymore,
262 * the returned value must be freed.
264 EAPI char *eina_str_convert(const char *enc_from, const char *enc_to, const char *text) EINA_WARN_UNUSED_RESULT EINA_MALLOC EINA_ARG_NONNULL(1, 2, 3);
268 * @brief Escape slashes, spaces and apostrophes in strings.
270 * @param str The string to escape.
271 * @return The escaped string.
273 * Escaping is done by adding a slash "\" before any occurrence of slashes "\",
274 * spaces " " or apostrophes "'". This function returns a newly allocated
275 * escaped string on success, @c NULL on failure. When not used anymore, the
276 * returned value must be freed.
278 EAPI char *eina_str_escape(const char *str) EINA_WARN_UNUSED_RESULT EINA_MALLOC EINA_ARG_NONNULL(1);
282 * @brief Lowercase all the characters in range [A-Z] in the given string.
284 * @param str The string to lowercase.
286 * This function modifies the original string, changing all characters
287 * in [A-Z] to lowercase. If @p str is @c NULL or is an empty string,
288 * this function does nothing.
290 EAPI void eina_str_tolower(char **str);
293 * @brief Uppercase all the characters in range [a-z] in the given string.
295 * @param str The string to uppercase.
297 * This function modifies the original string, changing all characters
298 * in [a-z] to uppercase. If @p str is @c NULL or is an empty string,
299 * this function does nothing.
301 EAPI void eina_str_toupper(char **str);
303 static inline size_t eina_str_join(char *dst, size_t size, char sep, const char *a, const char *b) EINA_ARG_NONNULL(1, 4, 5);
306 * @def eina_str_join_static(dst, sep, a, b)
307 * @brief Join two static strings and store the result in a static buffer.
309 * @param dst The buffer to store the result.
310 * @param sep The separator character to use.
311 * @param a First string to use, before @p sep.
312 * @param b Second string to use, after @p sep.
313 * @return The number of characters printed.
315 * This function is similar to eina_str_join_len(), but will assume
316 * string sizes are know using sizeof(X).
318 * @see eina_str_join()
319 * @see eina_str_join_static()
321 #define eina_str_join_static(dst, sep, a, b) eina_str_join_len(dst, sizeof(dst), sep, a, (sizeof(a) > 0) ? sizeof(a) - 1 : 0, b, (sizeof(b) > 0) ? sizeof(b) - 1 : 0)
323 static inline size_t eina_strlen_bounded(const char *str, size_t maxlen) EINA_PURE EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
325 #include "eina_inline_str.x"
335 #endif /* EINA_STR_H */