Skip to content

Commit 1578563

Browse files
fix arrsort to use the new lengths array
1 parent b97c4a8 commit 1578563

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

src/basic/array.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,34 +1110,21 @@ static int compare_double_array_desc(const void* a, const void* b)
11101110
return 0;
11111111
}
11121112

1113-
static int compare_string_array_asc(const void* a, const void* b)
1114-
{
1115-
const char* left = *(char* const*)a;
1116-
const char* right = *(char* const*)b;
1113+
struct string_array_sort_item {
1114+
char* value;
1115+
size_t value_length;
1116+
};
11171117

1118-
if (!left) {
1119-
left = "";
1120-
}
1121-
if (!right) {
1122-
right = "";
1123-
}
1124-
1125-
return strcmp(left, right);
1118+
static int compare_string_array_item_asc(const void* a, const void* b)
1119+
{
1120+
const struct string_array_sort_item* aa = a;
1121+
const struct string_array_sort_item* bb = b;
1122+
return strcmp(aa->value ? aa->value : "", bb->value ? bb->value : "");
11261123
}
11271124

1128-
static int compare_string_array_desc(const void* a, const void* b)
1125+
static int compare_string_array_item_desc(const void* a, const void* b)
11291126
{
1130-
const char* left = *(char* const*)a;
1131-
const char* right = *(char* const*)b;
1132-
1133-
if (!left) {
1134-
left = "";
1135-
}
1136-
if (!right) {
1137-
right = "";
1138-
}
1139-
1140-
return strcmp(right, left);
1127+
return -compare_string_array_item_asc(a, b);
11411128
}
11421129

11431130
static void basic_sort_int_array(const char* var, bool descending, struct basic_ctx* ctx)
@@ -1192,12 +1179,30 @@ static void basic_sort_string_array(const char* var, bool descending, struct bas
11921179
return;
11931180
}
11941181

1182+
struct string_array_sort_item* items = buddy_malloc(ctx->allocator, cur->itemcount * sizeof(struct string_array_sort_item));
1183+
if (!items) {
1184+
tokenizer_error_print(ctx, "Out of memory");
1185+
return;
1186+
}
1187+
1188+
for (uint64_t i = 0; i < cur->itemcount; i++) {
1189+
items[i].value = cur->values[i];
1190+
items[i].value_length = cur->value_lengths[i];
1191+
}
1192+
11951193
qsort(
1196-
cur->values,
1194+
items,
11971195
cur->itemcount,
1198-
sizeof(char*),
1199-
descending ? compare_string_array_desc : compare_string_array_asc
1196+
sizeof(items[0]),
1197+
descending ? compare_string_array_item_desc : compare_string_array_item_asc
12001198
);
1199+
1200+
for (uint64_t i = 0; i < cur->itemcount; i++) {
1201+
cur->values[i] = items[i].value;
1202+
cur->value_lengths[i] = items[i].value_length;
1203+
}
1204+
1205+
buddy_free(ctx->allocator, items);
12011206
}
12021207

12031208
void arrsort_statement(struct basic_ctx* ctx)
@@ -1542,4 +1547,4 @@ void arrsortby_statement(struct basic_ctx* ctx)
15421547
}
15431548

15441549
tokenizer_error_printf(ctx, "No such array variable '%s'", key_var);
1545-
}
1550+
}

0 commit comments

Comments
 (0)