Skip to content

Commit 3ef5a2b

Browse files
author
Léopold Hubert
committed
Adding the __concat metamethod for easy string concatenation
1 parent 08ca8ee commit 3ef5a2b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

bint.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,12 @@ function bint:__tostring()
17251725
return self:tobase(10)
17261726
end
17271727

1728+
--- Convert a bint to a string when concatenating it to an other string.
1729+
-- @see bint.tostring
1730+
function bint.__concat(a, b)
1731+
return tostring(a) .. tostring(b)
1732+
end
1733+
17281734
-- Allow creating bints by calling bint itself
17291735
setmetatable(bint, {
17301736
__call = function(_, x)

tests.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ local function test(bits, wordbits)
105105
assert(bint.frombase('AAAAAAAAAAAAAAAAAAA') == nil)
106106
assert(bint.frombase('AAAAAAAAAAAAAAAAAAA_') == nil)
107107

108+
assert(bint(9592924) .. '_MyString' == '9592924_MyString')
109+
assert('MyString_' .. bint(9592924) == 'MyString_9592924')
110+
assert('MyString_' .. bint(9592924) .. "_MyString" == 'MyString_9592924_MyString')
111+
108112
assert(bint.fromstring(1) == nil)
109113
assert(bint.eq(bint.fromstring('0xff'), 0xff))
110114
assert(bint.eq(bint.fromstring('+0xff'), 0xff))

0 commit comments

Comments
 (0)