This commit is contained in:
2022-01-12 14:16:14 +08:00
parent 78a3e890d2
commit b064cb7f5c
4 changed files with 52 additions and 1 deletions

10
dict.h
View File

@@ -38,6 +38,16 @@ template <typename K, typename V>
bool dict_have_key(struct Dict<K, V>* d, K key) {
return linked_list_have((struct LinkedList<struct dict_entry<K, V>>*)d, key, &dict_heve_key_internal);
}
template <typename K, typename V, typename ... Args>
void dict_iter(struct Dict<K, V>* d, void(*callback)(K key, V value, Args... args), Args... args) {
if (!d || !callback) return;
struct Dict<K, V>* t = d;
callback(t->d.key, t->d.value, args...);
while (t->next) {
t = t->next;
callback(t->d.key, t->d.value, args...);
}
}
template <typename K, typename V>
bool dict_set(struct Dict<K, V>*& d, K key, V value, void(*free_func)(V) = nullptr) {
if (!d) {

View File

@@ -236,6 +236,29 @@ bool linked_list_have(struct LinkedList<T>* list, D data, bool(*compare_func)(T,
return false;
}
template <typename T>
struct LinkedList<T>* linked_list_head(struct LinkedList<T>* list) {
if (!list) return nullptr;
struct LinkedList<T>* t = list;
while (t->prev) {
t = t->prev;
}
return t;
}
template <typename T, typename ... Args>
void linked_list_iter(struct LinkedList<T>* list, void(*callback)(size_t index, T data, Args... args), Args... args) {
if (!list || !callback) return;
size_t i = 0;
struct LinkedList<T>* t = list;
callback(i, t->d, args...);
while (t->next) {
t = t->next;
i++;
callback(i, t->d, args...);
}
}
template <typename T>
void linked_list_remove(struct LinkedList<T>*& node, void(*free_func)(T) = nullptr) {
if (!node) return;

View File

@@ -45,3 +45,15 @@ std::list<std::string> str_util::str_split(std::string input, std::string patter
if (last_loc <= input.length()) li.push_back(input.substr(last_loc, input.length() - last_loc));
return li;
}
std::string str_util::str_hex(std::string input) {
if (input.empty()) return "";
const char* t = "0123456789abcdef";
std::string output;
for (auto i = input.begin(); i != input.end(); i++) {
unsigned char c = *i;
output += t[c / 16];
output += t[c % 16];
}
return output;
}

View File

@@ -27,5 +27,11 @@ namespace str_util {
* @return Result.
*/
std::list<std::string> str_split(std::string input, std::string pattern, size_t max = -1);
/**
* @brief Convert data to a lowercase hexadecimal string
* @param input Input data
* @return Output
*/
std::string str_hex(std::string input);
}
#endif