wcwidth-0.0.2/0000755000000000000000000000000011546243357011362 5ustar0000000000000000wcwidth-0.0.2/Setup.hs0000644000000000000000000000011111546243357013007 0ustar0000000000000000import Distribution.Simple main = defaultMain wcwidth-0.0.2/WCWidthTableaux.hs0000644000000000000000000000624611546243357014725 0ustar0000000000000000#!/usr/bin/env runhaskell {-# LANGUAGE StandaloneDeriving #-} import Data.Char import Data.List import System.Environment.UTF8 import System.IO import System.Exit import Text.Printf import System.Locale.SetLocale import qualified System.IO.UTF8 as UTF8 import Data.Char.WCWidth import CompileRanges (compile_ranges) usage name = unlines [ "USAGE: " ++ name ++ " > table" , " " ++ name ++ " --table > table" , " " ++ name ++ " --ranges > ranges" , " " ++ name ++ " --compile < ranges > Data/Char/Cols/Generated.hs" , " " ++ name ++ " -h,--help" , "" , " This program polls your local wcwidth implementation for character width" , " information and generates tables or a chart of ranges." , "" ] main = do setLocale LC_ALL (Just "") usage' <- fmap usage getProgName programs <- fmap ((Table:) . fmap program) getArgs case (head . sort) programs of Usage -> putStrLn usage' >> exitSuccess Compile -> compile_ranges stdin stdout Ranges -> (rolling_print range_entry) ranges Table -> (rolling_print table_entry) widths Error s -> do hPutStrLn stderr s hPutStrLn stderr usage' exitFailure where rolling_print f = sequence_ . fmap (putStrLn . f) range_entry ((a,b),w) = printf fmt a' b' w count s where count = 1 + fromEnum b - fromEnum a fmt = "0x%08x..0x%08x %2d %6d %s" (a', b') = (fromEnum a, fromEnum b) s = represent a ++ " .. " ++ represent b table_entry (c,cols) = printf "0x%08x %2d %s" c' cols c'' where c' = fromEnum c c'' = represent c represent c | ' ' == c = "\\SP" | '\xA0' == c = " " | isControl c = display c | isSpace c = '\\' : show (fromEnum c) | isPrint c = [c] | otherwise = display c where display = reverse . drop 1 . reverse . drop 1 . show program opt = case opt of "-h" -> Usage "--help" -> Usage "--compile" -> Compile "--ranges" -> Ranges "--table" -> Table s -> Error ("No such option/arg: " ++ s) data Program = Usage | Compile | Error String | Ranges | Table deriving instance Eq Program instance Ord Program where compare a b | a == b = EQ | otherwise = case a of Usage -> LT Compile -> LT Ranges -> LT Table -> GT Error s -> case b of Error t -> compare s t _ -> LT wcwidth-0.0.2/LICENSE0000644000000000000000000000262711546243357012376 0ustar0000000000000000 ©2009 Jason Dusek. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: . Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. . Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. . Names of the contributors to this software may not be used to endorse or promote products derived from this software without specific prior written permission. This software is provided by the contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. wcwidth-0.0.2/CompileRanges.hs0000644000000000000000000000534311546243357014453 0ustar0000000000000000#!/usr/bin/env runhaskell {-# LANGUAGE OverloadedStrings #-} module CompileRanges where import Prelude hiding (concat, lines, unlines, takeWhile) import Data.List (foldl') import Data.Char hiding (isDigit) import Data.Maybe import Data.Either import Control.Applicative hiding (empty) import System.IO (stdin, stdout, stderr) import Data.ByteString.Char8 hiding (takeWhile, count, foldl', reverse) import Data.Attoparsec (parseOnly) import Data.Attoparsec.Char8 main = compile_ranges stdin stdout compile_ranges i o = do ranges <- rights . parse' . lines <$> hGetContents i --sequence_ . (hPut o . display <$>) $ collate ranges hPut o preamble (sequence_ . (hPut o . compile <$>) . collate) ranges hPut o postamble where parse' = (parseOnly range <$>) display ((a,z),w) = a `append` pack ".." `append` z `snoc` '\n' compile ((a,z),w) = guard `append` eq `append` w `snoc` '\n' where guard = pack " | i <= " `append` z eq = pack " = " preamble = (unlines . fmap pack) [warning, mod, sig, f] where warning = "\n\n-- This file was autogenerated.\n" mod = "\nmodule Data.Char.Cols.Generated where\n\n" sig = "cols :: Char -> Int\n" f = "cols c\n" postamble = otherwise_clause `append` where_clause where otherwise_clause = pack " | otherwise = -1\n" where_clause = pack " where\n" `append` pack i `snoc` '\n' where i = " i = fromEnum c\n" range = do start <- short_hex string ".." end <- short_hex some (char ' ') columns <- little_int return ((start, end), columns) short_hex :: Parser ByteString short_hex = do ox <- string "0x" count 4 (char '0') append ox . pack <$> count 4 (satisfy isHexDigit) little_int :: Parser ByteString little_int = do sign <- maybe empty id <$> optional minus digits <- takeWhile isDigit return (append sign digits) where minus = string "-" collate = reverse . foldl' collate' [] where collate' [] range = [range] collate' (((a,z),w):t) ((a',z'),w') | w == w' = ((a,z'),w) : t | otherwise = ((a',z'),w') : ((a,z),w) : t wcwidth-0.0.2/wcwidth.cabal0000644000000000000000000000371611546243357014026 0ustar0000000000000000name : wcwidth version : 0.0.2 category : Text license : BSD3 license-file : LICENSE author : Jason Dusek maintainer : oss@solidsnack.be homepage : http://github.com/solidsnack/wcwidth/ synopsis : Native wcwidth. description : Bindings for your system's native wcwidth and a command line tool to examine the widths assigned by it. The command line tool can compile a width table to Haskell code that assigns widths to the Char type. cabal-version : >= 1.6.0 build-type : Simple extra-source-files : CompileRanges.hs flag split-base flag cli description : Enable command line tool. default : False library if flag(split-base) build-depends : base >= 4 && < 5 else build-depends : base < 4 build-depends : containers exposed-modules : Data.Char.WCWidth extensions : StandaloneDeriving ForeignFunctionInterface OverloadedStrings executable wcwidth-tools main-is : WCWidthTableaux.hs if flag(cli) buildable : True else buildable : False if flag(split-base) build-depends : base >= 4 && < 5 else build-depends : base < 4 build-depends : containers , bytestring , setlocale >= 0.0.3 , utf8-string >= 0.3 , attoparsec >= 0.8.5 extensions : StandaloneDeriving ForeignFunctionInterface OverloadedStrings wcwidth-0.0.2/Data/0000755000000000000000000000000011546243357012233 5ustar0000000000000000wcwidth-0.0.2/Data/Char/0000755000000000000000000000000011546243357013110 5ustar0000000000000000wcwidth-0.0.2/Data/Char/WCWidth.hs0000644000000000000000000000366511546243357014767 0ustar0000000000000000 {-# LANGUAGE ForeignFunctionInterface #-} {-| A binding for the native 'wcwidth'. It's important that you 'setLocale' before using it, like this: > #!/usr/bin/env runhaskell > > import Text.Printf > > import System.Locale.SetLocale > import Data.Char.WCWidth > > main = do > setLocale LC_ALL (Just "") > sequence_ [ display c | c <- chars ] > where > chars = [minBound..'A'] > display c = printf "%04x %2d %s\n" (fromEnum c) (wcwidth c) (show c) The program file @WCWidthTableaux.hs@ contains a more extensive example of using 'wcwidth'. Note that this binding to the native implementation gets certain characters wrong in obvious ways as well as ways that are problematic for indentation based languages. The ASCII tab should be assigned a width of 8, not -1; and one is likely to find -1 assigned to numerous obscure characters (for example, symbols from the Book of Changes). -} module Data.Char.WCWidth ( wcwidth , widths , ranges ) where import Foreign.C import Data.List {-| Widths of all characters. -} widths :: [ (Char, Int) ] widths = [ (c, wcwidth c) | c <- [minBound..maxBound] ] {-| Characters broken into contiguous ranges with the same width. -} ranges :: [ ((Char, Char), Int) ] ranges = reverse (foldl' aggregate start (tail widths)) where start = aggregate [] (head widths) aggregate [] (c, w) = [((c, c), w)] aggregate (((a, z), i) : t) (c, w) | i == w = ((a, c), i) : t | otherwise = ((c, c), w) : ((a, z), i) : t {-| Binding to the native 'wcwidth'. -} wcwidth :: Char -> Int wcwidth = fromEnum . native . toEnum . fromEnum foreign import ccall unsafe "wchar.h wcwidth" native :: CWchar -> CInt