]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.h
2 * FRR string processing utilities.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
25 #include <sys/types.h>
26 #ifdef HAVE_LIBPCRE2_POSIX
27 #ifndef _FRR_PCRE2_POSIX
28 #define _FRR_PCRE2_POSIX
29 #include <pcre2posix.h>
30 #endif /* _FRR_PCRE2_POSIX */
31 #elif defined(HAVE_LIBPCREPOSIX)
32 #include <pcreposix.h>
35 #endif /* HAVE_LIBPCRE2_POSIX */
45 * Tokenizes a string, storing tokens in a vector. Whitespace is ignored.
46 * Delimiter characters are not included.
52 * Delimiter string, as used in strsep()
55 * The split string. Each token is allocated with MTYPE_TMP.
57 void frrstr_split(const char *string
, const char *delimiter
, char ***result
,
59 vector
frrstr_split_vec(const char *string
, const char *delimiter
);
62 * Concatenate string array into a single string.
65 * array of string pointers to concatenate
71 * string to insert between each part, or NULL for nothing
74 * the joined string, allocated with MTYPE_TMP
76 char *frrstr_join(const char **parts
, int argc
, const char *join
);
77 char *frrstr_join_vec(vector v
, const char *join
);
80 * Filter string vector.
81 * Removes lines that do not contain a match for the provided regex.
84 * The vector to filter.
87 * Regex to filter with.
89 void frrstr_filter_vec(vector v
, regex_t
*filter
);
92 * Free allocated string vector.
93 * Assumes each item is allocated with MTYPE_TMP.
98 void frrstr_strvec_free(vector v
);
101 * Given a string, replaces all occurrences of a substring with a different
102 * string. The result is a new string. The original string is not modified.
104 * If 'replace' is longer than 'find', this function performs N+1 allocations,
105 * where N is the number of times 'find' occurs in 'str'. If 'replace' is equal
106 * in length or shorter than 'find', only 1 allocation is performed.
109 * String to perform replacement on.
112 * Substring to replace.
115 * String to replace 'find' with.
118 * A new string, allocated with MTYPE_TMP, that is the result of performing
119 * the replacement on 'str'. This must be freed by the caller.
121 char *frrstr_replace(const char *str
, const char *find
, const char *replace
);
124 * Prefix match for string.
127 * string to check for prefix match
133 * true if str starts with prefix, false otherwise
135 bool frrstr_startswith(const char *str
, const char *prefix
);
138 * Suffix match for string.
141 * string to check for suffix match
147 * true if str ends with suffix, false otherwise
149 bool frrstr_endswith(const char *str
, const char *suffix
);
152 * Check the string only contains digit characters.
155 * string to check for digits
158 * 1 str only contains digit characters, 0 otherwise
160 int all_digit(const char *str
);
163 * Copy the hexadecimal representation of the string to a buffer.
166 * Buffer to copy result into with size of at least (2 * num) + 1.
169 * Size of destination buffer.
172 * String to represent as hexadecimal.
175 * Number of characters to copy.
178 * Pointer to buffer containing resulting hexadecimal representation.
180 char *frrstr_hex(char *buff
, size_t bufsiz
, const uint8_t *str
, size_t num
);
186 #endif /* _FRRSTR_H_ */