Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(utils): linear scan is_uri #2648

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
perf(utils): linear scan is_uri
  • Loading branch information
jamestrew committed Aug 28, 2023
commit a2d76039f73de58495bca056b13ef7a380802f71
30 changes: 25 additions & 5 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)

-- 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

local calc_result_length = function(truncate_len)
Expand Down
42 changes: 33 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:https://ftp.example.com/files/document.pdf]],
Expand All @@ -16,40 +16,64 @@ 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()
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 = {
[[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)