From b41df538c72c7e9a26f9ff08f2668769c8d1a1d1 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Sat, 4 May 2024 09:06:03 +0200 Subject: switch "has" to "has_all_of" and "has_any_of" --- src/bitflags.c | 58 +++++++++++++++++++++++++++++++++++++++++----------------- src/term.c | 6 +++--- 2 files changed, 44 insertions(+), 20 deletions(-) (limited to 'src') 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 -- cgit v1.2.3-55-g6feb