a/util: Add a new constructor and a new method to u_string_list

This commit is contained in:
Ryan Pavlik 2022-01-24 16:10:37 -06:00
parent d057f00b54
commit 9b9e23be62
2 changed files with 60 additions and 0 deletions

View file

@ -58,6 +58,22 @@ u_string_list_create_from_list(struct u_string_list *usl)
} }
} }
struct u_string_list *
u_string_list_create_from_array(const char *const *arr, uint32_t size)
{
if (arr == nullptr || size == 0) {
return u_string_list_create();
}
try {
auto ret = std::make_unique<u_string_list>(xrt::auxiliary::util::StringList{size});
for (uint32_t i = 0; i < size; ++i) {
ret->list.push_back(arr[i]);
}
return ret.release();
} catch (std::exception const &) {
return nullptr;
}
}
uint32_t uint32_t
u_string_list_get_size(const struct u_string_list *usl) u_string_list_get_size(const struct u_string_list *usl)
@ -95,6 +111,23 @@ u_string_list_append(struct u_string_list *usl, const char *str)
} }
} }
int
u_string_list_append_array(struct u_string_list *usl, const char *const *arr, uint32_t size)
{
if (usl == nullptr) {
return -1;
}
try {
for (uint32_t i = 0; i < size; ++i) {
usl->list.push_back(arr[i]);
}
return 1;
} catch (std::exception const &) {
return -1;
}
}
int int
u_string_list_append_unique(struct u_string_list *usl, const char *str) u_string_list_append_unique(struct u_string_list *usl, const char *str)
{ {

View file

@ -48,6 +48,18 @@ u_string_list_create_with_capacity(uint32_t capacity);
struct u_string_list * struct u_string_list *
u_string_list_create_from_list(struct u_string_list *usl); u_string_list_create_from_list(struct u_string_list *usl);
/*!
* @brief Create a new string list from an array of suitable strings.
*
* @param arr an array of zero or more non-null, null-terminated string that must live at least as long as the list,
* preferably string literals.
* @param size the number of elements in the array.
*
* @public @memberof u_string_list
*/
struct u_string_list *
u_string_list_create_from_array(const char *const *arr, uint32_t size);
/*! /*!
* @brief Retrieve the number of elements in the list * @brief Retrieve the number of elements in the list
* *
@ -77,6 +89,21 @@ u_string_list_get_data(const struct u_string_list *usl);
int int
u_string_list_append(struct u_string_list *usl, const char *str); u_string_list_append(struct u_string_list *usl, const char *str);
/*!
* @brief Append an array of new string literals to the list.
*
* @param usl self pointer
* @param arr an array of zero or more non-null, null-terminated string that must live at least as long as the list,
* preferably string literals.
* @param size the number of elements in the array.
* @return 1 if successfully added, negative for errors.
*
* @public @memberof u_string_list
*/
int
u_string_list_append_array(struct u_string_list *usl, const char *const *arr, uint32_t size);
/*! /*!
* @brief Append a new string literal to the list, if it's not the same as a string already in the list. * @brief Append a new string literal to the list, if it's not the same as a string already in the list.
* *