Path: blob/master/dep/ffmpeg/include/libavutil/avstring.h
4216 views
/*1* Copyright (c) 2007 Mans Rullgard2*3* This file is part of FFmpeg.4*5* FFmpeg is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* FFmpeg is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with FFmpeg; if not, write to the Free Software17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA18*/1920#ifndef AVUTIL_AVSTRING_H21#define AVUTIL_AVSTRING_H2223#include <stddef.h>24#include <stdint.h>25#include "attributes.h"2627/**28* @addtogroup lavu_string29* @{30*/3132/**33* Return non-zero if pfx is a prefix of str. If it is, *ptr is set to34* the address of the first character in str after the prefix.35*36* @param str input string37* @param pfx prefix to test38* @param ptr updated if the prefix is matched inside str39* @return non-zero if the prefix matches, zero otherwise40*/41int av_strstart(const char *str, const char *pfx, const char **ptr);4243/**44* Return non-zero if pfx is a prefix of str independent of case. If45* it is, *ptr is set to the address of the first character in str46* after the prefix.47*48* @param str input string49* @param pfx prefix to test50* @param ptr updated if the prefix is matched inside str51* @return non-zero if the prefix matches, zero otherwise52*/53int av_stristart(const char *str, const char *pfx, const char **ptr);5455/**56* Locate the first case-independent occurrence in the string haystack57* of the string needle. A zero-length string needle is considered to58* match at the start of haystack.59*60* This function is a case-insensitive version of the standard strstr().61*62* @param haystack string to search in63* @param needle string to search for64* @return pointer to the located match within haystack65* or a null pointer if no match66*/67char *av_stristr(const char *haystack, const char *needle);6869/**70* Locate the first occurrence of the string needle in the string haystack71* where not more than hay_length characters are searched. A zero-length72* string needle is considered to match at the start of haystack.73*74* This function is a length-limited version of the standard strstr().75*76* @param haystack string to search in77* @param needle string to search for78* @param hay_length length of string to search in79* @return pointer to the located match within haystack80* or a null pointer if no match81*/82char *av_strnstr(const char *haystack, const char *needle, size_t hay_length);8384/**85* Copy the string src to dst, but no more than size - 1 bytes, and86* null-terminate dst.87*88* This function is the same as BSD strlcpy().89*90* @param dst destination buffer91* @param src source string92* @param size size of destination buffer93* @return the length of src94*95* @warning since the return value is the length of src, src absolutely96* _must_ be a properly 0-terminated string, otherwise this will read beyond97* the end of the buffer and possibly crash.98*/99size_t av_strlcpy(char *dst, const char *src, size_t size);100101/**102* Append the string src to the string dst, but to a total length of103* no more than size - 1 bytes, and null-terminate dst.104*105* This function is similar to BSD strlcat(), but differs when106* size <= strlen(dst).107*108* @param dst destination buffer109* @param src source string110* @param size size of destination buffer111* @return the total length of src and dst112*113* @warning since the return value use the length of src and dst, these114* absolutely _must_ be a properly 0-terminated strings, otherwise this115* will read beyond the end of the buffer and possibly crash.116*/117size_t av_strlcat(char *dst, const char *src, size_t size);118119/**120* Append output to a string, according to a format. Never write out of121* the destination buffer, and always put a terminating 0 within122* the buffer.123* @param dst destination buffer (string to which the output is124* appended)125* @param size total size of the destination buffer126* @param fmt printf-compatible format string, specifying how the127* following parameters are used128* @return the length of the string that would have been generated129* if enough space had been available130*/131size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);132133/**134* Get the count of continuous non zero chars starting from the beginning.135*136* @param s the string whose length to count137* @param len maximum number of characters to check in the string, that138* is the maximum value which is returned by the function139*/140static inline size_t av_strnlen(const char *s, size_t len)141{142size_t i;143for (i = 0; i < len && s[i]; i++)144;145return i;146}147148/**149* Print arguments following specified format into a large enough auto150* allocated buffer. It is similar to GNU asprintf().151* @param fmt printf-compatible format string, specifying how the152* following parameters are used.153* @return the allocated string154* @note You have to free the string yourself with av_free().155*/156char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);157158/**159* Unescape the given string until a non escaped terminating char,160* and return the token corresponding to the unescaped string.161*162* The normal \ and ' escaping is supported. Leading and trailing163* whitespaces are removed, unless they are escaped with '\' or are164* enclosed between ''.165*166* @param buf the buffer to parse, buf will be updated to point to the167* terminating char168* @param term a 0-terminated list of terminating chars169* @return the malloced unescaped string, which must be av_freed by170* the user, NULL in case of allocation failure171*/172char *av_get_token(const char **buf, const char *term);173174/**175* Split the string into several tokens which can be accessed by176* successive calls to av_strtok().177*178* A token is defined as a sequence of characters not belonging to the179* set specified in delim.180*181* On the first call to av_strtok(), s should point to the string to182* parse, and the value of saveptr is ignored. In subsequent calls, s183* should be NULL, and saveptr should be unchanged since the previous184* call.185*186* This function is similar to strtok_r() defined in POSIX.1.187*188* @param s the string to parse, may be NULL189* @param delim 0-terminated list of token delimiters, must be non-NULL190* @param saveptr user-provided pointer which points to stored191* information necessary for av_strtok() to continue scanning the same192* string. saveptr is updated to point to the next character after the193* first delimiter found, or to NULL if the string was terminated194* @return the found token, or NULL when no token is found195*/196char *av_strtok(char *s, const char *delim, char **saveptr);197198/**199* Locale-independent conversion of ASCII isdigit.200*/201static inline av_const int av_isdigit(int c)202{203return c >= '0' && c <= '9';204}205206/**207* Locale-independent conversion of ASCII isgraph.208*/209static inline av_const int av_isgraph(int c)210{211return c > 32 && c < 127;212}213214/**215* Locale-independent conversion of ASCII isspace.216*/217static inline av_const int av_isspace(int c)218{219return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||220c == '\v';221}222223/**224* Locale-independent conversion of ASCII characters to uppercase.225*/226static inline av_const int av_toupper(int c)227{228if (c >= 'a' && c <= 'z')229c ^= 0x20;230return c;231}232233/**234* Locale-independent conversion of ASCII characters to lowercase.235*/236static inline av_const int av_tolower(int c)237{238if (c >= 'A' && c <= 'Z')239c ^= 0x20;240return c;241}242243/**244* Locale-independent conversion of ASCII isxdigit.245*/246static inline av_const int av_isxdigit(int c)247{248c = av_tolower(c);249return av_isdigit(c) || (c >= 'a' && c <= 'f');250}251252/**253* Locale-independent case-insensitive compare.254* @note This means only ASCII-range characters are case-insensitive255*/256int av_strcasecmp(const char *a, const char *b);257258/**259* Locale-independent case-insensitive compare.260* @note This means only ASCII-range characters are case-insensitive261*/262int av_strncasecmp(const char *a, const char *b, size_t n);263264/**265* Locale-independent strings replace.266* @note This means only ASCII-range characters are replaced.267*/268char *av_strireplace(const char *str, const char *from, const char *to);269270/**271* Thread safe basename.272* @param path the string to parse, on DOS both \ and / are considered separators.273* @return pointer to the basename substring.274* If path does not contain a slash, the function returns a copy of path.275* If path is a NULL pointer or points to an empty string, a pointer276* to a string "." is returned.277*/278const char *av_basename(const char *path);279280/**281* Thread safe dirname.282* @param path the string to parse, on DOS both \ and / are considered separators.283* @return A pointer to a string that's the parent directory of path.284* If path is a NULL pointer or points to an empty string, a pointer285* to a string "." is returned.286* @note the function may modify the contents of the path, so copies should be passed.287*/288const char *av_dirname(char *path);289290/**291* Match instances of a name in a comma-separated list of names.292* List entries are checked from the start to the end of the names list,293* the first match ends further processing. If an entry prefixed with '-'294* matches, then 0 is returned. The "ALL" list entry is considered to295* match all names.296*297* @param name Name to look for.298* @param names List of names.299* @return 1 on match, 0 otherwise.300*/301int av_match_name(const char *name, const char *names);302303/**304* Append path component to the existing path.305* Path separator '/' is placed between when needed.306* Resulting string have to be freed with av_free().307* @param path base path308* @param component component to be appended309* @return new path or NULL on error.310*/311char *av_append_path_component(const char *path, const char *component);312313enum AVEscapeMode {314AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.315AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.316AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping.317AV_ESCAPE_MODE_XML, ///< Use XML non-markup character data escaping.318};319320/**321* Consider spaces special and escape them even in the middle of the322* string.323*324* This is equivalent to adding the whitespace characters to the special325* characters lists, except it is guaranteed to use the exact same list326* of whitespace characters as the rest of libavutil.327*/328#define AV_ESCAPE_FLAG_WHITESPACE (1 << 0)329330/**331* Escape only specified special characters.332* Without this flag, escape also any characters that may be considered333* special by av_get_token(), such as the single quote.334*/335#define AV_ESCAPE_FLAG_STRICT (1 << 1)336337/**338* Within AV_ESCAPE_MODE_XML, additionally escape single quotes for single339* quoted attributes.340*/341#define AV_ESCAPE_FLAG_XML_SINGLE_QUOTES (1 << 2)342343/**344* Within AV_ESCAPE_MODE_XML, additionally escape double quotes for double345* quoted attributes.346*/347#define AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES (1 << 3)348349350/**351* Escape string in src, and put the escaped string in an allocated352* string in *dst, which must be freed with av_free().353*354* @param dst pointer where an allocated string is put355* @param src string to escape, must be non-NULL356* @param special_chars string containing the special characters which357* need to be escaped, can be NULL358* @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.359* Any unknown value for mode will be considered equivalent to360* AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without361* notice.362* @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros363* @return the length of the allocated string, or a negative error code in case of error364* @see av_bprint_escape()365*/366av_warn_unused_result367int av_escape(char **dst, const char *src, const char *special_chars,368enum AVEscapeMode mode, int flags);369370#define AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES 1 ///< accept codepoints over 0x10FFFF371#define AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS 2 ///< accept non-characters - 0xFFFE and 0xFFFF372#define AV_UTF8_FLAG_ACCEPT_SURROGATES 4 ///< accept UTF-16 surrogates codes373#define AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES 8 ///< exclude control codes not accepted by XML374375#define AV_UTF8_FLAG_ACCEPT_ALL \376AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES|AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS|AV_UTF8_FLAG_ACCEPT_SURROGATES377378/**379* Read and decode a single UTF-8 code point (character) from the380* buffer in *buf, and update *buf to point to the next byte to381* decode.382*383* In case of an invalid byte sequence, the pointer will be updated to384* the next byte after the invalid sequence and the function will385* return an error code.386*387* Depending on the specified flags, the function will also fail in388* case the decoded code point does not belong to a valid range.389*390* @note For speed-relevant code a carefully implemented use of391* GET_UTF8() may be preferred.392*393* @param codep pointer used to return the parsed code in case of success.394* The value in *codep is set even in case the range check fails.395* @param bufp pointer to the address the first byte of the sequence396* to decode, updated by the function to point to the397* byte next after the decoded sequence398* @param buf_end pointer to the end of the buffer, points to the next399* byte past the last in the buffer. This is used to400* avoid buffer overreads (in case of an unfinished401* UTF-8 sequence towards the end of the buffer).402* @param flags a collection of AV_UTF8_FLAG_* flags403* @return >= 0 in case a sequence was successfully read, a negative404* value in case of invalid sequence405*/406av_warn_unused_result407int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,408unsigned int flags);409410/**411* Check if a name is in a list.412* @returns 0 if not found, or the 1 based index where it has been found in the413* list.414*/415int av_match_list(const char *name, const char *list, char separator);416417/**418* See libc sscanf manual for more information.419* Locale-independent sscanf implementation.420*/421int av_sscanf(const char *string, const char *format, ...);422423/**424* @}425*/426427#endif /* AVUTIL_AVSTRING_H */428429430