Questions tagged [bytestring]

A time and space-efficient implementation of byte vectors for Haskell.

A time and space-efficient implementation of byte vectors using packed Word8 arrays, suitable for high performance use, both in terms of large data quantities, or high speed requirements. Byte vectors are encoded as strict Word8 arrays of bytes, and lazy lists of strict chunks, held in a ForeignPtr, and can be passed between C and Haskell with little effort. Documentation can be found at the bytestring hackage page.

206 questions
43
votes
3 answers

What is the best way to convert String to ByteString

What is the best way to convert a String to a ByteString in Haskell? My gut reaction to the problem is import qualified Data.ByteString as B import Data.Char (ord) packStr = B.pack . map (fromIntegral . ord) But this doesn't seem satisfactory.
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
28
votes
5 answers

Haskell Bytestrings: How to pattern match?

I'm a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString. The [Char] version of my function looks like: dropAB :: String -> String dropAB [] = [] dropAB (x:[]) = x:[] dropAB (x:y:xs) = if x=='a' &&…
LOS
  • 523
  • 4
  • 6
27
votes
3 answers

Using Haskell to output a UTF-8-encoded ByteString

I'm going out of my mind trying to simply output UTF-8-encoded data to the console. I've managed to accomplish this using String, but now I'd like to do the same with ByteString. Is there a nice and fast way to do this? This is what I've got so far,…
user12163
24
votes
2 answers

When do I use ByteString and when do I not?

I've been making rather poor attempts at the PRIME1 problem on SPOJ. I discovered using that using ByteString really helped performance for reading in the problem text. However, using ByteString to write out the results is actually slightly slower…
Tim Perry
  • 3,066
  • 1
  • 24
  • 40
24
votes
1 answer

Couldn't match expected type `Data.ByteString.Internal.ByteString' with actual type `ByteString'

Running the following code: import Crypto.BCrypt import Data.ByteString.Lazy.Char8 main = do maybe_pwhash <- hashPasswordUsingPolicy slowerBcryptHashingPolicy (pack "hunter2") print $ maybe_pwhash I get the following compilation…
hugomg
  • 68,213
  • 24
  • 160
  • 246
24
votes
5 answers

Haskell How to convert Char to Word8

I want to split ByteString to words like so: import qualified Data.ByteString as BS main = do input <- BS.getLine let xs = BS.split ' ' input But it appears that GHC can't convert a character literal to Word8 by itself, so I got: Couldn't…
Andrew
  • 8,330
  • 11
  • 45
  • 78
23
votes
5 answers

Convert a Lazy ByteString to a strict ByteString

I have a function that takes a lazy ByteString, that I wish to have return lists of strict ByteStrings (the laziness should be transferred to the list type of the output). import qualified Data.ByteString as B import qualified Data.ByteString.Lazy…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
22
votes
1 answer

Data.Text vs Data.ByteString.Char8

Can anyone explain the pros and cons to using Data.Textand Data.ByteString.Char8 data types? Does working with ASCII-only text change these pros and cons? Do their lazy variants change the story as well?
Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
21
votes
2 answers

Haskell Lazy ByteString + read/write progress function

I am learing Haskell Lazy IO. I am looking for an elegant way to copy a large file (8Gb) while printing copy progress to console. Consider the following simple program that copies a file silently. module Main where import System import qualified…
oshyshko
  • 2,008
  • 2
  • 21
  • 31
19
votes
1 answer

Haskell iteratee: simple worked example of stripping trailing whitespace

I'm trying to understand how to use the iteratee library with Haskell. All of the articles I've seen so far seem to focus on building an intuition for how iteratees could be built, which is helpful, but now that I want to get down and actually use…
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
19
votes
1 answer

Reading large file in haskell?

i've been trying to read a large file in haskell. I need to compress it using a custom algorithm for a university project. Everything works fine untill i start to compress big files. I extracted what was going wrong out of my program, and i expose…
Joel
  • 3,427
  • 5
  • 38
  • 60
19
votes
3 answers

How to convert a Integer to a ByteString in Haskell

We'd like to serialize data in a specific binary format. We use Data.ByteStrings internally. So, the question is: How to convert the different data types we use to a ByteString. For String we have no problem, we can use encodeLazyByteString UTF8…
Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
17
votes
3 answers

Many types of String (ByteString)

I wish to compress my application's network traffic. According to the (latest?) "Haskell Popularity Rankings", zlib seems to be a pretty popular solution. zlib's interface uses ByteStrings: compress :: ByteString -> ByteString decompress ::…
yairchu
  • 23,680
  • 7
  • 69
  • 109
15
votes
1 answer

Haskell How to Create a Word8?

I want to write a simple function which splits a ByteString into [ByteString] using '\n' as the delimiter. My attempt: import Data.ByteString listize :: ByteString -> [ByteString] listize xs = Data.ByteString.splitWith (=='\n') xs This throws an…
Xander Dunn
  • 2,349
  • 2
  • 20
  • 32
14
votes
1 answer

Purity of functions generating ByteString (or any object with ForeignPtr component)

Since a ByteString is a constructor with ForeignPtr: data ByteString = PS {-# UNPACK #-} !(ForeignPtr Word8) -- payload {-# UNPACK #-} !Int                -- offset {-# UNPACK #-} !Int                --…
Sal
  • 4,312
  • 1
  • 17
  • 26
1
2 3
13 14