Simply curl_slist_sort_insert (#1494)

Some good taste from Linus:
https://github.com/mkirchner/linked-list-good-taste
Also avoid an allocation when replacing a value and tighten up tests.
This commit is contained in:
Andrew Gaul 2020-12-23 20:29:33 +09:00 committed by GitHub
parent 4d0daddad4
commit a16d00d673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 43 deletions

View File

@ -55,31 +55,22 @@ struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* d
struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* key, const char* value) struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* key, const char* value)
{ {
struct curl_slist* curpos;
struct curl_slist* lastpos;
struct curl_slist* new_item;
if(!key){ if(!key){
return list; return list;
} }
if(NULL == (new_item = reinterpret_cast<struct curl_slist*>(malloc(sizeof(struct curl_slist))))){
return list;
}
// key & value are trimmed and lower (only key) // key & value are trimmed and lower (only key)
std::string strkey = trim(std::string(key)); std::string strkey = trim(std::string(key));
std::string strval = trim(std::string(value ? value : "")); std::string strval = value ? trim(std::string(value)) : "";
std::string strnew = key + std::string(": ") + strval; std::string strnew = key + std::string(": ") + strval;
if(NULL == (new_item->data = strdup(strnew.c_str()))){ char* data;
free(new_item); if(NULL == (data = strdup(strnew.c_str()))){
return list; return list;
} }
new_item->next = NULL;
// cppcheck-suppress unmatchedSuppression struct curl_slist **p = &list;
// cppcheck-suppress nullPointerRedundantCheck for(;*p; p = &(*p)->next){
for(lastpos = NULL, curpos = list; curpos; lastpos = curpos, curpos = curpos->next){ std::string strcur = (*p)->data;
std::string strcur = curpos->data;
size_t pos; size_t pos;
if(std::string::npos != (pos = strcur.find(':', 0))){ if(std::string::npos != (pos = strcur.find(':', 0))){
strcur = strcur.substr(0, pos); strcur = strcur.substr(0, pos);
@ -87,38 +78,25 @@ struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* k
int result = strcasecmp(strkey.c_str(), strcur.c_str()); int result = strcasecmp(strkey.c_str(), strcur.c_str());
if(0 == result){ if(0 == result){
// same data, so replace it. free((*p)->data);
if(lastpos){ (*p)->data = data;
lastpos->next = new_item; return list;
}else{ }else if(result < 0){
list = new_item;
}
new_item->next = curpos->next;
free(curpos->data);
free(curpos);
break;
}else if(0 > result){
// add data before curpos.
if(lastpos){
lastpos->next = new_item;
}else{
list = new_item;
}
new_item->next = curpos;
break; break;
} }
} }
if(!curpos){ struct curl_slist* new_item;
// append to last pos if(NULL == (new_item = reinterpret_cast<struct curl_slist*>(malloc(sizeof(*new_item))))){
if(lastpos){ free(data);
lastpos->next = new_item; return list;
}else{
// a case of list is null
list = new_item;
}
} }
struct curl_slist* before = *p;
*p = new_item;
new_item->data = data;
new_item->next = before;
return list; return list;
} }

View File

@ -28,7 +28,7 @@
void assert_is_sorted(struct curl_slist* list, const char *file, int line) void assert_is_sorted(struct curl_slist* list, const char *file, int line)
{ {
for(; list != NULL && list->next != NULL; list = list->next){ for(; list != NULL; list = list->next){
std::string key1 = list->data; std::string key1 = list->data;
key1.erase(key1.find(':')); key1.erase(key1.find(':'));
std::string key2 = list->data; std::string key2 = list->data;
@ -40,6 +40,7 @@ void assert_is_sorted(struct curl_slist* list, const char *file, int line)
std::exit(1); std::exit(1);
} }
} }
std::cerr << std::endl;
} }
size_t curl_slist_length(const struct curl_slist* list) size_t curl_slist_length(const struct curl_slist* list)
@ -67,11 +68,13 @@ void test_sort_insert()
// add to head // add to head
list = curl_slist_sort_insert(list, "1", "val"); list = curl_slist_sort_insert(list, "1", "val");
ASSERT_IS_SORTED(list); ASSERT_IS_SORTED(list);
ASSERT_STREQUALS("1: val", list->data);
// replace head // replace head
list = curl_slist_sort_insert(list, "1", "val2"); list = curl_slist_sort_insert(list, "1", "val2");
ASSERT_IS_SORTED(list); ASSERT_IS_SORTED(list);
ASSERT_EQUALS(static_cast<size_t>(4), curl_slist_length(list)); ASSERT_EQUALS(static_cast<size_t>(4), curl_slist_length(list));
ASSERT_STREQUALS("1: val2", list->data); ASSERT_STREQUALS("1: val2", list->data);
curl_slist_free_all(list);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])