From 6c748439ed0274d844c7634f0d60f872e3008630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sun, 20 Feb 2022 10:50:08 +0100 Subject: [PATCH] Use natural sort order This is exposed in GLib through collate key construction. --- fiv-io.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/fiv-io.c b/fiv-io.c index aa053e8..1778120 100644 --- a/fiv-io.c +++ b/fiv-io.c @@ -2711,6 +2711,7 @@ fiv_io_open_from_data( typedef struct _ModelEntry { gchar *uri; ///< GIO URI + gchar *collate_key; ///< Collate key for the filename gint64 mtime_msec; ///< Modification time in milliseconds } ModelEntry; @@ -2718,6 +2719,7 @@ static void model_entry_finalize(ModelEntry *entry) { g_free(entry->uri); + g_free(entry->collate_key); } struct _FivIoModel { @@ -2778,17 +2780,6 @@ model_supports(FivIoModel *self, const gchar *filename) return FALSE; } -static inline int -model_compare_name(GFile *location1, GFile *location2) -{ - gchar *name1 = g_file_get_parse_name(location1); - gchar *name2 = g_file_get_parse_name(location2); - int result = g_utf8_collate(name1, name2); - g_free(name1); - g_free(name2); - return result; -} - static inline int model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1, const ModelEntry *entry2, GFile *file2) @@ -2809,7 +2800,7 @@ model_compare_entries(FivIoModel *self, const ModelEntry *entry1, GFile *file1, // Fall-through case FIV_IO_MODEL_SORT_NAME: case FIV_IO_MODEL_SORT_COUNT: - result = model_compare_name(file1, file2); + result = strcmp(entry1->collate_key, entry2->collate_key); } return self->sort_descending ? -result : +result; } @@ -2870,6 +2861,12 @@ model_reload(FivIoModel *self, GError **error) g_date_time_unref(mtime); } + gchar *parse_name = g_file_get_parse_name(child); + // TODO(p): Make it possible to use g_utf8_collate_key() instead, + // which does not use natural sorting. + entry.collate_key = g_utf8_collate_key_for_filename(parse_name, -1); + g_free(parse_name); + const char *name = g_file_info_get_name(info); if (g_file_info_get_file_type(info) == G_FILE_TYPE_DIRECTORY) { entry.uri = g_file_get_uri(child);