on 5edacaa help sign in
vty/src/Codec/Binary/UTF8/Width.hs
32 lines 812 B history raw
1-- Copyright 2009 Corey O'Connor
2{-# OPTIONS_GHC -D_XOPEN_SOURCE -fno-cse #-}
3{-# LANGUAGE ForeignFunctionInterface #-}
4{-# INCLUDE <wchar.h> #-}
5module Codec.Binary.UTF8.Width ( wcwidth
6 , wcswidth
7 )
8 where
10import Foreign.C.Types
11import Foreign.C.String
12import Foreign.Storable
14import Numeric ( showHex )
16import System.IO.Unsafe
18wcwidth :: Char -> Int
19wcwidth c = unsafePerformIO (withCWString [c] $ \ws -> do
20 wc <- peek ws
21 putStr $ "wcwidth(0x" ++ showHex (fromEnum wc) "" ++ ")"
22 w <- wcwidth' wc >>= return . fromIntegral
23 putStrLn $ " -> " ++ show w
24 return w
25 )
26{-# NOINLINE wcwidth #-}
28foreign import ccall "mk_wcwidth" wcwidth' :: CWchar -> IO CInt
30wcswidth :: String -> Int
31wcswidth str = sum $ map wcwidth str