primitive (byte) string literal with length?

Viktor Dukhovni ietf-dane at dukhovni.org
Wed Aug 25 15:31:40 UTC 2021


On Tue, Aug 24, 2021 at 09:03:30AM -0400, Viktor Dukhovni wrote:

I originally wrote:

> > >Is there any GHC syntax for constructing a primitive string literal
> > >with a known (not hand coded) byte count?
> > >With `"some bytes"#` I get just the `Addr#` pointer, but not the size.
> > >
> > >If there's nothing available, would it be reasonable to introduce a new
> > >syntax?
> > >Perhaps:
> > >
> > >	"some bytes"## :: (# Addr#, Int# #)

But neglected to mention that I knew about `cstringLength#`, but found
it wanting, because it does not support octet-strings with embedded NUL
characters:

> Sadly, that does not work when the primitive octet string contains
> internal NUL bytes.
> 
>     λ> :set -package ghc-prim
>     λ> :set -XMagicHash
>     λ> import GHC.CString
>     λ> import GHC.Int
>     λ>
>     λ> I# (cstringLength# "foobar\xa0"#)
>     7
>     λ> I# (cstringLength# "foo\0bar\xa0"#)
>     3

If there isn't some other extant work-around, any feedback on my
proposal of a new syntax for a primitive unboxed (address, length) pair:

    "some bytes"## :: (# Addr#, Int# #)

-- 
    Viktor.


More information about the ghc-devs mailing list