From 1c73bbc20710e493cf600b288e9c1124c7e94b39 Mon Sep 17 00:00:00 2001 From: monaqa Date: Thu, 30 Nov 2023 20:53:39 +0900 Subject: [PATCH] fix: make delimited + prefix integer work --- lua/dial/augend/integer.lua | 10 +++++----- tests/dial/augend/integer_spec.lua | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lua/dial/augend/integer.lua b/lua/dial/augend/integer.lua index 1ac0192..cf0004d 100644 --- a/lua/dial/augend/integer.lua +++ b/lua/dial/augend/integer.lua @@ -48,8 +48,8 @@ function BigInt.new(n, radix) end end - if type(n) == 'string' then - if n:sub(1, 1) == '-' then + if type(n) == "string" then + if n:sub(1, 1) == "-" then self.sign = -1 n = n:sub(2) end @@ -127,8 +127,8 @@ function BigInt.new(n, radix) -- Calculate self - value local value_is_large = ( - #self.digits < #value.digits or - (#self.digits == #value.digits and self.digits[#self.digits] < value.digits[#value.digits]) + #self.digits < #value.digits + or (#self.digits == #value.digits and self.digits[#self.digits] < value.digits[#value.digits]) ) if natural and value_is_large then self.digits = { 0 } @@ -295,7 +295,7 @@ function AugendInteger:add(text, addend, cursor) if ptn == "." or ptn == "%" or ptn == "^" or ptn == "$" then ptn = "%" .. ptn end - subtext = text:gsub(ptn, "") + subtext = subtext:gsub(ptn, "") end local n = BigInt.new(subtext, self.radix) diff --git a/tests/dial/augend/integer_spec.lua b/tests/dial/augend/integer_spec.lua index b8ec8fb..2158d9b 100644 --- a/tests/dial/augend/integer_spec.lua +++ b/tests/dial/augend/integer_spec.lua @@ -199,3 +199,24 @@ describe([[Test of integer.new {delimiter = ",", delimiter_digits = 4}:]], funct end) end) end) +describe([[Test of delimited binary number:]], function() + local augend = integer.new { + radix = 2, + prefix = "0b", + delimiter = "_", + delimiter_digits = 4, + } + + describe("find function", function() + it("can find comma-separated integer", function() + assert.are.same(augend:find("0b1010_0101", 1), { from = 1, to = 11 }) + end) + end) + + describe("add function", function() + it("separates numbers by four digits", function() + assert.are.same(augend:add("0b1010_0101", 1, 1), { text = "0b1010_0110", cursor = 11 }) + assert.are.same(augend:add("0b1010_1111", 1, 1), { text = "0b1011_0000", cursor = 11 }) + end) + end) +end)