diff --git a/spec/03-editline_spec.lua b/spec/03-editline_spec.lua index bec20d6e..fe8f80e9 100644 --- a/spec/03-editline_spec.lua +++ b/spec/03-editline_spec.lua @@ -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)) diff --git a/src/terminal/editline.lua b/src/terminal/editline.lua index 7bd7327c..dd92c732 100644 --- a/src/terminal/editline.lua +++ b/src/terminal/editline.lua @@ -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