mirror of
https://gittea.dev/nova/th.git
synced 2025-10-22 10:50:15 -04:00
improvements to sort_natural
This commit is contained in:
108
sorting.c
108
sorting.c
@@ -24,75 +24,79 @@ int skip_dot(const struct dirent *entry){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sort_natural(const void *file0_, const void *file1_){
|
int sort_natural(const void *file0, const void *file1){
|
||||||
file *file0 = (file*)file0_;
|
|
||||||
file *file1 = (file*)file1_;
|
|
||||||
|
|
||||||
unsigned char *a = (unsigned char*)file0->file_name;
|
if (((file*)file0)->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR) && !(((file*)file1)->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR))) {
|
||||||
unsigned char *b = (unsigned char*)file1->file_name;
|
|
||||||
|
|
||||||
if (file0->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR) && !(file1->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR))) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (!(file0->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) && file1->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) {
|
if (!(((file*)file0)->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) && ((file*)file1)->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
unsigned long num0 = 0;
|
const unsigned char *a = (unsigned char*)((file*)file0)->file_name;
|
||||||
unsigned long num1 = 0;
|
const unsigned char *b = (unsigned char*)((file*)file1)->file_name;
|
||||||
/* bitwise OR a with ' ' turns turns caps into small letters
|
|
||||||
* while doing this on all chars may cause unexpected behaviour on the extended ascii set,
|
long parsed_number0 = 0;
|
||||||
* for now i dont care */
|
long parsed_number1 = 0;
|
||||||
while ((*a | ' ') == (*b | ' ') && *a != '\0') {
|
char is_num = 0;
|
||||||
|
char result = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
is_num = 0;
|
||||||
|
|
||||||
if ((*a >= '0') && (*a <= '9')) {
|
if ((*a >= '0') && (*a <= '9')) {
|
||||||
|
parsed_number0 = 0;
|
||||||
while((*a >= '0') && (*a <= '9')) {
|
while((*a >= '0') && (*a <= '9')) {
|
||||||
num0 = (num0 * 10) + (*a - '0');
|
parsed_number0 = (parsed_number0 * 10) + (*a - '0');
|
||||||
a++;
|
a++;
|
||||||
}
|
}
|
||||||
|
is_num |= 1;
|
||||||
|
}
|
||||||
|
if ((*b >= '0') && (*b <= '9')) {
|
||||||
|
parsed_number1 = 0;
|
||||||
while((*b >= '0') && (*b <= '9')) {
|
while((*b >= '0') && (*b <= '9')) {
|
||||||
num1 = (num1 * 10) + (*b - '0');
|
parsed_number1 = (parsed_number1 * 10) + (*b - '0');
|
||||||
b++;
|
b++;
|
||||||
}
|
}
|
||||||
if (num0 != num1) {
|
is_num |= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_num) {
|
||||||
|
if (is_num == 1) {
|
||||||
|
if (*b < '0') {
|
||||||
|
result = 1;
|
||||||
|
} else {
|
||||||
|
result = -1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else if (is_num == 2) {
|
||||||
|
if (*a < '0') {
|
||||||
|
result = -1;
|
||||||
|
} else {
|
||||||
|
result = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if (parsed_number0 > parsed_number1) {
|
||||||
|
result = 1;
|
||||||
|
break;
|
||||||
|
} else if (parsed_number0 < parsed_number1) {
|
||||||
|
result = -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
a++;
|
|
||||||
b++;
|
|
||||||
}
|
}
|
||||||
}
|
/* those breaks are not set here, due to the possibillity that both numbers are equal
|
||||||
if (num0 == num1) {
|
* in which case the comparison should continue */
|
||||||
if (*a == '\0') {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
if (*b == '\0') {
|
|
||||||
b--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char c0;
|
unsigned char aa = ((*a >= 'A') && (*a <= 'Z')) ? (*a | ' ') : *a;
|
||||||
unsigned char c1;
|
unsigned char bb = ((*b >= 'A') && (*b <= 'Z')) ? (*b | ' ') : *b;
|
||||||
/* in this case we actually check for a through z as otherwise unicode characters get ordered wrongly */
|
/*using a simple aa - bb would occasionaly cause underflows with wide chars*/
|
||||||
if (*a >= 'A' && *a <= 'Z') {
|
result = ((aa == bb) ? 0 : ((aa > bb) ? 1 : -1 ));
|
||||||
c0 = (*a | ' ');
|
a++;
|
||||||
} else {
|
b++;
|
||||||
c0 = *a;
|
} while (result == 0);
|
||||||
}
|
|
||||||
if (*b >= 'A' && *b <= 'Z') {
|
return result;
|
||||||
c1 = (*b | ' ');
|
|
||||||
} else {
|
|
||||||
c1 = *b;
|
|
||||||
}
|
|
||||||
if (c0 > c1) {
|
|
||||||
return 1;
|
|
||||||
} else if (c0 < c1) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} else if (num0 > num1) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
int sort_alpha(const void *file0, const void *file1){
|
int sort_alpha(const void *file0, const void *file1){
|
||||||
char *file_name0 = ((file*)file0)->file_name;
|
char *file_name0 = ((file*)file0)->file_name;
|
||||||
|
Reference in New Issue
Block a user