{-# OPTIONS_GHC -D_XOPEN_SOURCE #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Graphics.Text.Width
( wcwidth
, wcswidth
, wctwidth
, wctlwidth
, safeWcwidth
, safeWcswidth
, safeWctwidth
, safeWctlwidth
)
where
import Data.List (foldl')
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
foreign import ccall unsafe "vty_mk_wcwidth" wcwidth :: Char -> Int
wcswidth :: String -> Int
wcswidth :: String -> Int
wcswidth = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Int
l Char
c -> Char -> Int
wcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0
{-# INLINE [1] wcswidth #-}
wctwidth :: T.Text -> Int
wctwidth :: Text -> Int
wctwidth = forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Int
l Char
c -> Char -> Int
wcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0
wctlwidth :: TL.Text -> Int
wctlwidth :: Text -> Int
wctlwidth = forall a. (a -> Char -> a) -> a -> Text -> a
TL.foldl' (\Int
l Char
c -> Char -> Int
wcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0
{-# RULES
"wcswidth/unpack" forall x. wcswidth (T.unpack x) = wctwidth x
"wcswidth/lazy-unpack" forall x. wcswidth (TL.unpack x) = wctlwidth x
#-}
safeWcwidth :: Char -> Int
safeWcwidth :: Char -> Int
safeWcwidth = forall a. Ord a => a -> a -> a
max Int
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
wcwidth
safeWcswidth :: String -> Int
safeWcswidth :: String -> Int
safeWcswidth = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Int
l Char
c -> Char -> Int
safeWcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0
safeWctwidth :: T.Text -> Int
safeWctwidth :: Text -> Int
safeWctwidth = forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Int
l Char
c -> Char -> Int
safeWcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0
safeWctlwidth :: TL.Text -> Int
safeWctlwidth :: Text -> Int
safeWctlwidth = forall a. (a -> Char -> a) -> a -> Text -> a
TL.foldl' (\Int
l Char
c -> Char -> Int
safeWcwidth Char
c forall a. Num a => a -> a -> a
+ Int
l) Int
0