on master help sign in
Fix wcwidth import and match safeWcswidth to its documented behavior

Previously vty_mk_wcwidth was being imported with the wrong type causing the -1 return value to be mapped to the wrong Int value. Additionally safeWcswidth was using the unsafe character width function and only ensuring that the final result was non-negative.

commit92ec351173d49c0dff5c6efc035ef20d8d757b19 authorEric Mertens <emertens@gmail.com> dateSun 28 Aug 2016, 14:29 (10 years ago) parent1f592bc
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