]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * FRR string processing utilities.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
11 #include <sys/types.h>
12 #include <sys/types.h>
13 #ifdef HAVE_LIBPCRE2_POSIX
14 #ifndef _FRR_PCRE2_POSIX
15 #define _FRR_PCRE2_POSIX
16 #include <pcre2posix.h>
17 #endif /* _FRR_PCRE2_POSIX */
18 #elif defined(HAVE_LIBPCREPOSIX)
19 #include <pcreposix.h>
22 #endif /* HAVE_LIBPCRE2_POSIX */
32 * Tokenizes a string, storing tokens in a vector. Whitespace is ignored.
33 * Delimiter characters are not included.
39 * Delimiter string, as used in strsep()
42 * The split string. Each token is allocated with MTYPE_TMP.
44 void frrstr_split(const char *string
, const char *delimiter
, char ***result
,
46 vector
frrstr_split_vec(const char *string
, const char *delimiter
);
49 * Concatenate string array into a single string.
52 * array of string pointers to concatenate
58 * string to insert between each part, or NULL for nothing
61 * the joined string, allocated with MTYPE_TMP
63 char *frrstr_join(const char **parts
, int argc
, const char *join
);
64 char *frrstr_join_vec(vector v
, const char *join
);
67 * Filter string vector.
68 * Removes lines that do not contain a match for the provided regex.
71 * The vector to filter.
74 * Regex to filter with.
76 void frrstr_filter_vec(vector v
, regex_t
*filter
);
79 * Free allocated string vector.
80 * Assumes each item is allocated with MTYPE_TMP.
85 void frrstr_strvec_free(vector v
);
88 * Given a string, replaces all occurrences of a substring with a different
89 * string. The result is a new string. The original string is not modified.
91 * If 'replace' is longer than 'find', this function performs N+1 allocations,
92 * where N is the number of times 'find' occurs in 'str'. If 'replace' is equal
93 * in length or shorter than 'find', only 1 allocation is performed.
96 * String to perform replacement on.
99 * Substring to replace.
102 * String to replace 'find' with.
105 * A new string, allocated with MTYPE_TMP, that is the result of performing
106 * the replacement on 'str'. This must be freed by the caller.
108 char *frrstr_replace(const char *str
, const char *find
, const char *replace
);
111 * Prefix match for string.
114 * string to check for prefix match
120 * true if str starts with prefix, false otherwise
122 bool frrstr_startswith(const char *str
, const char *prefix
);
125 * Suffix match for string.
128 * string to check for suffix match
134 * true if str ends with suffix, false otherwise
136 bool frrstr_endswith(const char *str
, const char *suffix
);
139 * Check the string only contains digit characters.
142 * string to check for digits
145 * 1 str only contains digit characters, 0 otherwise
147 int all_digit(const char *str
);
150 * Copy the hexadecimal representation of the string to a buffer.
153 * Buffer to copy result into with size of at least (2 * num) + 1.
156 * Size of destination buffer.
159 * String to represent as hexadecimal.
162 * Number of characters to copy.
165 * Pointer to buffer containing resulting hexadecimal representation.
167 char *frrstr_hex(char *buff
, size_t bufsiz
, const uint8_t *str
, size_t num
);
173 #endif /* _FRRSTR_H_ */