~andrewrk/ziglang

2 2

std.mem.eql and sentinels

Details
Message ID
<DBAP190MB0982ECB1CD2B1B264F2C7182F99A9@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
DKIM signature
pass
Download raw message
Are these considered equal?

const a = [_:0]u8{ '4', '2'};
const b = [_]u8{ '4', '2'};

I am not sure how I could even check the sentinels match, or if it even 
matters :) but since I still have some custom string utils (some with 
sentinels, and some without (wasm not C)), I want to also have some 
tests to make sure they are adding/not adding sentinels in the correct 
place.  But then I get index of of bounds...  Any tips on how to test 
such functions please? Cheers Peter
Details
Message ID
<Yuh+sxH3pZRY3wlZ@gpanders.com>
In-Reply-To
<DBAP190MB0982ECB1CD2B1B264F2C7182F99A9@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM> (view parent)
DKIM signature
pass
Download raw message
On Mon, 01 Aug 2022 15:55 +0200, Peter Bridge wrote:
>Are these considered equal?
>
>const a = [_:0]u8{ '4', '2'};
>const b = [_]u8{ '4', '2'};
>
>I am not sure how I could even check the sentinels match, or if it 
>even matters :) but since I still have some custom string utils (some 
>with sentinels, and some without (wasm not C)), I want to also have 
>some tests to make sure they are adding/not adding sentinels in the 
>correct place.  But then I get index of of bounds...  Any tips on how 
>to test such functions please? Cheers Peter

"Equal" in what sense? They have different types, but they would compare
equal element-wise.

Sentinels are encoded in the type system. To check if a given variable
has a sentinel you can check the type info using @typeInfo, e.g.

     @typeInfo(@TypeOf(a)).Array.sentinel

There are also some handy functions in std.meta for this kind of
thing, e.g. std.meta.sentinel:

     const std = @import("std");
     test {
         const a = [_:0]u8{ '4', '2' };
         const b = [_]u8{ '4', '2' };

         try std.testing.expectEqual(std.meta.sentinel(@TypeOf(a)), 0);
         try std.testing.expectEqual(std.meta.sentinel(@TypeOf(b)), null);
     }

Greg
Details
Message ID
<DBAP190MB098294E7BE591F1A195A42AFF99D9@DBAP190MB0982.EURP190.PROD.OUTLOOK.COM>
In-Reply-To
<Yuh+sxH3pZRY3wlZ@gpanders.com> (view parent)
DKIM signature
pass
Download raw message
Hi Greg, Perfect! just what I was looking for, and makes total sense.  
Many thanks, Peter

On 02/08/2022 03:32, Gregory Anders wrote:
> On Mon, 01 Aug 2022 15:55 +0200, Peter Bridge wrote:
>> Are these considered equal?
>>
>> const a = [_:0]u8{ '4', '2'};
>> const b = [_]u8{ '4', '2'};
>>
>> I am not sure how I could even check the sentinels match, or if it 
>> even matters :) but since I still have some custom string utils (some 
>> with sentinels, and some without (wasm not C)), I want to also have 
>> some tests to make sure they are adding/not adding sentinels in the 
>> correct place.  But then I get index of of bounds...  Any tips on how 
>> to test such functions please? Cheers Peter
>
> "Equal" in what sense? They have different types, but they would compare
> equal element-wise.
>
> Sentinels are encoded in the type system. To check if a given variable
> has a sentinel you can check the type info using @typeInfo, e.g.
>
>     @typeInfo(@TypeOf(a)).Array.sentinel
>
> There are also some handy functions in std.meta for this kind of
> thing, e.g. std.meta.sentinel:
>
>     const std = @import("std");
>     test {
>         const a = [_:0]u8{ '4', '2' };
>         const b = [_]u8{ '4', '2' };
>
>         try std.testing.expectEqual(std.meta.sentinel(@TypeOf(a)), 0);
>         try std.testing.expectEqual(std.meta.sentinel(@TypeOf(b)), null);
>     }
>
> Greg
> .
>
Reply to thread Export thread (mbox)