Skip to content

Instantly share code, notes, and snippets.

@nghamilton
Created March 20, 2023 03:06
Text.append appears broken?
module TextIssue where
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
import System.IO
import Network.Socket
working :: IO ()
working = do
input <- Text.hGetLine stdin
Text.putStrLn input
-- this works as expected
Text.putStrLn $ Text.append "prepends" input
-- this works as expected
Text.putStrLn $ Text.append input "appends"
broken :: IO ()
broken = do
let hints = defaultHints { addrFlags = [AI_NUMERICHOST, AI_NUMERICSERV], addrSocketType = Stream }
addr:_ <- getAddrInfo (Just hints) (Just "127.0.0.1") (Just "5000")
sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
setSocketOption sock ReuseAddr 1
bind sock (addrAddress addr)
listen sock 1
(conn, _) <- accept sock
i <- socketToHandle conn ReadWriteMode
input <- Text.hGetLine i
Text.putStrLn input
-- this works as expected
Text.putStrLn $ Text.append "prepends" input
-- this overwrites the first 7 characters of input with "prepends"
Text.putStrLn $ Text.append input "appends"
hClose i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment