Add array_contains0() and array_count0() function to tools module

This commit is contained in:
Thomas Jensen 2021-04-02 15:45:51 +02:00
parent b51724f3b1
commit aab643acbc
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
2 changed files with 43 additions and 0 deletions

View File

@ -653,4 +653,33 @@ int array_contains(char **array, const size_t array_len, const char *s)
}
int array_contains0(char **array, const char *s)
{
int result = 0;
if (array != NULL) {
for (size_t i = 0; array[i] != NULL; ++i) {
if (strcasecmp(array[i], s) == 0) {
result = 1;
break;
}
}
}
return result;
}
size_t array_count0(char **array)
{
size_t num_elems = 0;
if (array != NULL) {
while (array[num_elems] != NULL) {
++num_elems;
}
}
return num_elems;
}
/*EOF*/ /* vim: set sw=4: */

View File

@ -82,6 +82,20 @@ void analyze_line_ascii(line_t *line);
int array_contains(char **array, const size_t array_len, const char *s);
/**
* Determine if the given `array` contains the given string (case-insensitive!).
* @param array an array of strings to search
* @param s the string to search
* @returns != 0 if found, 0 if not found
*/
int array_contains0(char **array, const char *s);
/**
* Count the number of elements in a zero-terminated array.
* @param array a zero-terminated array of strings (can be NULL)
* @returns the number of elements, excluding the zero terminator
*/
size_t array_count0(char **array);
#endif