primitive (byte) string literal with length?

Viktor Dukhovni ietf-dane at dukhovni.org
Tue Aug 24 13:03:30 UTC 2021


On Tue, Aug 24, 2021 at 08:48:53AM +0200, Sylvain Henry wrote:

> Le 24 août 2021 à 06:34, à 06:34, Viktor Dukhovni <ietf-dane at dukhovni.org> a écrit:
> >
> >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# #)
>
> You can use cstringLength# which has a constant-folding rules for
> literals. That's what we use in GHC to build FastString literals.

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

-- 
	Viktor.


More information about the ghc-devs mailing list