From b2c994618f97501830bed6cb4a317274f284394e Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 15 Jun 2025 16:54:20 +0200 Subject: [PATCH] Fix to `is_array_or_slice_of_char` #2214. `is_array_or_slice_of_char` and `is_arrayptr_or_slice_of_char` are replaced by constant `@` variants. --- lib/std/core/bitorder.c3 | 112 +++++++++++++++++++++------------------ releasenotes.md | 3 +- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/lib/std/core/bitorder.c3 b/lib/std/core/bitorder.c3 index da6c06484..34b9457ea 100644 --- a/lib/std/core/bitorder.c3 +++ b/lib/std/core/bitorder.c3 @@ -88,33 +88,33 @@ bitstruct UInt128LE : uint128 @littleendian } <* - @require is_array_or_slice_of_char(bytes) : "argument must be an array, a pointer to an array or a slice of char" + @require @is_array_or_slice_of_char(bytes) : "argument must be an array, a pointer to an array or a slice of char" @require is_bitorder($Type) : "type must be a bitorder integer" *> macro read(bytes, $Type) { char[] s; $switch @typekind(bytes): - $case POINTER: - s = (*bytes)[:$Type.sizeof]; - $default: - s = bytes[:$Type.sizeof]; + $case POINTER: + s = (*bytes)[:$Type.sizeof]; + $default: + s = bytes[:$Type.sizeof]; $endswitch return bitcast(*(char[$Type.sizeof]*)s.ptr, $Type).val; } <* - @require is_arrayptr_or_slice_of_char(bytes) : "argument must be a pointer to an array or a slice of char" + @require @is_arrayptr_or_slice_of_char(bytes) : "argument must be a pointer to an array or a slice of char" @require is_bitorder($Type) : "type must be a bitorder integer" *> macro write(x, bytes, $Type) { char[] s; $switch @typekind(bytes): - $case POINTER: - s = (*bytes)[:$Type.sizeof]; - $default: - s = bytes[:$Type.sizeof]; + $case POINTER: + s = (*bytes)[:$Type.sizeof]; + $default: + s = bytes[:$Type.sizeof]; $endswitch *($typeof(x)*)s.ptr = bitcast(x, $Type).val; } @@ -122,55 +122,63 @@ macro write(x, bytes, $Type) macro is_bitorder($Type) { $switch $Type: - $case UShortLE: - $case ShortLE: - $case UIntLE: - $case IntLE: - $case ULongLE: - $case LongLE: - $case UInt128LE: - $case Int128LE: - $case UShortBE: - $case ShortBE: - $case UIntBE: - $case IntBE: - $case ULongBE: - $case LongBE: - $case UInt128BE: - $case Int128BE: - return true; - $default: - return false; + $case UShortLE: + $case ShortLE: + $case UIntLE: + $case IntLE: + $case ULongLE: + $case LongLE: + $case UInt128LE: + $case Int128LE: + $case UShortBE: + $case ShortBE: + $case UIntBE: + $case IntBE: + $case ULongBE: + $case LongBE: + $case UInt128BE: + $case Int128BE: + return true; + $default: + return false; $endswitch } -macro bool is_array_or_slice_of_char(bytes) +macro bool is_array_or_slice_of_char(bytes) @deprecated("Use @is_array_or_slice_of_char") { - $switch @typekind(bytes): - $case POINTER: - typeid $inner = $typeof(bytes).inner; - $if $inner.kindof == ARRAY: - return $inner.inner == char.typeid; - $endif - $case ARRAY: - $case SLICE: - return $typeof(bytes).inner == char.typeid; - $default: - return false; + return @is_array_or_slice_of_char(bytes); +} + +macro bool @is_array_or_slice_of_char(#bytes) @const +{ + var $Type = $typeof(#bytes); + $switch $Type.kindof: + $case POINTER: + typeid $inner = $Type.inner; + return $inner.kindof == ARRAY &&& $inner.inner == char.typeid; + $case ARRAY: + $case SLICE: + return $Type.inner == char.typeid; + $default: + return false; $endswitch } -macro bool is_arrayptr_or_slice_of_char(bytes) +macro bool is_arrayptr_or_slice_of_char(bytes) @deprecated("Use @is_arrayptr_or_slice_of_char") { - $switch @typekind(bytes): - $case POINTER: - var $inner = $typeof(bytes).inner; - $if $inner.kindof == ARRAY: - return $inner.inner == char.typeid; - $endif - $case SLICE: - return $typeof(bytes).inner == char.typeid; - $default: - return false; + return @is_arrayptr_or_slice_of_char(bytes); +} + +macro bool @is_arrayptr_or_slice_of_char(#bytes) @const +{ + var $Type = $typeof(#bytes); + $switch $Type.kindof: + $case POINTER: + typeid $inner = $Type.inner; + return $inner.kindof == ARRAY &&& $inner.inner == char.typeid; + $case SLICE: + return $Type.inner == char.typeid; + $default: + return false; $endswitch } \ No newline at end of file diff --git a/releasenotes.md b/releasenotes.md index 7ebb86651..334af2359 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -3,7 +3,6 @@ ## 0.7.3 Change list ### Changes / improvements - - `$typefrom` now also accepts a constant string, and so works like `$evaltype`. - `$evaltype` is deprecated in favour of `$typefrom`. - Literal rules have changed, this makes `-0xFF` now a signed integer. @@ -38,10 +37,12 @@ - `cflags` additions for targets was not handed properly. #2209 - `$echo` would suppress warning about unreachable code. #2205 - Correctly format '%c' when given a width. #2199 +- Fix to `is_array_or_slice_of_char` #2214. ### Stdlib changes - Deprecate `String.is_zstr` and `String.quick_zstr` #2188. - Add comparison with `==` for ZString types. +- `is_array_or_slice_of_char` and `is_arrayptr_or_slice_of_char` are replaced by constant `@` variants. ## 0.7.2 Change list