summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-05-04 09:06:03 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2024-05-04 09:06:03 +0200
commitb41df538c72c7e9a26f9ff08f2668769c8d1a1d1 (patch)
tree59820cae6b9edb014520487f5d2b0c9f91bc6b17
parentfdf62fe6c4125d29b3097e49566b2bbe1f650382 (diff)
downloadluasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.zip
switch "has" to "has_all_of" and "has_any_of"
-rw-r--r--spec/05-bitflags_spec.lua23
-rw-r--r--src/bitflags.c58
-rw-r--r--src/term.c6
-rw-r--r--system/init.lua4
4 files changed, 64 insertions, 27 deletions
diff --git a/spec/05-bitflags_spec.lua b/spec/05-bitflags_spec.lua
index 8024245..8eea27f 100644
--- a/spec/05-bitflags_spec.lua
+++ b/spec/05-bitflags_spec.lua
@@ -87,15 +87,28 @@ describe("BitFlags library", function()
assert.has_error(function() bf.not_a_number = true end, "index must be a number")
end)
- it("checks for a subset using 'has'", function()
+ it("checks for a subset using 'has_all_of'", function()
local bf1 = sys.bitflag(3) -- b0011
local bf2 = sys.bitflag(3) -- b0011
local bf3 = sys.bitflag(15) -- b1111
local bf0 = sys.bitflag(0) -- b0000
- assert.is_true(bf1:has(bf2)) -- equal
- assert.is_true(bf3:has(bf1)) -- is a subset, and has more flags
- assert.is_false(bf1:has(bf3)) -- not a subset, bf3 has more flags
- assert.is_false(bf1:has(bf0)) -- bf0 is unset, always returns false
+ assert.is_true(bf1:has_all_of(bf2)) -- equal
+ assert.is_true(bf3:has_all_of(bf1)) -- is a subset, and has more flags
+ assert.is_false(bf1:has_all_of(bf3)) -- not a subset, bf3 has more flags
+ assert.is_false(bf1:has_all_of(bf0)) -- bf0 is unset, always returns false
+ end)
+
+ it("checks for a subset using 'has_any_of'", function()
+ local bf1 = sys.bitflag(3) -- b0011
+ local bf2 = sys.bitflag(3) -- b0011
+ local bf3 = sys.bitflag(7) -- b0111
+ local bf4 = sys.bitflag(8) -- b1000
+ local bf0 = sys.bitflag(0) -- b0000
+ assert.is_true(bf1:has_any_of(bf2)) -- equal
+ assert.is_true(bf3:has_any_of(bf1)) -- is a subset, and has more flags
+ assert.is_false(bf3:has_any_of(bf4)) -- no overlap in flags
+ assert.is_true(bf1:has_any_of(bf3)) -- not a subset, bf3 has more flags but still some overlap
+ assert.is_false(bf1:has_all_of(bf0)) -- bf0 is unset, always returns false
end)
end)
diff --git a/src/bitflags.c b/src/bitflags.c
index d90ad1d..39f8af0 100644
--- a/src/bitflags.c
+++ b/src/bitflags.c
@@ -82,11 +82,12 @@ local flags6 = sys.bitflag(7) -- b0111
print(flags6 == flags4) -- true, same flags
-- comparison of subsets
-local flags7 = sys.bitflag(0) -- b0000
-local flags8 = sys.bitflag(3) -- b0011
-local flags9 = sys.bitflag(7) -- b0111
-print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9
-print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8
+local flags7 = sys.bitflag(0) -- b0000
+local flags8 = sys.bitflag(3) -- b0011
+local flags9 = sys.bitflag(7) -- b0111
+print(flags9:has_all_of(flags8)) -- true, flags8 bits are all set in flags9
+print(flags8:has_any_of(flags9)) -- true, some of flags9 bits are set in flags8
+print(flags8:has_all_of(flags7)) -- false, flags7 (== 0) is not set in flags8
*/
static int lsbf_new(lua_State *L) {
LSBF_BITFLAG flags = 0;
@@ -134,26 +135,48 @@ static int lsbf_eq(lua_State *L) {
}
/***
-Checks if the given flags are set.
-This is different from the `>=` and `<=` operators because if the flag to check
-has a value `0`, it will always return `false`. So if there are flags that are
-unsupported on a platform, they can be set to 0 and the `has` function will
+Checks if all the flags in the given subset are set.
+If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
+unsupported on a platform, they can be set to 0 and the `has_all_of` function will
return `false` if the flags are checked.
-@function bitflag:has
+@function bitflag:has_all_of
@tparam bitflag subset the flags to check for.
@treturn boolean true if all the flags are set, false otherwise.
@usage
local sys = require 'system'
-local flags = sys.bitflag(12) -- b1100
-local myflags = sys.bitflag(15) -- b1111
-print(flags:has(myflags)) -- false, not all bits in myflags are set in flags
-print(myflags:has(flags)) -- true, all bits in flags are set in myflags
+local flags = sys.bitflag(12) -- b1100
+local myflags = sys.bitflag(15) -- b1111
+print(flags:has_all_of(myflags)) -- false, not all bits in myflags are set in flags
+print(myflags:has_all_of(flags)) -- true, all bits in flags are set in myflags
*/
-static int lsbf_has(lua_State *L) {
+static int lsbf_has_all_of(lua_State *L) {
LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
// Check if all bits in b are also set in a, and b is not 0
- lua_pushboolean(L, (a | b) == a && b != 0);
+ lua_pushboolean(L, (a & b) == b && b != 0);
+ return 1;
+}
+
+/***
+Checks if any of the flags in the given subset are set.
+If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
+unsupported on a platform, they can be set to 0 and the `has_any_of` function will
+return `false` if the flags are checked.
+@function bitflag:has_any_of
+@tparam bitflag subset the flags to check for.
+@treturn boolean true if any of the flags are set, false otherwise.
+@usage
+local sys = require 'system'
+local flags = sys.bitflag(12) -- b1100
+local myflags = sys.bitflag(7) -- b0111
+print(flags:has_any_of(myflags)) -- true, some bits in myflags are set in flags
+print(myflags:has_any_of(flags)) -- true, some bits in flags are set in myflags
+*/
+static int lsbf_has_any_of(lua_State *L) {
+ LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
+ LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
+ // Check if any bits in b are set in a
+ lua_pushboolean(L, (a & b) != 0);
return 1;
}
@@ -201,7 +224,8 @@ static const struct luaL_Reg lsbf_funcs[] = {
static const struct luaL_Reg lsbf_methods[] = {
{"value", lsbf_value},
- {"has", lsbf_has},
+ {"has_all_of", lsbf_has_all_of},
+ {"has_any_of", lsbf_has_any_of},
{"__tostring", lsbf_tostring},
{"__add", lsbf_add},
{"__sub", lsbf_sub},
diff --git a/src/term.c b/src/term.c
index 062394d..9a9967d 100644
--- a/src/term.c
+++ b/src/term.c
@@ -386,7 +386,7 @@ local system = require('system')
local flags = system.getconsoleflags(io.stdout)
print("Current stdout flags:", tostring(flags))
-if flags:has(system.COF_VIRTUAL_TERMINAL_PROCESSING + system.COF_PROCESSED_OUTPUT) then
+if flags:has_all_of(system.COF_VIRTUAL_TERMINAL_PROCESSING + system.COF_PROCESSED_OUTPUT) then
print("Both flags are set")
else
print("At least one flag is not set")
@@ -445,7 +445,7 @@ The terminal attributes is a table with the following fields:
local system = require('system')
local status = assert(tcgetattr(io.stdin))
-if status.iflag:has(system.I_IGNBRK) then
+if status.iflag:has_all_of(system.I_IGNBRK) then
print("Ignoring break condition")
end
*/
@@ -539,7 +539,7 @@ _Note_: only `iflag`, `oflag`, and `lflag` are supported at the moment. The othe
local system = require('system')
local status = assert(tcgetattr(io.stdin))
-if not status.lflag:has(system.L_ECHO) then
+if not status.lflag:has_all_of(system.L_ECHO) then
-- if echo is off, turn echoing newlines on
tcsetattr(io.stdin, system.TCSANOW, { lflag = status.lflag + system.L_ECHONL }))
end
diff --git a/system/init.lua b/system/init.lua
index c3ea94d..93dd488 100644
--- a/system/init.lua
+++ b/system/init.lua
@@ -154,7 +154,7 @@ function sys.listconsoleflags(fh)
local out = {}
for k,v in pairs(sys) do
if type(k) == "string" and k:sub(1,4) == flagtype then
- if flags:has(v) then
+ if flags:has_all_of(v) then
out[#out+1] = string.format("%10d [x] %s",v:value(),k)
else
out[#out+1] = string.format("%10d [ ] %s",v:value(),k)
@@ -191,7 +191,7 @@ function sys.listtermflags(fh)
local out = {}
for k,v in pairs(sys) do
if type(k) == "string" and k:sub(1,2) == prefix then
- if flags[flagtype]:has(v) then
+ if flags[flagtype]:has_all_of(v) then
out[#out+1] = string.format("%10d [x] %s",v:value(),k)
else
out[#out+1] = string.format("%10d [ ] %s",v:value(),k)