Skip to content

Adding the __concat metamethod for easy string concatenation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions bint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,12 @@ function bint:__tostring()
return self:tobase(10)
end

--- Convert a bint to a string when concatenating it to an other string.
-- @see bint.tostring
function bint.__concat(a, b)
return tostring(a) .. tostring(b)
end

-- Allow creating bints by calling bint itself
setmetatable(bint, {
__call = function(_, x)
Expand Down
4 changes: 4 additions & 0 deletions tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ local function test(bits, wordbits)
assert(bint.frombase('AAAAAAAAAAAAAAAAAAA') == nil)
assert(bint.frombase('AAAAAAAAAAAAAAAAAAA_') == nil)

assert(bint(9592924) .. '_MyString' == '9592924_MyString')
assert('MyString_' .. bint(9592924) == 'MyString_9592924')
assert('MyString_' .. bint(9592924) .. "_MyString" == 'MyString_9592924_MyString')

assert(bint.fromstring(1) == nil)
assert(bint.eq(bint.fromstring('0xff'), 0xff))
assert(bint.eq(bint.fromstring('+0xff'), 0xff))
Expand Down