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 /src | |
| 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"
Diffstat (limited to 'src')
| -rw-r--r-- | src/bitflags.c | 58 | ||||
| -rw-r--r-- | src/term.c | 6 |
2 files changed, 44 insertions, 20 deletions
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 |
