Skip to content
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
3 changes: 1 addition & 2 deletions spec/03-editline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,7 @@ describe("EditLine:", function()
end)


pending("returns an empty string for out-of-bounds indices", function()
-- TODO: fix this test
it("returns an empty string for out-of-bounds indices", function()
local line = EditLine("hello")
local exp = tostring(line):sub(10, 15)
local val = tostring(line:sub_char(10, 15))
Expand Down
17 changes: 15 additions & 2 deletions src/terminal/editline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,21 @@ end
-- @treturn EditLine A new EditLine instance containing the substring.
function EditLine:sub_char(i, j)
assert(i, "expected argument #1 to be a number")
i = utils.resolve_index(i, #self.chars, 1)
j = utils.resolve_index(j or -1, #self.chars, 1)

local len = #self.chars
j = j or -1

if i > len then
return EditLine("")
end

i = utils.resolve_index(i, len, 1)
j = utils.resolve_index(j, len, 1)

if i > j then
return EditLine("")
end

return EditLine(table.concat(self.chars, "", i, j))
end

Expand Down
Loading