on master help sign in
Merge pull request #105 from glguy/fix-wcwidth

Fix wcwidth import and match safeWcswidth to its documented behavior

commite729e2abd354b3215aef2e4058c1ea2ac5cfbc1d authorJonathan Daugherty <cygnus@foobox.com> dateSun 28 Aug 2016, 14:38 (10 years ago) parent1f592bc92ec351
1 file changed+8−8
src/Graphics/Text/Width.hs+8−8
src/Graphics/Text/Width.hs+8−8
@@ -8,7 +8,12 @@ module Graphics.Text.Width ( wcwidth
88 )
99 where
1010
11−foreign import ccall unsafe "vty_mk_wcwidth" wcwidth :: Char -> Int
11+import Foreign.C.Types (CInt(..))
12+
13+foreign import ccall unsafe "vty_mk_wcwidth" c_wcwidth :: Char -> CInt
14+
15+wcwidth :: Char -> Int
16+wcwidth = fromIntegral . c_wcwidth
1217
1318wcswidth :: String -> Int
1419wcswidth = sum . map wcwidth
@@ -22,13 +27,8 @@ wcswidth = sum . map wcwidth
2227
2328-- | Returns the display width of a character. Assumes all characters with unknown widths are 0 width
2429safeWcwidth :: Char -> Int
25−safeWcwidth c = case wcwidth c of
26− i | i < 0 -> 0
27− | otherwise -> i
30+safeWcwidth = max 0 . wcwidth
2831
2932-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
3033safeWcswidth :: String -> Int
31−safeWcswidth str = case wcswidth str of
32− i | i < 0 -> 0
33− | otherwise -> i
34−
34+safeWcswidth = sum . map safeWcwidth