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.
This commit is contained in:
Christoffer Lerno
2025-06-15 16:54:20 +02:00
parent 2afa544d7d
commit b2c994618f
2 changed files with 62 additions and 53 deletions

View File

@@ -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
}

View File

@@ -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