Difference between revisions of "UTF-8"

From HaskellWiki
Jump to: navigation, search
m (Reverted edits by Tomjaguarpaw (talk) to last revision by PhilipNeustrom)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Code]]
 
[[Category:Code]]
   
The simplest solution seems to be to use the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/utf8-string utf8-string package] from Galois. It provides a drop-in replacement for System.IO
+
The simplest solution seems to be to use the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/utf8-string utf8-string package] from Galois. It
  +
provides a drop-in replacement for System.IO
  +
  +
''What about other string encodings?''
  +
  +
== Example ==
  +
If we use a function from System.IO.UTF8, we should also hide the equivalent one from the Prelude. (Alternatively, we could import the UTF8 module qualified)
  +
  +
<haskell>
  +
> import System.IO.UTF8
  +
> import Prelude hiding (readFile, writeFile)
  +
> import System.Environment (getArgs)
  +
</haskell>
  +
  +
The readFile and writeFile functions are the same as before...
   
 
<haskell>
 
<haskell>
import System.IO.UTF8
 
  +
> main :: IO ()
import Prelude hiding (getContents, putStrLn)
 
 
> main =
  +
> do args <- getArgs
  +
> mapM_ reverseUTF8File args
   
main =
 
  +
> reverseUTF8File :: FilePath -> IO ()
do putStrLn "what is your name? "
 
  +
> reverseUTF8File f =
name <- getContents
+
> do c <- readFile f
putStrLn $ "hello, " ++ name ++ "!"
+
> writeFile (f ++ ".rev") $ reverseLines c
  +
> where
  +
> reverseLines = unlines . map reverse . lines
 
</haskell>
 
</haskell>

Latest revision as of 15:20, 6 February 2021


The simplest solution seems to be to use the utf8-string package from Galois. It provides a drop-in replacement for System.IO

What about other string encodings?

Example

If we use a function from System.IO.UTF8, we should also hide the equivalent one from the Prelude. (Alternatively, we could import the UTF8 module qualified)

> import System.IO.UTF8
> import Prelude hiding (readFile, writeFile)
> import System.Environment (getArgs)

The readFile and writeFile functions are the same as before...

> main :: IO ()
> main =
>  do args <- getArgs
>     mapM_ reverseUTF8File args

> reverseUTF8File :: FilePath -> IO ()
> reverseUTF8File f =
>   do c <- readFile f
>      writeFile (f ++ ".rev") $ reverseLines c
>   where
>     reverseLines = unlines . map reverse . lines