summaryrefslogtreecommitdiff
path: root/libraries/base/tools/ucd2haskell/tests/export_all_chars.hs
blob: 80f9c42f5edcc59fd2f6686ec9149c3e569ca8bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE BlockArguments #-}

-------------------------------------------------------------------------------
-- |
-- Description: Export all characters and their properties to a CSV file.
--
-- This is intended to compare Haskell results to other languages.
-------------------------------------------------------------------------------

module Main where

import Data.Char
import Data.Foldable
import Data.Version
import GHC.Unicode (unicodeVersion)
import Numeric

main :: IO ()
main = do
  -- First line is Unicode version
  putStrLn (showVersion unicodeVersion)
  -- Second line is CSV header
  putStrLn header
  -- Then all the supported characters
  traverse_ addEntry [minBound..maxBound]

-- | File header
header :: String
header = "Char,General Category,Lower Case,Upper Case,Title Case,isLowerCase,isUpperCase"

-- | Convert a character to its (short) hexadecimal Unicode codepoint.
mkCodePointHex :: Char -> String
mkCodePointHex c = showHex (ord c) mempty

-- | Make a CSV entry for a char.
addEntry :: Char -> IO ()
addEntry c = do
  putStr (mkCodePointHex c)
  putChar ','
  putStr (show (generalCategory c))
  putChar ','
  putStr (mkCodePointHex (toLower c))
  putChar ','
  putStr (mkCodePointHex (toUpper c))
  putChar ','
  putStr (mkCodePointHex (toTitle c))
  putChar ','
  putStr (show (isLowerCase c))
  putChar ','
  putStrLn (show (isUpperCase c))