mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-07-08 01:30:39 +08:00
Update
This commit is contained in:
10
dict.h
10
dict.h
@@ -38,6 +38,16 @@ template <typename K, typename V>
|
|||||||
bool dict_have_key(struct Dict<K, V>* d, K key) {
|
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);
|
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>
|
template <typename K, typename V>
|
||||||
bool dict_set(struct Dict<K, V>*& d, K key, V value, void(*free_func)(V) = nullptr) {
|
bool dict_set(struct Dict<K, V>*& d, K key, V value, void(*free_func)(V) = nullptr) {
|
||||||
if (!d) {
|
if (!d) {
|
||||||
|
|||||||
@@ -236,6 +236,29 @@ bool linked_list_have(struct LinkedList<T>* list, D data, bool(*compare_func)(T,
|
|||||||
return false;
|
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>
|
template <typename T>
|
||||||
void linked_list_remove(struct LinkedList<T>*& node, void(*free_func)(T) = nullptr) {
|
void linked_list_remove(struct LinkedList<T>*& node, void(*free_func)(T) = nullptr) {
|
||||||
if (!node) return;
|
if (!node) return;
|
||||||
|
|||||||
14
str_util.cpp
14
str_util.cpp
@@ -44,4 +44,16 @@ 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));
|
if (last_loc <= input.length()) li.push_back(input.substr(last_loc, input.length() - last_loc));
|
||||||
return li;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,5 +27,11 @@ namespace str_util {
|
|||||||
* @return Result.
|
* @return Result.
|
||||||
*/
|
*/
|
||||||
std::list<std::string> str_split(std::string input, std::string pattern, size_t max = -1);
|
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
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user