packages feed

unicode-show (empty) → 0.1.0.0

raw patch · 5 files changed

+262/−0 lines, 5 filesdep +HUnitdep +QuickCheckdep +basesetup-changed

Dependencies added: HUnit, QuickCheck, base, test-framework, test-framework-hunit, test-framework-quickcheck2, unicode-show

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Takayuki Muranushi (c) 2016++All rights reserved.++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.++    * Neither the name of Author name here nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 COPYRIGHT+OWNER OR 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Text/Show/Unicode.hs view
@@ -0,0 +1,121 @@+{- |+Copyright   : (c) Takayuki Muranushi, 2016+License     : MIT+Maintainer  : muranushi@gmail.com+Stability   : experimental+++Provides a interactive printer for printing Unicode characters in ghci REPL. Our design goal is that 'uprint' produces String representations that are valid Haskell 'String' literals and uses as many Unicode printable characters as possible. Hence++@+read . ushow == id+@++see the tests of this package for detailed specifications.++__Example__++With 'print' :++@+$ __ghci__+...+Ok, modules loaded: Text.Show.Unicode.+> __["哈斯克尔7.6.1"]__+["\\21704\\26031\\20811\\23572\\&7.6.1"]+>+@++With 'uprint' :++@+$ __ghci -interactive-print=Text.Show.Unicode.uprint Text.Show.Unicode__+...+Ok, modules loaded: Text.Show.Unicode.+> __("Хорошо!",["哈斯克尔7.6.1的力量","感じる"])__+("Хорошо!",["哈斯克尔7.6.1的力量","感じる"])+> "改\\n行"+"改\\n行"+@++You can make 'uprint' the default interactive printer in several ways. One is to+@cabal install unicode-show@, and add the following lines to your @~/.ghci@ config file.++@+import qualified Text.Show.Unicode+:set -interactive-print=Text.Show.Unicode.uprint+@++-}+++module Text.Show.Unicode (ushow, uprint, ushowWith, uprintWith) where++import Control.Applicative ((<$>), (<$), (<|>))+import GHC.Show (showLitChar)+import GHC.Read (readLitChar)+import Data.Char(isPrint)+import Text.ParserCombinators.ReadP++++type Replacement = (String, String)++-- | Parse a value of type 'a', toghether with the original representation.+-- This is needed because a quotation mark character can be represented in two ways ---+-- @"@ or @\\"@ , and we'd like to preserve both representations.++readsWithMatch :: ReadS a -> ReadS (a, String)+readsWithMatch parser input =+  [ ((ret, take (length input - length leftover) input), leftover)+  | (ret, leftover) <- parser input]++-- | Parse one Haskell character literal expression from a 'String' produced by 'show', and+--+--  * If the found char satisfies the predicate, replace the literal string with the character itself.+--  * Otherwise, leave the string as it was.+--  * Note that special delimiter sequence "\&" may appear in a string. c.f.  <https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-200002.6 Section 2.6 of the Haskell 2010 specification>.++recoverChar :: (Char -> Bool) -> ReadP Replacement+recoverChar p = (represent <$> readS_to_P (readsWithMatch readLitChar)) <|> (("\\&","\\&") <$ string "\\&")+  where+    represent :: (Char, String) -> Replacement+    represent (c,original) | p c  = (original, [c])+    represent (_,original)        = (original, original)++-- | Parse many Haskell character literals from the input,+-- and concatenate them.+reparse :: (Char -> Bool) -> ReadP String+reparse p = cat2 ("","") <$> many (recoverChar p)+  where+    -- concatenate while removing redundant separator.+    cat2 :: Replacement -> [Replacement] -> String+    cat2 _ [] = ""+    cat2 (pb,pa) ((xb,xa):xs)+      | pb /= pa && xb == "\\&"  =       cat2 (xb,xa) xs+      | otherwise                = xa ++ cat2 (xb,xa) xs+++-- | Show the input, and then replace Haskell character literals+-- with the character it represents, for any Unicode printable characters except backslash, single and double quotation marks.+-- If something fails, fallback to standard 'show'.+ushow :: Show a => a -> String+ushow = ushowWith (\c -> isPrint c && not (c `elem` ['\\', '\'','\"'] ))++-- | A version of 'print' that uses 'ushow'.+uprint :: Show a => a -> IO ()+uprint = putStrLn . ushow+++-- | Show the input, and then replace character literals+-- with the character itself, for characters that satisfy the given predicate.+ushowWith :: Show a => (Char -> Bool) -> a -> String+ushowWith p x = let showx = show x in case readP_to_S (reparse p) $ showx of+           [] -> showx+           ys -> case last ys of+             (ret,"") -> ret+             _        -> showx++-- | A version of 'print' that uses 'ushowWith'.+uprintWith :: Show a => (Char -> Bool) -> a -> IO ()+uprintWith p = putStrLn . ushowWith p
+ test/Spec.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE ScopedTypeVariables #-}+++import Test.Framework (defaultMain, testGroup)+import Test.Framework.Providers.API (Test)+import Test.Framework.Providers.HUnit (testCase)+import Test.Framework.Providers.QuickCheck2 (testProperty)+import Test.HUnit.Base hiding (Test)++import Text.Show.Unicode hiding (ushow)+++++ushow :: Show a => a -> String+ushow x = go (show x) where+      go :: String -> String+      go [] = []+      go s@(x:xs) = case x of+          '\"' -> '\"' : str ++ "\"" ++ go rest+          '\'' -> '\'' : char : '\'' : go rest'+          _    -> x : go xs+        where+          (str :: String, rest):_ = reads s+          (char :: Char, rest'):_ = reads s++data T試6験 = Å4 { すけろく :: String} deriving (Eq, Ord, Show, Read)+data T試7験 = String :\& String  deriving (Eq, Ord, Show, Read)++-- Derived instance of Read and Show produced by GHC+-- does not satisfy Haskell 2010 spec, for the moment (ghc 7.10.3).+-- Therefore, we must regretfully skip this test.+data T試8験 = String :@\& String  deriving (Eq, Ord, Show, Read)+++ushowTo :: Show a => a -> String -> Test+ushowTo f t = testCase ("ushow " ++ show f ++ " == " ++ t) $ t @=? ushow f++tests :: [Test]+tests =+  [ testGroup "individual representations test"+    [+      "漢6" `ushowTo` "\"漢6\""+    , "\na\ri\ta 国際空港" `ushowTo` "\"\\na\\ri\\ta 国際空港\""+    , "\SOH\SO\&H" `ushowTo` "\"\\SOH\\SO\\&H\""+    ]++  , testGroup "read . ushow == id"+    [ testProperty "read . ushow == id, for String" $+      \str -> read (ushow str) == (str :: String)+    , testProperty "read . read . ushow . ushow == id, for String" $+      \str -> (read $ read $ ushow $ ushow str) == (str :: String)+    , testProperty "read . ushow == id, for some crazy Unicode type" $+      \str -> let v = Å4 str in read (ushow v) == v+    , testProperty "read . ushow == id, for some crazy Unicode type" $+      \a b -> let v = a :\& b in read (ushow v) == v+--     , testProperty "read . ushow == id, for some crazy Unicode type" $+--       \a b -> let v = a :@\& b in read (show v) == v+    , testProperty "read . ushow == id, for compound type" $+      \str -> read (ushow str) == (str :: Either [String] (String,String))+    ]+  ]+main :: IO ()+main = defaultMain tests
+ unicode-show.cabal view
@@ -0,0 +1,45 @@+name:                unicode-show+version:             0.1.0.0+synopsis:            print and show in unicode+description:+            This package provides variants of 'show' and 'print' functions that does not escape non-ascii characters. Run ghci with  __@-interactive-print@__ flag to prints unicode characters. See <https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/interactive-evaluation.html#ghci-interactive-print Using a custom interactive printing function> section in the GHC manual.++++++homepage:            http://github.com/githubuser/unicode-show#readme+license:             BSD3+license-file:        LICENSE+author:              Takayuki Muranushi+maintainer:          muranushi@gmail.com+copyright:           2016 Takayuki Muranushi+category:            Text+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Text.Show.Unicode+  build-depends:       base >= 4.7 && < 5+  default-language:    Haskell2010++test-suite unicode-show-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , HUnit+                     , QuickCheck+                     , unicode-show+                     , test-framework+                     , test-framework-hunit+                     , test-framework-quickcheck2++  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/githubuser/unicode-show