refactor(path): pathcmp() #41035

Problem:
Redudant code

Solution:
Add `path_fold_char()` to normalize path sep.
Refactor `pathcmp()` and `path_fnamencmp()` into `path_cmp`.

| feature                  | pathcmp        | path_fnamencmp | path_cmp(now) |
| ------------------------ | -------------- | -------------- | ------------- |
| case folding             | `mb_toupper()` | `utf_fold()`   | `utf_fold()`  |
| consults fileignorecase  |              |              | `ic` param    |
| `maxlen`                 |              |              |             |
| `/` == `\`               |              |              |             |
| MSWIN drive letter       |              |              |             |
| sep affects sorting      |              |              |             |
| ignores a trailing slash |              |              |             |

Refactor `path_fnamecmp` and `path_full_compare` into `path_equal`,
with flags controlling "no filesystem" comparison (i.e. `path_cmp`), env
variables expansion, absolute path resolution and filesystem access.
This commit is contained in:
tao
2026-08-08 07:54:29 +08:00
committed by GitHub
parent b53c00b425
commit f0a0182285
31 changed files with 306 additions and 324 deletions

View File

@@ -205,6 +205,18 @@ describe('fnamemodify()', function()
eq('txt', fnamemodify('path/to/hello.txt', ':e'))
end)
it(':~', function()
local cwd = vim.fs.normalize(fnamemodify('.', ':p'))
local full_path = ('%s/foo'):format(cwd)
command(([[let $HOME='%s']]):format(cwd))
eq('~/foo', fnamemodify(full_path, ':~'))
command(([[let $HOME='%s/']]):format(cwd)) -- a trailing slash
-- `init_home` calls `os_realpath` on Unix, which removes the trailing slash
eq(is_os('win') and full_path or '~/foo', fnamemodify(full_path, ':~'))
end)
it('regex replacements', function()
eq('content-there-here.txt', fnamemodify('content-here-here.txt', ':s/here/there/'))
eq('content-there-there.txt', fnamemodify('content-here-here.txt', ':gs/here/there/'))