diff options
author | dm1try <me@dmitry.it> | 2020-05-03 23:46:55 +0300 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2020-12-01 10:50:38 +0100 |
commit | 13b88573005d84cc0ebcd7e7bf4dd488673919d3 (patch) | |
tree | 0ac5a5e231ecbb45fb15817f5dbec6296a092669 | |
parent | 70d0bee7655d70e4417143e45ec7b0cf3f92d9d3 (diff) | |
download | rneovim-13b88573005d84cc0ebcd7e7bf4dd488673919d3.tar.gz rneovim-13b88573005d84cc0ebcd7e7bf4dd488673919d3.tar.bz2 rneovim-13b88573005d84cc0ebcd7e7bf4dd488673919d3.zip |
path: add helper for checking a file extension
-rw-r--r-- | src/nvim/path.c | 7 | ||||
-rw-r--r-- | test/unit/path_spec.lua | 17 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/nvim/path.c b/src/nvim/path.c index f52fbbd5c8..2de7e00ddb 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -1704,6 +1704,13 @@ int path_with_url(const char *fname) return path_is_url(p); } +bool path_with_extension(const char *path, const char *extension) +{ + const char *last_dot = strrchr(path, '.'); + if (!last_dot) { return false; } + return strcmp(last_dot + 1, extension) == 0; +} + /* * Return TRUE if "name" is a full (absolute) path name or URL. */ diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index 356c4997fa..41954de9be 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -603,4 +603,21 @@ describe('path.c', function() eq(FAIL, path_is_absolute('not/in/my/home~/directory')) end) end) + + describe('path_with_extension', function() + local function path_with_extension(filename, extension) + local c_filename = to_cstr(filename) + local c_extension = to_cstr(extension) + return cimp.path_with_extension(c_filename, c_extension) + end + + itp('returns true if filename includes a provided extension', function() + eq(true, path_with_extension('/some/path/file.lua', 'lua')) + end) + + itp('returns false if filename does not include a provided extension', function() + eq(false, path_with_extension('/some/path/file.vim', 'lua')) + eq(false, path_with_extension('/some/path/file', 'lua')) + end) + end) end) |