FFmpeg
avstring.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Mans Rullgard
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #ifndef AVUTIL_AVSTRING_H
22 #define AVUTIL_AVSTRING_H
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 #include "attributes.h"
27 
28 /**
29  * @addtogroup lavu_string
30  * @{
31  */
32 
33 /**
34  * Return non-zero if pfx is a prefix of str. If it is, *ptr is set to
35  * the address of the first character in str after the prefix.
36  *
37  * @param str input string
38  * @param pfx prefix to test
39  * @param ptr updated if the prefix is matched inside str
40  * @return non-zero if the prefix matches, zero otherwise
41  */
42 int av_strstart(const char *str, const char *pfx, const char **ptr);
43 
44 /**
45  * Return non-zero if pfx is a prefix of str independent of case. If
46  * it is, *ptr is set to the address of the first character in str
47  * after the prefix.
48  *
49  * @param str input string
50  * @param pfx prefix to test
51  * @param ptr updated if the prefix is matched inside str
52  * @return non-zero if the prefix matches, zero otherwise
53  */
54 int av_stristart(const char *str, const char *pfx, const char **ptr);
55 
56 /**
57  * Locate the first case-independent occurrence in the string haystack
58  * of the string needle. A zero-length string needle is considered to
59  * match at the start of haystack.
60  *
61  * This function is a case-insensitive version of the standard strstr().
62  *
63  * @param haystack string to search in
64  * @param needle string to search for
65  * @return pointer to the located match within haystack
66  * or a null pointer if no match
67  */
68 char *av_stristr(const char *haystack, const char *needle);
69 
70 /**
71  * Locate the first occurrence of the string needle in the string haystack
72  * where not more than hay_length characters are searched. A zero-length
73  * string needle is considered to match at the start of haystack.
74  *
75  * This function is a length-limited version of the standard strstr().
76  *
77  * @param haystack string to search in
78  * @param needle string to search for
79  * @param hay_length length of string to search in
80  * @return pointer to the located match within haystack
81  * or a null pointer if no match
82  */
83 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length);
84 
85 /**
86  * Copy the string src to dst, but no more than size - 1 bytes, and
87  * null-terminate dst.
88  *
89  * This function is the same as BSD strlcpy().
90  *
91  * @param dst destination buffer
92  * @param src source string
93  * @param size size of destination buffer
94  * @return the length of src
95  *
96  * @warning since the return value is the length of src, src absolutely
97  * _must_ be a properly 0-terminated string, otherwise this will read beyond
98  * the end of the buffer and possibly crash.
99  */
100 size_t av_strlcpy(char *dst, const char *src, size_t size);
101 
102 /**
103  * Append the string src to the string dst, but to a total length of
104  * no more than size - 1 bytes, and null-terminate dst.
105  *
106  * This function is similar to BSD strlcat(), but differs when
107  * size <= strlen(dst).
108  *
109  * @param dst destination buffer
110  * @param src source string
111  * @param size size of destination buffer
112  * @return the total length of src and dst
113  *
114  * @warning since the return value use the length of src and dst, these
115  * absolutely _must_ be a properly 0-terminated strings, otherwise this
116  * will read beyond the end of the buffer and possibly crash.
117  */
118 size_t av_strlcat(char *dst, const char *src, size_t size);
119 
120 /**
121  * Append output to a string, according to a format. Never write out of
122  * the destination buffer, and always put a terminating 0 within
123  * the buffer.
124  * @param dst destination buffer (string to which the output is
125  * appended)
126  * @param size total size of the destination buffer
127  * @param fmt printf-compatible format string, specifying how the
128  * following parameters are used
129  * @return the length of the string that would have been generated
130  * if enough space had been available
131  */
132 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
133 
134 /**
135  * Get the count of continuous non zero chars starting from the beginning.
136  *
137  * @param len maximum number of characters to check in the string, that
138  * is the maximum value which is returned by the function
139  */
140 static inline size_t av_strnlen(const char *s, size_t len)
141 {
142  size_t i;
143  for (i = 0; i < len && s[i]; i++)
144  ;
145  return i;
146 }
147 
148 /**
149  * Print arguments following specified format into a large enough auto
150  * allocated buffer. It is similar to GNU asprintf().
151  * @param fmt printf-compatible format string, specifying how the
152  * following parameters are used.
153  * @return the allocated string
154  * @note You have to free the string yourself with av_free().
155  */
156 char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
157 
158 /**
159  * Convert a number to an av_malloced string.
160  */
161 char *av_d2str(double d);
162 
163 /**
164  * Unescape the given string until a non escaped terminating char,
165  * and return the token corresponding to the unescaped string.
166  *
167  * The normal \ and ' escaping is supported. Leading and trailing
168  * whitespaces are removed, unless they are escaped with '\' or are
169  * enclosed between ''.
170  *
171  * @param buf the buffer to parse, buf will be updated to point to the
172  * terminating char
173  * @param term a 0-terminated list of terminating chars
174  * @return the malloced unescaped string, which must be av_freed by
175  * the user, NULL in case of allocation failure
176  */
177 char *av_get_token(const char **buf, const char *term);
178 
179 /**
180  * Split the string into several tokens which can be accessed by
181  * successive calls to av_strtok().
182  *
183  * A token is defined as a sequence of characters not belonging to the
184  * set specified in delim.
185  *
186  * On the first call to av_strtok(), s should point to the string to
187  * parse, and the value of saveptr is ignored. In subsequent calls, s
188  * should be NULL, and saveptr should be unchanged since the previous
189  * call.
190  *
191  * This function is similar to strtok_r() defined in POSIX.1.
192  *
193  * @param s the string to parse, may be NULL
194  * @param delim 0-terminated list of token delimiters, must be non-NULL
195  * @param saveptr user-provided pointer which points to stored
196  * information necessary for av_strtok() to continue scanning the same
197  * string. saveptr is updated to point to the next character after the
198  * first delimiter found, or to NULL if the string was terminated
199  * @return the found token, or NULL when no token is found
200  */
201 char *av_strtok(char *s, const char *delim, char **saveptr);
202 
203 /**
204  * Locale-independent conversion of ASCII isdigit.
205  */
206 static inline av_const int av_isdigit(int c)
207 {
208  return c >= '0' && c <= '9';
209 }
210 
211 /**
212  * Locale-independent conversion of ASCII isgraph.
213  */
214 static inline av_const int av_isgraph(int c)
215 {
216  return c > 32 && c < 127;
217 }
218 
219 /**
220  * Locale-independent conversion of ASCII isspace.
221  */
222 static inline av_const int av_isspace(int c)
223 {
224  return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
225  c == '\v';
226 }
227 
228 /**
229  * Locale-independent conversion of ASCII characters to uppercase.
230  */
231 static inline av_const int av_toupper(int c)
232 {
233  if (c >= 'a' && c <= 'z')
234  c ^= 0x20;
235  return c;
236 }
237 
238 /**
239  * Locale-independent conversion of ASCII characters to lowercase.
240  */
241 static inline av_const int av_tolower(int c)
242 {
243  if (c >= 'A' && c <= 'Z')
244  c ^= 0x20;
245  return c;
246 }
247 
248 /**
249  * Locale-independent conversion of ASCII isxdigit.
250  */
251 static inline av_const int av_isxdigit(int c)
252 {
253  c = av_tolower(c);
254  return av_isdigit(c) || (c >= 'a' && c <= 'f');
255 }
256 
257 /**
258  * Locale-independent case-insensitive compare.
259  * @note This means only ASCII-range characters are case-insensitive
260  */
261 int av_strcasecmp(const char *a, const char *b);
262 
263 /**
264  * Locale-independent case-insensitive compare.
265  * @note This means only ASCII-range characters are case-insensitive
266  */
267 int av_strncasecmp(const char *a, const char *b, size_t n);
268 
269 /**
270  * Locale-independent strings replace.
271  * @note This means only ASCII-range characters are replace
272  */
273 char *av_strireplace(const char *str, const char *from, const char *to);
274 
275 /**
276  * Thread safe basename.
277  * @param path the string to parse, on DOS both \ and / are considered separators.
278  * @return pointer to the basename substring.
279  * If path does not contain a slash, the function returns a copy of path.
280  * If path is a NULL pointer or points to an empty string, a pointer
281  * to a string "." is returned.
282  */
283 const char *av_basename(const char *path);
284 
285 /**
286  * Thread safe dirname.
287  * @param path the string to parse, on DOS both \ and / are considered separators.
288  * @return A pointer to a string that's the parent directory of path.
289  * If path is a NULL pointer or points to an empty string, a pointer
290  * to a string "." is returned.
291  * @note the function may modify the contents of the path, so copies should be passed.
292  */
293 const char *av_dirname(char *path);
294 
295 /**
296  * Match instances of a name in a comma-separated list of names.
297  * List entries are checked from the start to the end of the names list,
298  * the first match ends further processing. If an entry prefixed with '-'
299  * matches, then 0 is returned. The "ALL" list entry is considered to
300  * match all names.
301  *
302  * @param name Name to look for.
303  * @param names List of names.
304  * @return 1 on match, 0 otherwise.
305  */
306 int av_match_name(const char *name, const char *names);
307 
308 /**
309  * Append path component to the existing path.
310  * Path separator '/' is placed between when needed.
311  * Resulting string have to be freed with av_free().
312  * @param path base path
313  * @param component component to be appended
314  * @return new path or NULL on error.
315  */
316 char *av_append_path_component(const char *path, const char *component);
317 
319  AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.
320  AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
321  AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping.
322 };
323 
324 /**
325  * Consider spaces special and escape them even in the middle of the
326  * string.
327  *
328  * This is equivalent to adding the whitespace characters to the special
329  * characters lists, except it is guaranteed to use the exact same list
330  * of whitespace characters as the rest of libavutil.
331  */
332 #define AV_ESCAPE_FLAG_WHITESPACE (1 << 0)
333 
334 /**
335  * Escape only specified special characters.
336  * Without this flag, escape also any characters that may be considered
337  * special by av_get_token(), such as the single quote.
338  */
339 #define AV_ESCAPE_FLAG_STRICT (1 << 1)
340 
341 /**
342  * Escape string in src, and put the escaped string in an allocated
343  * string in *dst, which must be freed with av_free().
344  *
345  * @param dst pointer where an allocated string is put
346  * @param src string to escape, must be non-NULL
347  * @param special_chars string containing the special characters which
348  * need to be escaped, can be NULL
349  * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
350  * Any unknown value for mode will be considered equivalent to
351  * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
352  * notice.
353  * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros
354  * @return the length of the allocated string, or a negative error code in case of error
355  * @see av_bprint_escape()
356  */
358 int av_escape(char **dst, const char *src, const char *special_chars,
359  enum AVEscapeMode mode, int flags);
360 
361 #define AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES 1 ///< accept codepoints over 0x10FFFF
362 #define AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS 2 ///< accept non-characters - 0xFFFE and 0xFFFF
363 #define AV_UTF8_FLAG_ACCEPT_SURROGATES 4 ///< accept UTF-16 surrogates codes
364 #define AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES 8 ///< exclude control codes not accepted by XML
365 
366 #define AV_UTF8_FLAG_ACCEPT_ALL \
367  AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES|AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS|AV_UTF8_FLAG_ACCEPT_SURROGATES
368 
369 /**
370  * Read and decode a single UTF-8 code point (character) from the
371  * buffer in *buf, and update *buf to point to the next byte to
372  * decode.
373  *
374  * In case of an invalid byte sequence, the pointer will be updated to
375  * the next byte after the invalid sequence and the function will
376  * return an error code.
377  *
378  * Depending on the specified flags, the function will also fail in
379  * case the decoded code point does not belong to a valid range.
380  *
381  * @note For speed-relevant code a carefully implemented use of
382  * GET_UTF8() may be preferred.
383  *
384  * @param codep pointer used to return the parsed code in case of success.
385  * The value in *codep is set even in case the range check fails.
386  * @param bufp pointer to the address the first byte of the sequence
387  * to decode, updated by the function to point to the
388  * byte next after the decoded sequence
389  * @param buf_end pointer to the end of the buffer, points to the next
390  * byte past the last in the buffer. This is used to
391  * avoid buffer overreads (in case of an unfinished
392  * UTF-8 sequence towards the end of the buffer).
393  * @param flags a collection of AV_UTF8_FLAG_* flags
394  * @return >= 0 in case a sequence was successfully read, a negative
395  * value in case of invalid sequence
396  */
398 int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
399  unsigned int flags);
400 
401 /**
402  * Check if a name is in a list.
403  * @returns 0 if not found, or the 1 based index where it has been found in the
404  * list.
405  */
406 int av_match_list(const char *name, const char *list, char separator);
407 
408 /**
409  * See libc sscanf manual for more information.
410  * Locale-independent sscanf implementation.
411  */
412 int av_sscanf(const char *string, const char *format, ...);
413 
414 /**
415  * @}
416  */
417 
418 #endif /* AVUTIL_AVSTRING_H */
#define av_const
Definition: attributes.h:76
av_warn_unused_result int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end, unsigned int flags)
Read and decode a single UTF-8 code point (character) from the buffer in *buf, and update *buf to poi...
Definition: avstring.c:371
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:206
const char * fmt
Definition: avisynth_c.h:861
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
Definition: avstring.h:140
char * av_stristr(const char *haystack, const char *needle)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
av_warn_unused_result int av_escape(char **dst, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape string in src, and put the escaped string in an allocated string in *dst, which must be freed ...
Definition: avstring.c:331
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
Use backslash escaping.
Definition: avstring.h:320
#define src
Definition: vp8dsp.c:254
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
Macro definitions for various function/variable attributes.
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
Definition: avstring.c:45
const char * av_basename(const char *path)
Thread safe basename.
Definition: avstring.c:258
uint8_t
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
const char * from
Definition: jacosubdec.c:65
char char * av_d2str(double d)
Convert a number to an av_malloced string.
Definition: avstring.c:139
Use single-quote escaping.
Definition: avstring.h:321
ptrdiff_t size
Definition: opengl_enc.c:100
const char * to
Definition: webvttdec.c:34
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:241
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition: avstring.c:447
char * av_strireplace(const char *str, const char *from, const char *to)
Locale-independent strings replace.
Definition: avstring.c:235
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition: avstring.c:348
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:962
#define av_printf_format(fmtpos, attrpos)
Definition: attributes.h:155
Use auto-selected escaping mode.
Definition: avstring.h:319
#define b
Definition: input.c:41
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
int32_t
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining list
void * buf
Definition: avisynth_c.h:766
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:231
static av_const int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.h:251
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:93
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define av_warn_unused_result
Definition: attributes.h:56
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
static av_const int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
Definition: avstring.h:214
char * av_append_path_component(const char *path, const char *component)
Append path component to the existing path.
Definition: avstring.c:300
char * av_strnstr(const char *haystack, const char *needle, size_t hay_length)
Locate the first occurrence of the string needle in the string haystack where not more than hay_lengt...
Definition: avstring.c:69
const char * av_dirname(char *path)
Thread safe dirname.
Definition: avstring.c:279
int len
AVEscapeMode
Definition: avstring.h:318
char * av_asprintf(const char *fmt,...) av_printf_format(1
Print arguments following specified format into a large enough auto allocated buffer.
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...) av_printf_format(3
Append output to a string, according to a format.
const char * name
Definition: opengl_enc.c:102