clean up list sort
parent
cf21ba0a86
commit
4bbe2ca6df
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
namespace cscript {
|
namespace cscript {
|
||||||
|
|
||||||
char *cs_dup_ostr(ostd::ConstCharRange s);
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct CsArgVal;
|
struct CsArgVal;
|
||||||
|
|
||||||
|
@ -502,7 +500,7 @@ end:
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ListSortItem {
|
struct ListSortItem {
|
||||||
char const *str;
|
ostd::ConstCharRange str;
|
||||||
ostd::ConstCharRange quote;
|
ostd::ConstCharRange quote;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -531,19 +529,16 @@ static void cs_list_sort(
|
||||||
CsAlias *xa = static_cast<CsAlias *>(x), *ya = static_cast<CsAlias *>(y);
|
CsAlias *xa = static_cast<CsAlias *>(x), *ya = static_cast<CsAlias *>(y);
|
||||||
|
|
||||||
CsVector<ListSortItem> items;
|
CsVector<ListSortItem> items;
|
||||||
ostd::Size clen = list.size();
|
|
||||||
ostd::Size total = 0;
|
ostd::Size total = 0;
|
||||||
|
|
||||||
char *cstr = cs_dup_ostr(list);
|
|
||||||
for (util::ListParser p(list); p.parse();) {
|
for (util::ListParser p(list); p.parse();) {
|
||||||
cstr[&p.item[p.item.size()] - list.data()] = '\0';
|
ListSortItem item = { p.item, p.quote };
|
||||||
ListSortItem item = { &cstr[p.item.data() - list.data()], p.quote };
|
|
||||||
items.push(item);
|
items.push(item);
|
||||||
total += item.quote.size();
|
total += item.quote.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (items.empty()) {
|
if (items.empty()) {
|
||||||
res.set_mstr(cstr);
|
res.set_str(list);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,28 +590,19 @@ static void cs_list_sort(
|
||||||
xval.pop();
|
xval.pop();
|
||||||
yval.pop();
|
yval.pop();
|
||||||
|
|
||||||
char *sorted = cstr;
|
CsString sorted;
|
||||||
ostd::Size sortedlen = totaluniq + ostd::max(nuniq - 1, ostd::Size(0));
|
sorted.reserve(totaluniq + ostd::max(nuniq - 1, ostd::Size(0)));
|
||||||
if (clen < sortedlen) {
|
|
||||||
delete[] cstr;
|
|
||||||
sorted = new char[sortedlen + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
ostd::Size offset = 0;
|
|
||||||
for (ostd::Size i = 0; i < items.size(); ++i) {
|
for (ostd::Size i = 0; i < items.size(); ++i) {
|
||||||
ListSortItem &item = items[i];
|
ListSortItem &item = items[i];
|
||||||
if (item.quote.empty()) {
|
if (item.quote.empty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (i) {
|
if (i) {
|
||||||
sorted[offset++] = ' ';
|
sorted += ' ';
|
||||||
}
|
}
|
||||||
memcpy(&sorted[offset], item.quote.data(), item.quote.size());
|
sorted += item.quote;
|
||||||
offset += item.quote.size();
|
|
||||||
}
|
}
|
||||||
sorted[offset] = '\0';
|
res.set_str(ostd::move(sorted));
|
||||||
|
|
||||||
res.set_mstr(sorted);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cs_init_lib_list_sort(CsState &gcs) {
|
static void cs_init_lib_list_sort(CsState &gcs) {
|
||||||
|
|
Loading…
Reference in New Issue