Skip to content

Commit

Permalink
perf(utils): linear scan is_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestrew committed Aug 26, 2023
1 parent 2d92125 commit cc9193e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
28 changes: 23 additions & 5 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,30 @@ utils.is_path_hidden = function(opts, path_display)
or type(path_display) == "table" and (vim.tbl_contains(path_display, "hidden") or path_display.hidden)
end

local URI_SCHEME_PATTERN = "^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*"
local WINDOWS_ROOT_PATTERN = "^[a-zA-Z]:\\"
utils.is_uri = function(filename)
local is_uri_match = filename:match(URI_SCHEME_PATTERN) ~= nil
local is_windows_root_match = filename:match(WINDOWS_ROOT_PATTERN)
return is_uri_match and not is_windows_root_match
local char = string.byte(filename, 1)
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 string.byte(filename, i + 1) ~= 92
elseif
not (
(char >= 48 and char <= 57)
or (char >= 65 and char <= 90)
or (char >= 97 and char <= 122)
or char == 43 -- `+`
or char == 46 -- `.`
or char == 45 -- `-`
)
then
return false
end
end
return false
end

local calc_result_length = function(truncate_len)
Expand Down
33 changes: 24 additions & 9 deletions lua/tests/automated/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local utils = require "telescope.utils"

describe("is_uri", function()
it("detects valid uris", function()
describe("detects valid uris", function()
local uris = {
[[https://www.example.com/index.html]],
[[ftp://ftp.example.com/files/document.pdf]],
Expand All @@ -16,40 +16,55 @@ describe("is_uri", function()
}

for _, uri in ipairs(uris) do
assert.True(utils.is_uri(uri))
it(uri, function()
assert.True(utils.is_uri(uri))
end)
end
end)

it("handles windows paths", function()
it("detects invalid uris", function()
assert.False(utils.is_uri "hello")
end)

describe("handles windows", function()
local paths = {
[[C:\Users\Usuario\Documents\archivo.txt]],
[[D:\Projects\project_folder\source_code.py]],
[[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)

it("handles linux paths", function()
describe("handles linux paths", function()
local paths = {
[[/home/usuario/documents/archivo.txt]],
[[/var/www/html/index.html]],
[[/mnt/backup/backup_file.tar.gz]],
}

for _, path in ipairs(paths) do
assert.False(utils.is_uri(path))
it(path, function()
assert.False(utils.is_uri(path))
end)
end
end)

it("handles macos paths", function()
describe("handles macos paths", function()
local paths = {
[[/Users/Usuario/Documents/archivo.txt]],
[[/Applications/App.app/Contents/MacOS/app_executable]],
[[/Volumes/ExternalDrive/Data/file.xlsx]],
}

for _, path in ipairs(paths) do
assert.False(utils.is_uri(path))
it(path, function()
assert.False(utils.is_uri(path))
end)
end
end)
end)

0 comments on commit cc9193e

Please sign in to comment.