aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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()
87 assert.has_error(function() bf.not_a_number = true end, "index must be a number") 87 assert.has_error(function() bf.not_a_number = true end, "index must be a number")
88 end) 88 end)
89 89
90 it("checks for a subset using 'has'", function() 90 it("checks for a subset using 'has_all_of'", function()
91 local bf1 = sys.bitflag(3) -- b0011 91 local bf1 = sys.bitflag(3) -- b0011
92 local bf2 = sys.bitflag(3) -- b0011 92 local bf2 = sys.bitflag(3) -- b0011
93 local bf3 = sys.bitflag(15) -- b1111 93 local bf3 = sys.bitflag(15) -- b1111
94 local bf0 = sys.bitflag(0) -- b0000 94 local bf0 = sys.bitflag(0) -- b0000
95 assert.is_true(bf1:has(bf2)) -- equal 95 assert.is_true(bf1:has_all_of(bf2)) -- equal
96 assert.is_true(bf3:has(bf1)) -- is a subset, and has more flags 96 assert.is_true(bf3:has_all_of(bf1)) -- is a subset, and has more flags
97 assert.is_false(bf1:has(bf3)) -- not a subset, bf3 has more flags 97 assert.is_false(bf1:has_all_of(bf3)) -- not a subset, bf3 has more flags
98 assert.is_false(bf1:has(bf0)) -- bf0 is unset, always returns false 98 assert.is_false(bf1:has_all_of(bf0)) -- bf0 is unset, always returns false
99 end)
100
101 it("checks for a subset using 'has_any_of'", function()
102 local bf1 = sys.bitflag(3) -- b0011
103 local bf2 = sys.bitflag(3) -- b0011
104 local bf3 = sys.bitflag(7) -- b0111
105 local bf4 = sys.bitflag(8) -- b1000
106 local bf0 = sys.bitflag(0) -- b0000
107 assert.is_true(bf1:has_any_of(bf2)) -- equal
108 assert.is_true(bf3:has_any_of(bf1)) -- is a subset, and has more flags
109 assert.is_false(bf3:has_any_of(bf4)) -- no overlap in flags
110 assert.is_true(bf1:has_any_of(bf3)) -- not a subset, bf3 has more flags but still some overlap
111 assert.is_false(bf1:has_all_of(bf0)) -- bf0 is unset, always returns false
99 end) 112 end)
100 113
101end) 114end)
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
82print(flags6 == flags4) -- true, same flags 82print(flags6 == flags4) -- true, same flags
83 83
84-- comparison of subsets 84-- comparison of subsets
85local flags7 = sys.bitflag(0) -- b0000 85local flags7 = sys.bitflag(0) -- b0000
86local flags8 = sys.bitflag(3) -- b0011 86local flags8 = sys.bitflag(3) -- b0011
87local flags9 = sys.bitflag(7) -- b0111 87local flags9 = sys.bitflag(7) -- b0111
88print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9 88print(flags9:has_all_of(flags8)) -- true, flags8 bits are all set in flags9
89print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8 89print(flags8:has_any_of(flags9)) -- true, some of flags9 bits are set in flags8
90print(flags8:has_all_of(flags7)) -- false, flags7 (== 0) is not set in flags8
90*/ 91*/
91static int lsbf_new(lua_State *L) { 92static int lsbf_new(lua_State *L) {
92 LSBF_BITFLAG flags = 0; 93 LSBF_BITFLAG flags = 0;
@@ -134,26 +135,48 @@ static int lsbf_eq(lua_State *L) {
134} 135}
135 136
136/*** 137/***
137Checks if the given flags are set. 138Checks if all the flags in the given subset are set.
138This is different from the `>=` and `<=` operators because if the flag to check 139If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
139has a value `0`, it will always return `false`. So if there are flags that are 140unsupported on a platform, they can be set to 0 and the `has_all_of` function will
140unsupported on a platform, they can be set to 0 and the `has` function will
141return `false` if the flags are checked. 141return `false` if the flags are checked.
142@function bitflag:has 142@function bitflag:has_all_of
143@tparam bitflag subset the flags to check for. 143@tparam bitflag subset the flags to check for.
144@treturn boolean true if all the flags are set, false otherwise. 144@treturn boolean true if all the flags are set, false otherwise.
145@usage 145@usage
146local sys = require 'system' 146local sys = require 'system'
147local flags = sys.bitflag(12) -- b1100 147local flags = sys.bitflag(12) -- b1100
148local myflags = sys.bitflag(15) -- b1111 148local myflags = sys.bitflag(15) -- b1111
149print(flags:has(myflags)) -- false, not all bits in myflags are set in flags 149print(flags:has_all_of(myflags)) -- false, not all bits in myflags are set in flags
150print(myflags:has(flags)) -- true, all bits in flags are set in myflags 150print(myflags:has_all_of(flags)) -- true, all bits in flags are set in myflags
151*/ 151*/
152static int lsbf_has(lua_State *L) { 152static int lsbf_has_all_of(lua_State *L) {
153 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1); 153 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
154 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2); 154 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
155 // Check if all bits in b are also set in a, and b is not 0 155 // Check if all bits in b are also set in a, and b is not 0
156 lua_pushboolean(L, (a | b) == a && b != 0); 156 lua_pushboolean(L, (a & b) == b && b != 0);
157 return 1;
158}
159
160/***
161Checks if any of the flags in the given subset are set.
162If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
163unsupported on a platform, they can be set to 0 and the `has_any_of` function will
164return `false` if the flags are checked.
165@function bitflag:has_any_of
166@tparam bitflag subset the flags to check for.
167@treturn boolean true if any of the flags are set, false otherwise.
168@usage
169local sys = require 'system'
170local flags = sys.bitflag(12) -- b1100
171local myflags = sys.bitflag(7) -- b0111
172print(flags:has_any_of(myflags)) -- true, some bits in myflags are set in flags
173print(myflags:has_any_of(flags)) -- true, some bits in flags are set in myflags
174*/
175static int lsbf_has_any_of(lua_State *L) {
176 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
177 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
178 // Check if any bits in b are set in a
179 lua_pushboolean(L, (a & b) != 0);
157 return 1; 180 return 1;
158} 181}
159 182
@@ -201,7 +224,8 @@ static const struct luaL_Reg lsbf_funcs[] = {
201 224
202static const struct luaL_Reg lsbf_methods[] = { 225static const struct luaL_Reg lsbf_methods[] = {
203 {"value", lsbf_value}, 226 {"value", lsbf_value},
204 {"has", lsbf_has}, 227 {"has_all_of", lsbf_has_all_of},
228 {"has_any_of", lsbf_has_any_of},
205 {"__tostring", lsbf_tostring}, 229 {"__tostring", lsbf_tostring},
206 {"__add", lsbf_add}, 230 {"__add", lsbf_add},
207 {"__sub", lsbf_sub}, 231 {"__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')
386local flags = system.getconsoleflags(io.stdout) 386local flags = system.getconsoleflags(io.stdout)
387print("Current stdout flags:", tostring(flags)) 387print("Current stdout flags:", tostring(flags))
388 388
389if flags:has(system.COF_VIRTUAL_TERMINAL_PROCESSING + system.COF_PROCESSED_OUTPUT) then 389if flags:has_all_of(system.COF_VIRTUAL_TERMINAL_PROCESSING + system.COF_PROCESSED_OUTPUT) then
390 print("Both flags are set") 390 print("Both flags are set")
391else 391else
392 print("At least one flag is not set") 392 print("At least one flag is not set")
@@ -445,7 +445,7 @@ The terminal attributes is a table with the following fields:
445local system = require('system') 445local system = require('system')
446 446
447local status = assert(tcgetattr(io.stdin)) 447local status = assert(tcgetattr(io.stdin))
448if status.iflag:has(system.I_IGNBRK) then 448if status.iflag:has_all_of(system.I_IGNBRK) then
449 print("Ignoring break condition") 449 print("Ignoring break condition")
450end 450end
451*/ 451*/
@@ -539,7 +539,7 @@ _Note_: only `iflag`, `oflag`, and `lflag` are supported at the moment. The othe
539local system = require('system') 539local system = require('system')
540 540
541local status = assert(tcgetattr(io.stdin)) 541local status = assert(tcgetattr(io.stdin))
542if not status.lflag:has(system.L_ECHO) then 542if not status.lflag:has_all_of(system.L_ECHO) then
543 -- if echo is off, turn echoing newlines on 543 -- if echo is off, turn echoing newlines on
544 tcsetattr(io.stdin, system.TCSANOW, { lflag = status.lflag + system.L_ECHONL })) 544 tcsetattr(io.stdin, system.TCSANOW, { lflag = status.lflag + system.L_ECHONL }))
545end 545end
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)
154 local out = {} 154 local out = {}
155 for k,v in pairs(sys) do 155 for k,v in pairs(sys) do
156 if type(k) == "string" and k:sub(1,4) == flagtype then 156 if type(k) == "string" and k:sub(1,4) == flagtype then
157 if flags:has(v) then 157 if flags:has_all_of(v) then
158 out[#out+1] = string.format("%10d [x] %s",v:value(),k) 158 out[#out+1] = string.format("%10d [x] %s",v:value(),k)
159 else 159 else
160 out[#out+1] = string.format("%10d [ ] %s",v:value(),k) 160 out[#out+1] = string.format("%10d [ ] %s",v:value(),k)
@@ -191,7 +191,7 @@ function sys.listtermflags(fh)
191 local out = {} 191 local out = {}
192 for k,v in pairs(sys) do 192 for k,v in pairs(sys) do
193 if type(k) == "string" and k:sub(1,2) == prefix then 193 if type(k) == "string" and k:sub(1,2) == prefix then
194 if flags[flagtype]:has(v) then 194 if flags[flagtype]:has_all_of(v) then
195 out[#out+1] = string.format("%10d [x] %s",v:value(),k) 195 out[#out+1] = string.format("%10d [x] %s",v:value(),k)
196 else 196 else
197 out[#out+1] = string.format("%10d [ ] %s",v:value(),k) 197 out[#out+1] = string.format("%10d [ ] %s",v:value(),k)