From 3ef5a2b1a9242bba0dd6bb5cee32e7a2f0180706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9opold=20Hubert?= Date: Fri, 25 Apr 2025 11:01:27 +0200 Subject: [PATCH] Adding the `__concat` metamethod for easy string concatenation --- bint.lua | 6 ++++++ tests.lua | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/bint.lua b/bint.lua index 72ce0ef..33d39e3 100644 --- a/bint.lua +++ b/bint.lua @@ -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) diff --git a/tests.lua b/tests.lua index 4449d9d..688889e 100644 --- a/tests.lua +++ b/tests.lua @@ -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))