Fix "\todo Add a flag to ignore case" from Fl_Browser_.cxx

This commit is contained in:
ManoloFLTK 2023-02-06 15:26:53 +01:00
parent 587687629e
commit dc51880e28
2 changed files with 6 additions and 3 deletions

View File

@ -35,6 +35,7 @@
#define FL_SORT_ASCENDING 0 /**< sort browser items in ascending alphabetic order. */ #define FL_SORT_ASCENDING 0 /**< sort browser items in ascending alphabetic order. */
#define FL_SORT_DESCENDING 1 /**< sort in descending order */ #define FL_SORT_DESCENDING 1 /**< sort in descending order */
#define FL_SORT_CASEINSENSITIVE 0x2 /**< sort case insensitively */
/** /**
This is the base class for browsers. To be useful it must be This is the base class for browsers. To be useful it must be

View File

@ -984,15 +984,17 @@ Fl_Browser_::Fl_Browser_(int X, int Y, int W, int H, const char* L)
item_swap(void*, void*) and item_text(void*) must be implemented for this call. item_swap(void*, void*) and item_text(void*) must be implemented for this call.
\param[in] flags FL_SORT_ASCENDING -- sort in ascending order\n \param[in] flags FL_SORT_ASCENDING -- sort in ascending order\n
FL_SORT_DESCENDING -- sort in descending order\n FL_SORT_DESCENDING -- sort in descending order\n
FL_SORT_CASEINSENSITIVE -- add this to sort case-insensitively\n
Values other than the above will cause undefined behavior\n Values other than the above will cause undefined behavior\n
Other flags may appear in the future. Other flags may appear in the future.
\todo Add a flag to ignore case
*/ */
void Fl_Browser_::sort(int flags) { void Fl_Browser_::sort(int flags) {
// //
// Simple bubble sort - pure lazyness on my side. // Simple bubble sort - pure lazyness on my side.
// //
int i, j, n = -1, desc = ((flags&FL_SORT_DESCENDING)==FL_SORT_DESCENDING); int i, j, n = -1, desc = ((flags&FL_SORT_DESCENDING)==FL_SORT_DESCENDING);
typedef int (*sort_f_type)(const char *, const char *);
sort_f_type sort_f = ( (flags&FL_SORT_CASEINSENSITIVE) ? strcasecmp : strcmp );
void *a =item_first(), *b, *c; void *a =item_first(), *b, *c;
if (!a) return; if (!a) return;
while (a) { while (a) {
@ -1008,12 +1010,12 @@ void Fl_Browser_::sort(int flags) {
const char *tb = item_text(b); const char *tb = item_text(b);
c = item_next(b); c = item_next(b);
if (desc) { if (desc) {
if (strcmp(ta, tb)<0) { if (sort_f(ta, tb) < 0) {
item_swap(a, b); item_swap(a, b);
swapped = 1; swapped = 1;
} }
} else { } else {
if (strcmp(ta, tb)>0) { if (sort_f(ta, tb)>0) {
item_swap(a, b); item_swap(a, b);
swapped = 1; swapped = 1;
} }