perf(utils): linear scan is_uri (#2648)
This commit is contained in:
@@ -173,12 +173,32 @@ utils.is_path_hidden = function(opts, path_display)
|
|||||||
or type(path_display) == "table" and (vim.tbl_contains(path_display, "hidden") or path_display.hidden)
|
or type(path_display) == "table" and (vim.tbl_contains(path_display, "hidden") or path_display.hidden)
|
||||||
end
|
end
|
||||||
|
|
||||||
local URI_SCHEME_PATTERN = "^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*"
|
|
||||||
local WINDOWS_ROOT_PATTERN = "^[a-zA-Z]:\\"
|
|
||||||
utils.is_uri = function(filename)
|
utils.is_uri = function(filename)
|
||||||
local is_uri_match = filename:match(URI_SCHEME_PATTERN) ~= nil
|
local char = string.byte(filename, 1)
|
||||||
local is_windows_root_match = filename:match(WINDOWS_ROOT_PATTERN)
|
|
||||||
return is_uri_match and not is_windows_root_match
|
-- is alpha?
|
||||||
|
if char < 65 or (char > 90 and char < 97) or char > 122 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 2, #filename do
|
||||||
|
char = string.byte(filename, i)
|
||||||
|
if char == 58 then -- `:`
|
||||||
|
return i < #filename and string.byte(filename, i + 1) ~= 92 -- `\`
|
||||||
|
elseif
|
||||||
|
not (
|
||||||
|
(char >= 48 and char <= 57) -- 0-9
|
||||||
|
or (char >= 65 and char <= 90) -- A-Z
|
||||||
|
or (char >= 97 and char <= 122) -- a-z
|
||||||
|
or char == 43 -- `+`
|
||||||
|
or char == 46 -- `.`
|
||||||
|
or char == 45 -- `-`
|
||||||
|
)
|
||||||
|
then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local calc_result_length = function(truncate_len)
|
local calc_result_length = function(truncate_len)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local utils = require "telescope.utils"
|
local utils = require "telescope.utils"
|
||||||
|
|
||||||
describe("is_uri", function()
|
describe("is_uri", function()
|
||||||
it("detects valid uris", function()
|
describe("detects valid uris", function()
|
||||||
local uris = {
|
local uris = {
|
||||||
[[https://www.example.com/index.html]],
|
[[https://www.example.com/index.html]],
|
||||||
[[ftp://ftp.example.com/files/document.pdf]],
|
[[ftp://ftp.example.com/files/document.pdf]],
|
||||||
@@ -16,40 +16,64 @@ describe("is_uri", function()
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, uri in ipairs(uris) do
|
for _, uri in ipairs(uris) do
|
||||||
|
it(uri, function()
|
||||||
assert.True(utils.is_uri(uri))
|
assert.True(utils.is_uri(uri))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles windows paths", function()
|
describe("detects invalid uris/paths", function()
|
||||||
|
local inputs = {
|
||||||
|
"hello",
|
||||||
|
"hello:",
|
||||||
|
"123",
|
||||||
|
}
|
||||||
|
for _, input in ipairs(inputs) do
|
||||||
|
it(input, function()
|
||||||
|
assert.False(utils.is_uri(input))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("handles windows paths", function()
|
||||||
local paths = {
|
local paths = {
|
||||||
[[C:\Users\Usuario\Documents\archivo.txt]],
|
[[C:\Users\Usuario\Documents\archivo.txt]],
|
||||||
[[D:\Projects\project_folder\source_code.py]],
|
[[D:\Projects\project_folder\source_code.py]],
|
||||||
[[E:\Music\song.mp3]],
|
[[E:\Music\song.mp3]],
|
||||||
}
|
}
|
||||||
for _, path in ipairs(paths) do
|
|
||||||
assert.False(utils.is_uri(path))
|
for _, uri in ipairs(paths) do
|
||||||
|
it(uri, function()
|
||||||
|
assert.False(utils.is_uri(uri))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles linux paths", function()
|
describe("handles linux paths", function()
|
||||||
local paths = {
|
local paths = {
|
||||||
[[/home/usuario/documents/archivo.txt]],
|
[[/home/usuario/documents/archivo.txt]],
|
||||||
[[/var/www/html/index.html]],
|
[[/var/www/html/index.html]],
|
||||||
[[/mnt/backup/backup_file.tar.gz]],
|
[[/mnt/backup/backup_file.tar.gz]],
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path in ipairs(paths) do
|
for _, path in ipairs(paths) do
|
||||||
|
it(path, function()
|
||||||
assert.False(utils.is_uri(path))
|
assert.False(utils.is_uri(path))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("handles macos paths", function()
|
describe("handles macos paths", function()
|
||||||
local paths = {
|
local paths = {
|
||||||
[[/Users/Usuario/Documents/archivo.txt]],
|
[[/Users/Usuario/Documents/archivo.txt]],
|
||||||
[[/Applications/App.app/Contents/MacOS/app_executable]],
|
[[/Applications/App.app/Contents/MacOS/app_executable]],
|
||||||
[[/Volumes/ExternalDrive/Data/file.xlsx]],
|
[[/Volumes/ExternalDrive/Data/file.xlsx]],
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path in ipairs(paths) do
|
for _, path in ipairs(paths) do
|
||||||
|
it(path, function()
|
||||||
assert.False(utils.is_uri(path))
|
assert.False(utils.is_uri(path))
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user