util.dbuffer: Fix traceback when :collapse() is called on empty buffer
authorMatthew Wild <mwild1@gmail.com>
Thu, 20 Aug 2020 15:22:19 +0100
changeset 11032 d7a403060946
parent 11031 8ed6c5bdbb21
child 11033 5550fc5e83f3
util.dbuffer: Fix traceback when :collapse() is called on empty buffer
spec/util_dbuffer_spec.lua
util/dbuffer.lua
--- a/spec/util_dbuffer_spec.lua	Sun Aug 16 20:30:02 2020 +0200
+++ b/spec/util_dbuffer_spec.lua	Thu Aug 20 15:22:19 2020 +0100
@@ -46,6 +46,13 @@
 		end);
 	end);
 
+	describe(":collapse()", function ()
+		it("works on an empty buffer", function ()
+			local b = dbuffer.new();
+			b:collapse();
+		end);
+	end);
+
 	describe(":sub", function ()
 		-- Helper function to compare buffer:sub() with string:sub()
 		local s = "hello world";
@@ -106,5 +113,10 @@
 			local r = { b:byte(1, 2) };
 			assert.same({ 0, 140 }, r);
 		end);
+
+		it("works on an empty buffer", function ()
+			local b = dbuffer.new();
+			assert.equal("", b:sub(1,1));
+		end);
 	end);
 end);
--- a/util/dbuffer.lua	Sun Aug 16 20:30:02 2020 +0200
+++ b/util/dbuffer.lua	Thu Aug 20 15:22:19 2020 +0100
@@ -142,7 +142,7 @@
 
 	local front_chunk = self.items:peek();
 
-	if #front_chunk - self.front_consumed >= bytes then
+	if not front_chunk or #front_chunk - self.front_consumed >= bytes then
 		return;
 	end