有没有办法缩短这部分代码?对于这个添加功能。遇到一些困难。基本上我想做一个列表,生成域的 TLD 并根据用户输入日期的方式显示它。但我觉得我的代码相当冗长,我希望缩短它。这是我希望缩短的功能之一。
int tldlist_add(TLDList *tld, char *hostname, Date *d) {
// check if it's within the tld dates
if (date_compare(tld->begin, d) > 0 ||
date_compare(tld->end, d) < 0)
return 0;
// capture the actual tld
char *tld_start = strrchr(hostname, '.') + 1;
int tld_len = strlen(tld_start);
char *tld_s = (char *)malloc(sizeof(char) * (tld_len + 1));
tld_s[tld_len] = '\0'; // make sure there is a null end
strcpy(tld_s, tld_start);
if (tld->root == NULL) {
// list is empty, just add to root
TLDNode *node = tldnode_create(tld_s);
node->count++;
tld->root = node;
tld->size++; // new node
} else {
// add to list. do we need to free the string?
if (!tldlist_add_r(tld, tld_s, tld->root))
free(tld_s);
else {
tld->size++; // new node, so increase size
// we must now rebalance the tree
tldlist_rebalance(tld);
}
}
tld->count++;
return 1; // maximum success
}