Skip to content

Commit

Permalink
Merge pull request #85 from monaqa/fix-delimited_prefix_integer
Browse files Browse the repository at this point in the history
fix: make delimited + prefix integer work
  • Loading branch information
monaqa authored Nov 30, 2023
2 parents 019bbe9 + 1c73bbc commit 27eb570
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lua/dial/augend/integer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/dial/augend/integer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 27eb570

Please sign in to comment.