public inbox for patches@ignore.pl
 help / color / mirror / Atom feed
From: Jakub Slepecki <jakub.slepecki@intel.com>
To: patches@ignore.pl
Cc: please@ignore.pl, Jakub Slepecki <jakub.slepecki@intel.com>
Subject: [PATCH what 1/3] what: add bits.b() for binary number "literals"
Date: Tue, 24 Feb 2026 09:56:32 +0100	[thread overview]
Message-ID: <20260224085635.319107-2-jakub.slepecki@intel.com> (raw)
In-Reply-To: <20260224085635.319107-1-jakub.slepecki@intel.com>

Good chunk of documentation uses the "1001b" notation.  Lua does not
support it, but we can leverage function calls syntax sugar to be somewhat
similar to it.

Signed-off-by: Jakub Slepecki <jakub.slepecki@intel.com>
---
 spec/bits_spec.lua | 25 +++++++++++++++++++++++++
 what/bits.lua      | 20 ++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 spec/bits_spec.lua

diff --git a/spec/bits_spec.lua b/spec/bits_spec.lua
new file mode 100644
index 000000000000..931ce32048d7
--- /dev/null
+++ b/spec/bits_spec.lua
@@ -0,0 +1,25 @@
+local bits = require"what.bits"
+local b = bits.b
+
+
+describe("bits.b()", function()
+	describe("builds integers from", function()
+		it("some examples", function()
+			assert.are.equal(1, b"1")
+			assert.are.equal(3, b"11")
+			assert.are.equal(5, b"101")
+			assert.are.equal(15, b"1111")
+			assert.are.equal(0, b"0000")
+			assert.are.equal(37, b"100101")
+		end)
+	end)
+
+	it("allows an empty string", function()
+		assert.are.equal(0, b"")
+	end)
+
+	it("rejects malformed strings", function()
+		assert.is_nil(b"abc")
+		assert.is_nil(b"10a1")
+	end)
+end)
diff --git a/what/bits.lua b/what/bits.lua
index c11571a725bd..440d5bacd36a 100644
--- a/what/bits.lua
+++ b/what/bits.lua
@@ -131,4 +131,24 @@ function bits.bit (nth)
 end
 
 
+--- Build an integer based on a string containing a [[binary]]
+--- representation of it, for example:
+---
+---     bits.b"011"  --> 3
+---     bits.b"101"  --> 5
+function bits.b (binary)
+	if not binary:match"^[01]*$" then
+		return nil
+	end
+	local len = #binary
+	local val = 0
+	local pos = binary:find"1"
+	while pos do
+		val = val + 2 ^ (len - pos)
+		pos = binary:find("1", pos + 1)
+	end
+	return math.tointeger(val)
+end
+
+
 return bits
-- 
2.43.0


  reply	other threads:[~2026-02-24  8:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-24  8:56 [PATCH what 0/3] ice: add LLDP filter control 0x0A0A Jakub Slepecki
2026-02-24  8:56 ` Jakub Slepecki [this message]
2026-02-24  8:56 ` [PATCH what 2/3] " Jakub Slepecki
2026-02-24  8:56 ` [PATCH what 3/3] scripts: add ID=ubuntu to scripts/install Jakub Slepecki
2026-02-24  9:08 ` [PATCH what 0/3] ice: add LLDP filter control 0x0A0A Aki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260224085635.319107-2-jakub.slepecki@intel.com \
    --to=jakub.slepecki@intel.com \
    --cc=patches@ignore.pl \
    --cc=please@ignore.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox