BLI: Add two helpers to search a given string in an array of strings.

This commit is contained in:
Bastien Montagne 2014-06-17 15:58:07 +02:00
parent 8f30414c29
commit ab5f4c4dfa
2 changed files with 45 additions and 0 deletions

View File

@ -76,6 +76,9 @@ void BLI_ascii_strtolower(char *str, const size_t len) ATTR_NONNULL();
void BLI_ascii_strtoupper(char *str, const size_t len) ATTR_NONNULL();
int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL();
int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len) ATTR_NONNULL();
int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array) ATTR_NONNULL();
#ifdef __cplusplus
}
#endif

View File

@ -639,3 +639,45 @@ int BLI_str_rstrip_float_zero(char *str, const char pad)
return totstrip;
}
/**
* Return index of a string in a string array.
*
* \param str The string to find.
* \param str_array Array of strings.
* \param str_array_len The length of the array, or -1 for a NULL-terminated array.
* \return The index of str in str_array or -1.
*/
int BLI_str_index_in_array_n(const char *str, const char **str_array, const int str_array_len)
{
int index;
const char **str_iter = str_array;
for (index = 0; index < str_array_len; str_iter++, index++) {
if (STREQ(str, *str_iter)) {
return index;
}
}
return -1;
}
/**
* Return index of a string in a string array.
*
* \param str The string to find.
* \param str_array Array of strings, (must be NULL-terminated).
* \return The index of str in str_array or -1.
*/
int BLI_str_index_in_array(const char *str, const char **str_array)
{
int index;
const char **str_iter = str_array;
for (index = 0; *str_iter; str_iter++, index++) {
if (STREQ(str, *str_iter)) {
return index;
}
}
return -1;
}