develooper Front page | perl.perl5.porters | Postings from January 2020

Re: “strict” strings?

Thread Previous | Thread Next
Felipe Gasper
January 7, 2020 04:04
Re: “strict” strings?
Message ID:

> On Jan 6, 2020, at 10:36 PM, Tony Cook <> wrote:
> On Mon, Jan 06, 2020 at 10:02:10PM -0500, Felipe Gasper wrote:
>>> On Jan 6, 2020, at 9:43 PM, Tony Cook <> wrote:
>>> If Sereal converts a SVf_UTF8 off SV-with-PV to a binary specific type in some
>>> other language, that is a bug in Sereal.  I haven't tried it.
>> FWIW:
>>> perl -MSereal::Encoder -e'my $a = "\xc2\xa9"; print encode_sereal($a)' | xxd
>> 00000000: 3df3 726c 0400 62c2 a9                   =.rl..b..
>> The antepenultimate 0x62 is SHORT_BINARY_2.
>>> perl -MSereal::Encoder -e'my $a = "\xc2\xa9"; utf8::decode($a); print encode_sereal($a)' | xxd
>> 00000000: 3df3 726c 0400 2702 c2a9                 =.rl..'...
>> The [0x27 0x02] sequence indicates STR_UTF8, length 2.
>> And in fact, the module’s POD specifically states that it keys off SVf_UTF8. So there’s that.
> That doesn't answer my supposition.

> If I understand the code either way it ends up with a string object,
> not a bytes object ( decoded with struct.unpack with a format of
> "<length>s" which returns a string ).

Python has a distinct byte-string type, though. With the new flag it behaves thus:

>>> from sereal.decoder import SrlDecoder
>>> decoder = SrlDecoder( bin_mode_classic=0 )
>>> decoder.decode(b'=\xf3rl\x04\x00b\xc2\xa9')
>>> decoder.decode(b'=\xf3rl\x04\x00\x27\x02\xc2\xa9')

Note that decoding the Sereal byte/“encodingless” string yields a Python byte string (the “b” prefix), whereas decoding the Sereal Unicode yields a text string. The only thing in Perl that caused the difference between the two Sereal documents is the UTF8 flag.

> Java appears to treat it as a String rather than a byte[]:

This decoder appears to be configurable as well:

Unlike the Python implementation, though, in Java the default appears to be to return byte[]. I’ve not run it, though.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About