From b064cb7f5cfe58d56d82061d8b718af0305ce439 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 12 Jan 2022 14:16:14 +0800 Subject: [PATCH] Update --- dict.h | 10 ++++++++++ linked_list.h | 23 +++++++++++++++++++++++ str_util.cpp | 14 +++++++++++++- str_util.h | 6 ++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/dict.h b/dict.h index caece24..c5d1fcf 100644 --- a/dict.h +++ b/dict.h @@ -38,6 +38,16 @@ template bool dict_have_key(struct Dict* d, K key) { return linked_list_have((struct LinkedList>*)d, key, &dict_heve_key_internal); } +template +void dict_iter(struct Dict* d, void(*callback)(K key, V value, Args... args), Args... args) { + if (!d || !callback) return; + struct Dict* 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 bool dict_set(struct Dict*& d, K key, V value, void(*free_func)(V) = nullptr) { if (!d) { diff --git a/linked_list.h b/linked_list.h index 1dd3596..b9231d6 100644 --- a/linked_list.h +++ b/linked_list.h @@ -236,6 +236,29 @@ bool linked_list_have(struct LinkedList* list, D data, bool(*compare_func)(T, return false; } +template +struct LinkedList* linked_list_head(struct LinkedList* list) { + if (!list) return nullptr; + struct LinkedList* t = list; + while (t->prev) { + t = t->prev; + } + return t; +} + +template +void linked_list_iter(struct LinkedList* list, void(*callback)(size_t index, T data, Args... args), Args... args) { + if (!list || !callback) return; + size_t i = 0; + struct LinkedList* t = list; + callback(i, t->d, args...); + while (t->next) { + t = t->next; + i++; + callback(i, t->d, args...); + } +} + template void linked_list_remove(struct LinkedList*& node, void(*free_func)(T) = nullptr) { if (!node) return; diff --git a/str_util.cpp b/str_util.cpp index 26fb974..a66b6e7 100644 --- a/str_util.cpp +++ b/str_util.cpp @@ -44,4 +44,16 @@ std::list 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; -} \ No newline at end of file +} + +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; +} diff --git a/str_util.h b/str_util.h index e70cb42..e3ac7c6 100644 --- a/str_util.h +++ b/str_util.h @@ -27,5 +27,11 @@ namespace str_util { * @return Result. */ std::list 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