2 * Copyright (c) 2007 Mans Rullgard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVUTIL_AVSTRING_H
22 #define AVUTIL_AVSTRING_H
26 #include "attributes.h"
30 * @addtogroup lavu_string
35 * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
36 * the address of the first character in str after the prefix.
38 * @param str input string
39 * @param pfx prefix to test
40 * @param ptr updated if the prefix is matched inside str
41 * @return non-zero if the prefix matches, zero otherwise
43 int av_strstart(const char *str, const char *pfx, const char **ptr);
46 * Return non-zero if pfx is a prefix of str independent of case. If
47 * it is, *ptr is set to the address of the first character in str
50 * @param str input string
51 * @param pfx prefix to test
52 * @param ptr updated if the prefix is matched inside str
53 * @return non-zero if the prefix matches, zero otherwise
55 int av_stristart(const char *str, const char *pfx, const char **ptr);
58 * Locate the first case-independent occurrence in the string haystack
59 * of the string needle. A zero-length string needle is considered to
60 * match at the start of haystack.
62 * This function is a case-insensitive version of the standard strstr().
64 * @param haystack string to search in
65 * @param needle string to search for
66 * @return pointer to the located match within haystack
67 * or a null pointer if no match
69 char *av_stristr(const char *haystack, const char *needle);
72 * Locate the first occurrence of the string needle in the string haystack
73 * where not more than hay_length characters are searched. A zero-length
74 * string needle is considered to match at the start of haystack.
76 * This function is a length-limited version of the standard strstr().
78 * @param haystack string to search in
79 * @param needle string to search for
80 * @param hay_length length of string to search in
81 * @return pointer to the located match within haystack
82 * or a null pointer if no match
84 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length);
87 * Copy the string src to dst, but no more than size - 1 bytes, and
90 * This function is the same as BSD strlcpy().
92 * @param dst destination buffer
93 * @param src source string
94 * @param size size of destination buffer
95 * @return the length of src
97 * @warning since the return value is the length of src, src absolutely
98 * _must_ be a properly 0-terminated string, otherwise this will read beyond
99 * the end of the buffer and possibly crash.
101 size_t av_strlcpy(char *dst, const char *src, size_t size);
104 * Append the string src to the string dst, but to a total length of
105 * no more than size - 1 bytes, and null-terminate dst.
107 * This function is similar to BSD strlcat(), but differs when
108 * size <= strlen(dst).
110 * @param dst destination buffer
111 * @param src source string
112 * @param size size of destination buffer
113 * @return the total length of src and dst
115 * @warning since the return value use the length of src and dst, these
116 * absolutely _must_ be a properly 0-terminated strings, otherwise this
117 * will read beyond the end of the buffer and possibly crash.
119 size_t av_strlcat(char *dst, const char *src, size_t size);
122 * Append output to a string, according to a format. Never write out of
123 * the destination buffer, and always put a terminating 0 within
125 * @param dst destination buffer (string to which the output is
127 * @param size total size of the destination buffer
128 * @param fmt printf-compatible format string, specifying how the
129 * following parameters are used
130 * @return the length of the string that would have been generated
131 * if enough space had been available
133 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
136 * Get the count of continuous non zero chars starting from the beginning.
138 * @param len maximum number of characters to check in the string, that
139 * is the maximum value which is returned by the function
141 static inline size_t av_strnlen(const char *s, size_t len)
144 for (i = 0; i < len && s[i]; i++)
150 * Print arguments following specified format into a large enough auto
151 * allocated buffer. It is similar to GNU asprintf().
152 * @param fmt printf-compatible format string, specifying how the
153 * following parameters are used.
154 * @return the allocated string
155 * @note You have to free the string yourself with av_free().
157 char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
161 * Convert a number to an av_malloced string.
162 * @deprecated use av_asprintf() with "%f" or a more specific format
165 char *av_d2str(double d);
169 * Unescape the given string until a non escaped terminating char,
170 * and return the token corresponding to the unescaped string.
172 * The normal \ and ' escaping is supported. Leading and trailing
173 * whitespaces are removed, unless they are escaped with '\' or are
174 * enclosed between ''.
176 * @param buf the buffer to parse, buf will be updated to point to the
178 * @param term a 0-terminated list of terminating chars
179 * @return the malloced unescaped string, which must be av_freed by
180 * the user, NULL in case of allocation failure
182 char *av_get_token(const char **buf, const char *term);
185 * Split the string into several tokens which can be accessed by
186 * successive calls to av_strtok().
188 * A token is defined as a sequence of characters not belonging to the
189 * set specified in delim.
191 * On the first call to av_strtok(), s should point to the string to
192 * parse, and the value of saveptr is ignored. In subsequent calls, s
193 * should be NULL, and saveptr should be unchanged since the previous
196 * This function is similar to strtok_r() defined in POSIX.1.
198 * @param s the string to parse, may be NULL
199 * @param delim 0-terminated list of token delimiters, must be non-NULL
200 * @param saveptr user-provided pointer which points to stored
201 * information necessary for av_strtok() to continue scanning the same
202 * string. saveptr is updated to point to the next character after the
203 * first delimiter found, or to NULL if the string was terminated
204 * @return the found token, or NULL when no token is found
206 char *av_strtok(char *s, const char *delim, char **saveptr);
209 * Locale-independent conversion of ASCII isdigit.
211 static inline av_const int av_isdigit(int c)
213 return c >= '0' && c <= '9';
217 * Locale-independent conversion of ASCII isgraph.
219 static inline av_const int av_isgraph(int c)
221 return c > 32 && c < 127;
225 * Locale-independent conversion of ASCII isspace.
227 static inline av_const int av_isspace(int c)
229 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
234 * Locale-independent conversion of ASCII characters to uppercase.
236 static inline av_const int av_toupper(int c)
238 if (c >= 'a' && c <= 'z')
244 * Locale-independent conversion of ASCII characters to lowercase.
246 static inline av_const int av_tolower(int c)
248 if (c >= 'A' && c <= 'Z')
254 * Locale-independent conversion of ASCII isxdigit.
256 static inline av_const int av_isxdigit(int c)
259 return av_isdigit(c) || (c >= 'a' && c <= 'f');
263 * Locale-independent case-insensitive compare.
264 * @note This means only ASCII-range characters are case-insensitive
266 int av_strcasecmp(const char *a, const char *b);
269 * Locale-independent case-insensitive compare.
270 * @note This means only ASCII-range characters are case-insensitive
272 int av_strncasecmp(const char *a, const char *b, size_t n);
275 * Locale-independent strings replace.
276 * @note This means only ASCII-range characters are replace
278 char *av_strireplace(const char *str, const char *from, const char *to);
281 * Thread safe basename.
282 * @param path the string to parse, on DOS both \ and / are considered separators.
283 * @return pointer to the basename substring.
284 * If path does not contain a slash, the function returns a copy of path.
285 * If path is a NULL pointer or points to an empty string, a pointer
286 * to a string "." is returned.
288 const char *av_basename(const char *path);
291 * Thread safe dirname.
292 * @param path the string to parse, on DOS both \ and / are considered separators.
293 * @return A pointer to a string that's the parent directory of path.
294 * If path is a NULL pointer or points to an empty string, a pointer
295 * to a string "." is returned.
296 * @note the function may modify the contents of the path, so copies should be passed.
298 const char *av_dirname(char *path);
301 * Match instances of a name in a comma-separated list of names.
302 * List entries are checked from the start to the end of the names list,
303 * the first match ends further processing. If an entry prefixed with '-'
304 * matches, then 0 is returned. The "ALL" list entry is considered to
307 * @param name Name to look for.
308 * @param names List of names.
309 * @return 1 on match, 0 otherwise.
311 int av_match_name(const char *name, const char *names);
314 * Append path component to the existing path.
315 * Path separator '/' is placed between when needed.
316 * Resulting string have to be freed with av_free().
317 * @param path base path
318 * @param component component to be appended
319 * @return new path or NULL on error.
321 char *av_append_path_component(const char *path, const char *component);
324 AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.
325 AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
326 AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping.
330 * Consider spaces special and escape them even in the middle of the
333 * This is equivalent to adding the whitespace characters to the special
334 * characters lists, except it is guaranteed to use the exact same list
335 * of whitespace characters as the rest of libavutil.
337 #define AV_ESCAPE_FLAG_WHITESPACE (1 << 0)
340 * Escape only specified special characters.
341 * Without this flag, escape also any characters that may be considered
342 * special by av_get_token(), such as the single quote.
344 #define AV_ESCAPE_FLAG_STRICT (1 << 1)
347 * Escape string in src, and put the escaped string in an allocated
348 * string in *dst, which must be freed with av_free().
350 * @param dst pointer where an allocated string is put
351 * @param src string to escape, must be non-NULL
352 * @param special_chars string containing the special characters which
353 * need to be escaped, can be NULL
354 * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
355 * Any unknown value for mode will be considered equivalent to
356 * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
358 * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros
359 * @return the length of the allocated string, or a negative error code in case of error
360 * @see av_bprint_escape()
362 av_warn_unused_result
363 int av_escape(char **dst, const char *src, const char *special_chars,
364 enum AVEscapeMode mode, int flags);
366 #define AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES 1 ///< accept codepoints over 0x10FFFF
367 #define AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS 2 ///< accept non-characters - 0xFFFE and 0xFFFF
368 #define AV_UTF8_FLAG_ACCEPT_SURROGATES 4 ///< accept UTF-16 surrogates codes
369 #define AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES 8 ///< exclude control codes not accepted by XML
371 #define AV_UTF8_FLAG_ACCEPT_ALL \
372 AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES|AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS|AV_UTF8_FLAG_ACCEPT_SURROGATES
375 * Read and decode a single UTF-8 code point (character) from the
376 * buffer in *buf, and update *buf to point to the next byte to
379 * In case of an invalid byte sequence, the pointer will be updated to
380 * the next byte after the invalid sequence and the function will
381 * return an error code.
383 * Depending on the specified flags, the function will also fail in
384 * case the decoded code point does not belong to a valid range.
386 * @note For speed-relevant code a carefully implemented use of
387 * GET_UTF8() may be preferred.
389 * @param codep pointer used to return the parsed code in case of success.
390 * The value in *codep is set even in case the range check fails.
391 * @param bufp pointer to the address the first byte of the sequence
392 * to decode, updated by the function to point to the
393 * byte next after the decoded sequence
394 * @param buf_end pointer to the end of the buffer, points to the next
395 * byte past the last in the buffer. This is used to
396 * avoid buffer overreads (in case of an unfinished
397 * UTF-8 sequence towards the end of the buffer).
398 * @param flags a collection of AV_UTF8_FLAG_* flags
399 * @return >= 0 in case a sequence was successfully read, a negative
400 * value in case of invalid sequence
402 av_warn_unused_result
403 int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
407 * Check if a name is in a list.
408 * @returns 0 if not found, or the 1 based index where it has been found in the
411 int av_match_list(const char *name, const char *list, char separator);
414 * See libc sscanf manual for more information.
415 * Locale-independent sscanf implementation.
417 int av_sscanf(const char *string, const char *format, ...);
423 #endif /* AVUTIL_AVSTRING_H */