[Haskell-cafe] How do I use ByteString?

Mads Lindstrøm mads_lindstroem at yahoo.dk
Tue Apr 6 09:14:16 EDT 2010


Hi

Don Stewart wrote:
> gue.schmidt:
> > Hi all,
> >
> > I've never found an easy way to deal with ByteStrings.
> >
> > I'm using the RSA library and it en- and decodes  
> > Data.ByteString.Lazy.ByteString.
> >
> > I initially start with Strings, ie. [Char], but there is no function to  
> > convert the 2 back and forth. There is however a function which takes  
> > [Word8] to BytesString and back.
> >
> > It all wouldn't be so confusing if there weren't several versions of  
> > ByteString in several modules to choose from. And a number of libraries  
> > requiring different types of ByteString.
> >
> > I am sure the designers of the bytestring package had good reason for  
> > this design, is there also a webpage which explains which one to use and  
> > under what circumstances?

I really would like such a webpage, because I am also confused about
when to use what type of strings.

> 
> As a general rule you should never convert between String and
> bytestring. You can read bytestrings from files, or write literals using
> "pack" or the -XOverloadedStrings extension

Except that different libraries require different kind of strings. Some
use strict bytestring, some use lazy bytestring, some use [Char], some
use lazy Data.Text, ... So you really need to convert between different
representations. It may seem unfair that I put byte-strings and
char-strings in the same bucket, but libraries do use byte-strings to
contain characters. For example, Parsec has a [Char] and a bytestring
interface.

Strings are the glue that binds many libraries together, and the many
different types of strings complicate this glue. It is therefore much
more complicated to compose different libraries than it was when most
libraries used [Char]. It really would make Haskell programming easier
if we could settle on one char-string type (and one type for
byte-strings).

> 
> Converting between lazy and strict bytestrings is a representation
> change, and changes complexity - so be aware of what you're doing. You
> can do this via fromChunks or concat . toChunks.

Reading the introduction to lazy bytestring here
http://hackage.haskell.org/packages/archive/bytestring/0.9.1.5/doc/html/Data-ByteString-Lazy.html it seems that lazy bytestring always has at least as good complexity as strict bytestrings. Lazy Data.Text strings also has at least as good complexity as strict Data.Text strings. So, for public library interfaces it would be great if could settle for some lazy string type like Data.Text. Preferably a string type that could be efficiently converted to lazy bytestrings for libraries that deal with bytes and not characters. This however, requires that the community can settle for one char-string type and a good beginning could be a description of how string handling is supposed to happen in Haskell (I am thinking about a webpage like Günther writes about).


/Mads

> 
> -- Don
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100406/3ef857a0/attachment.bin


More information about the Haskell-Cafe mailing list