From 4938e4cf1c728e0db2c0acc811a1b7967f158e08 Mon Sep 17 00:00:00 2001 From: anon Date: Sat, 8 Feb 2025 11:33:19 +0100 Subject: [PATCH] fix `-` ordering oversight --- documentation/TODO.md | 2 +- source/directive.c | 17 ++++++++++++- test/CMDTEST_vimdir.rb | 29 ++++++++++++++++++++++ test/myunfortunateprefixdir/prefix-postfix | 0 test/myunfortunateprefixdir/prefix/postfix | 0 test/myunfortunateprefixdir/prefixPostfix | 0 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/myunfortunateprefixdir/prefix-postfix create mode 100644 test/myunfortunateprefixdir/prefix/postfix create mode 100644 test/myunfortunateprefixdir/prefixPostfix diff --git a/documentation/TODO.md b/documentation/TODO.md index a80190b..f17a9f1 100644 --- a/documentation/TODO.md +++ b/documentation/TODO.md @@ -14,7 +14,7 @@ - [X] use `${VIMDIREDITOR}` - [X] dry and moist tests - [X] remove tempfile -- [ ] the ordering can be wrong if directories share the same prefix +- [X] path ordering that accounts for `-` (recursion; common prefix) ## Ideas Accept multiple folders and individual files. diff --git a/source/directive.c b/source/directive.c index 1fffa0e..dfb4f81 100644 --- a/source/directive.c +++ b/source/directive.c @@ -21,11 +21,26 @@ typedef struct { bool is_mentioned; } entry_t; +static inline +int path_cmp(const char * a, const char * b) { + while (*a == *b) { + if (!(*a)) { goto end; } + ++a; + ++b; + } + + if (*a == '/' && *b != '\0') { return -1; } + if (*b == '/' && *a != '\0') { return 1; } + + end: + return *(unsigned char *)a - *(unsigned char *)b; +} + static int entry_cmp(const void * a, const void * b) { // For qsort() const entry_t * const A = a; const entry_t * const B = b; - return strcmp(A->name, B->name); + return path_cmp(A->name, B->name); } static kvec_t(entry_t) entries; diff --git a/test/CMDTEST_vimdir.rb b/test/CMDTEST_vimdir.rb index 2739a31..3460e71 100644 --- a/test/CMDTEST_vimdir.rb +++ b/test/CMDTEST_vimdir.rb @@ -520,3 +520,32 @@ class CMDTEST_myswapdir < Cmdtest::Testcase end end end + + + +# ___ __ _ _ _ +# | _ \_ _ ___ / _(_)_ ____| (_)_ _ +# | _/ '_/ -_) _| \ \ / _` | | '_| +# |_| |_| \___|_| |_/_\_\__,_|_|_| +# +class CMDTEST_myswapdir < Cmdtest::Testcase + def setup + import_file "test/saver.sh", "./" + import_directory "test/myunfortunateprefixdir/", "./myprefixdir/" + end + + def test_prefix_order + expected = [ + "000\t./myprefixdir/prefix/", + "001\t./myprefixdir/prefix/postfix", + "002\t./myprefixdir/prefix-postfix", + "003\t./myprefixdir/prefixPostfix", + ] + + cmd "EDITOR=./saver.sh vimdir -n -r ./myprefixdir/" do + exit_zero + created_files ["output.txt"] + file_equal "output.txt", expected + end + end +end diff --git a/test/myunfortunateprefixdir/prefix-postfix b/test/myunfortunateprefixdir/prefix-postfix new file mode 100644 index 0000000..e69de29 diff --git a/test/myunfortunateprefixdir/prefix/postfix b/test/myunfortunateprefixdir/prefix/postfix new file mode 100644 index 0000000..e69de29 diff --git a/test/myunfortunateprefixdir/prefixPostfix b/test/myunfortunateprefixdir/prefixPostfix new file mode 100644 index 0000000..e69de29