diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-04 09:06:03 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-05-04 09:06:03 +0200 |
commit | b41df538c72c7e9a26f9ff08f2668769c8d1a1d1 (patch) | |
tree | 59820cae6b9edb014520487f5d2b0c9f91bc6b17 | |
parent | fdf62fe6c4125d29b3097e49566b2bbe1f650382 (diff) | |
download | luasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.tar.gz luasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.tar.bz2 luasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.zip |
switch "has" to "has_all_of" and "has_any_of"
-rw-r--r-- | spec/05-bitflags_spec.lua | 23 | ||||
-rw-r--r-- | src/bitflags.c | 58 | ||||
-rw-r--r-- | src/term.c | 6 | ||||
-rw-r--r-- | system/init.lua | 4 |
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 | ||
101 | end) | 114 | 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 | |||
82 | print(flags6 == flags4) -- true, same flags | 82 | print(flags6 == flags4) -- true, same flags |
83 | 83 | ||
84 | -- comparison of subsets | 84 | -- comparison of subsets |
85 | local flags7 = sys.bitflag(0) -- b0000 | 85 | local flags7 = sys.bitflag(0) -- b0000 |
86 | local flags8 = sys.bitflag(3) -- b0011 | 86 | local flags8 = sys.bitflag(3) -- b0011 |
87 | local flags9 = sys.bitflag(7) -- b0111 | 87 | local flags9 = sys.bitflag(7) -- b0111 |
88 | print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9 | 88 | print(flags9:has_all_of(flags8)) -- true, flags8 bits are all set in flags9 |
89 | print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8 | 89 | print(flags8:has_any_of(flags9)) -- true, some of flags9 bits are set in flags8 |
90 | print(flags8:has_all_of(flags7)) -- false, flags7 (== 0) is not set in flags8 | ||
90 | */ | 91 | */ |
91 | static int lsbf_new(lua_State *L) { | 92 | static 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 | /*** |
137 | Checks if the given flags are set. | 138 | Checks if all the flags in the given subset are set. |
138 | This is different from the `>=` and `<=` operators because if the flag to check | 139 | If the flags to check has a value `0`, it will always return `false`. So if there are flags that are |
139 | has a value `0`, it will always return `false`. So if there are flags that are | 140 | unsupported on a platform, they can be set to 0 and the `has_all_of` function will |
140 | unsupported on a platform, they can be set to 0 and the `has` function will | ||
141 | return `false` if the flags are checked. | 141 | return `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 |
146 | local sys = require 'system' | 146 | local sys = require 'system' |
147 | local flags = sys.bitflag(12) -- b1100 | 147 | local flags = sys.bitflag(12) -- b1100 |
148 | local myflags = sys.bitflag(15) -- b1111 | 148 | local myflags = sys.bitflag(15) -- b1111 |
149 | print(flags:has(myflags)) -- false, not all bits in myflags are set in flags | 149 | print(flags:has_all_of(myflags)) -- false, not all bits in myflags are set in flags |
150 | print(myflags:has(flags)) -- true, all bits in flags are set in myflags | 150 | print(myflags:has_all_of(flags)) -- true, all bits in flags are set in myflags |
151 | */ | 151 | */ |
152 | static int lsbf_has(lua_State *L) { | 152 | static 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 | /*** | ||
161 | Checks if any of the flags in the given subset are set. | ||
162 | If the flags to check has a value `0`, it will always return `false`. So if there are flags that are | ||
163 | unsupported on a platform, they can be set to 0 and the `has_any_of` function will | ||
164 | return `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 | ||
169 | local sys = require 'system' | ||
170 | local flags = sys.bitflag(12) -- b1100 | ||
171 | local myflags = sys.bitflag(7) -- b0111 | ||
172 | print(flags:has_any_of(myflags)) -- true, some bits in myflags are set in flags | ||
173 | print(myflags:has_any_of(flags)) -- true, some bits in flags are set in myflags | ||
174 | */ | ||
175 | static 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 | ||
202 | static const struct luaL_Reg lsbf_methods[] = { | 225 | static 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}, |
@@ -386,7 +386,7 @@ local system = require('system') | |||
386 | local flags = system.getconsoleflags(io.stdout) | 386 | local flags = system.getconsoleflags(io.stdout) |
387 | print("Current stdout flags:", tostring(flags)) | 387 | print("Current stdout flags:", tostring(flags)) |
388 | 388 | ||
389 | if flags:has(system.COF_VIRTUAL_TERMINAL_PROCESSING + system.COF_PROCESSED_OUTPUT) then | 389 | if 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") |
391 | else | 391 | else |
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: | |||
445 | local system = require('system') | 445 | local system = require('system') |
446 | 446 | ||
447 | local status = assert(tcgetattr(io.stdin)) | 447 | local status = assert(tcgetattr(io.stdin)) |
448 | if status.iflag:has(system.I_IGNBRK) then | 448 | if status.iflag:has_all_of(system.I_IGNBRK) then |
449 | print("Ignoring break condition") | 449 | print("Ignoring break condition") |
450 | end | 450 | end |
451 | */ | 451 | */ |
@@ -539,7 +539,7 @@ _Note_: only `iflag`, `oflag`, and `lflag` are supported at the moment. The othe | |||
539 | local system = require('system') | 539 | local system = require('system') |
540 | 540 | ||
541 | local status = assert(tcgetattr(io.stdin)) | 541 | local status = assert(tcgetattr(io.stdin)) |
542 | if not status.lflag:has(system.L_ECHO) then | 542 | if 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 })) |
545 | end | 545 | 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) | |||
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) |