Another way to do this could be to just create your own method.

my method decode-c(Blob:) { self.subbuf(^self.first(0,:kv)[0]).decode }
my $string-with-zero = Buf.new: 72, 101, 108, 108, 111, 0, 32, 119,
111, 114, 108, 100, 33;

say $string-with-zero.decode;
say $string-with-zero.&decode-c;

The output of the last two lines would be "Hello World!" and "Hello",
respectively (although with a U+0000 character after the first o).  If
used frequently enough (or one dislikes the .& syntax), it could also
be added into Blob/Buf via an augment.

El mié, 9 jun 2021 a las 10:47, Daniel Sockwell
(<dan...@codesections.com>) escribió:
>
> Hi Paul,
>
> If you _do_ want/need to work with C-style null-terminated strings, you can 
> use the (core)
> NativeCall library.  So, given your example:
>
> > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
> > say $b.decode;
> > I would expect this to print 'Hi'.
> >
> > Instead it prints 'Hi again'.
>
> You can write:
>
>    use NativeCall;
>    say nativecast(str, $b) # prints 'Hi'
>
> Hope that helps!
>
> – codesections

Reply via email to