Jump to content

Read a file character by character/UTF8: Difference between revisions

→‎{{header|Go}}: add Haskell
(Added Wren)
(→‎{{header|Go}}: add Haskell)
Line 182:
}
}</lang>
 
=={{header|Haskell}}==
 
{{Works with|GHC|7.8.3}}
 
<lang haskell>#!/usr/bin/env runhaskell
 
{- The procedure to read a UTF-8 character is just:
 
hGetChar :: Handle -> IO Char
 
assuming that the encoding for the handle has been set to utf8.
-}
 
import Control.Monad
import Data.Char
import System.Environment
import System.IO
import Text.Printf
 
processCharacters :: Handle -> IO ()
processCharacters h = do
done <- hIsEOF h
unless done $ do
c <- hGetChar h
putStrLn $ printf "U+%04X" (ord c)
processCharacters h
 
processOneFile :: Handle -> IO ()
processOneFile h = do
hSetEncoding h utf8
processCharacters h
 
{- You can specify one or more files on the command line, or if no
files are specified, it reads from standard input.
-}
main :: IO ()
main = do
args <- getArgs
case args of
[] -> processOneFile stdin
xs -> forM_ xs $ \name -> do
putStrLn name
withFile name ReadMode processOneFile</lang>
{{out}}
<pre>
bash$ echo €50 | ./read-char-utf8.hs
U+20AC
U+0035
U+0030
U+000A
</pre>
 
=={{header|J}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.