Skip to content

Instantly share code, notes, and snippets.

@avh4
Forked from dino-/string-conversions.hs
Created February 17, 2021 19:36
Show Gist options
  • Save avh4/49bc7894212c3058077750caf09f4c01 to your computer and use it in GitHub Desktop.
Save avh4/49bc7894212c3058077750caf09f4c01 to your computer and use it in GitHub Desktop.
A handy illustration of converting between String, Text and ByteString in Haskell
#! /usr/bin/env stack
-- stack --resolver lts-12.14 script
{-# LANGUAGE OverloadedStrings #-}
{-
This is a handy illustration of converting between five of the commonly-used
string types in Haskell (String, ByteString, lazy ByteString, Text and lazy
Text).
This code isn't necessarily encouraging anyone to not use some of the libraries
out there like string-conv (https://hackage.haskell.org/package/string-conv),
which is btw more comprehensive and safer via usage of
`Data.Text.Encoding.Error`.
Still, it's nice sometimes for small tasks to just use these examples below.
Also, note the use below of de/encodeUtf8, this may be inappropriate for your
tasks. See the many `decode*` and `encode*` functions in the `text` library.
If you've got Unicode strings getting mangled by these Data.ByteString.Char8.*
pack/unpack operations, use fromString/toString instead from
Data.ByteString.UTF8 in package utf8-string.
-}
import Data.ByteString.Char8 as B
import Data.ByteString.Lazy.Char8 as LB
import Data.Text as T
import Data.Text.Encoding as T
import Data.Text.IO as T
import Data.Text.Lazy as LT
import Data.Text.Lazy.Encoding as LT
import Data.Text.Lazy.IO as LT
import Prelude as P
main :: IO ()
main = do
P.putStrLn "from String"
B.putStrLn $ B.pack "String to strict ByteString"
LB.putStrLn $ LB.pack "String to lazy ByteString"
T.putStrLn $ T.pack "String to strict Text"
LT.putStrLn $ LT.pack "String to lazy Text"
P.putStrLn "\nfrom strict ByteString"
P.putStrLn $ B.unpack "strict ByteString to String"
LB.putStrLn $ LB.fromChunks . return $ "strict ByteString to lazy ByteString"
T.putStrLn $ T.decodeUtf8 "strict ByteString to strict Text"
LT.putStrLn $ LT.fromStrict . T.decodeUtf8 $ "strict ByteString to lazy Text"
P.putStrLn "\nfrom lazy ByteString"
P.putStrLn $ LB.unpack "lazy ByteString to String"
B.putStrLn $ B.concat . LB.toChunks $ "lazy ByteString to strict ByteString"
T.putStrLn $ T.decodeUtf8 . B.concat . LB.toChunks $ "lazy ByteString to strict Text"
LT.putStrLn $ LT.decodeUtf8 "lazy ByteString to lazy Text"
P.putStrLn "\nfrom strict Text"
P.putStrLn $ T.unpack "strict Text to String"
B.putStrLn $ T.encodeUtf8 "strict Text to strict ByteString"
LB.putStrLn $ LB.fromChunks . return . T.encodeUtf8 $ "strict Text to lazy ByteString"
LT.putStrLn $ LT.fromStrict "strict Text to lazy Text"
P.putStrLn "\nfrom lazy Text"
P.putStrLn $ LT.unpack "lazy Text to String"
B.putStrLn $ T.encodeUtf8 . LT.toStrict $ "lazy Text to strict ByteString"
LB.putStrLn $ LT.encodeUtf8 "lazy Text to lazy ByteString"
T.putStrLn $ LT.toStrict "lazy Text to strict Text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment