Ver Fonte

Initial version

Marcos Dumay de Medeiros há 9 anos atrás
commit
3291295fe1
5 ficheiros alterados com 83 adições e 0 exclusões
  1. 7 0
      .gitignore
  2. 20 0
      LICENSE
  3. 2 0
      Setup.hs
  4. 25 0
      istext.cabal
  5. 29 0
      src/Data/Text/IsText.hs

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+dist/
+.cabal-sandbox/
+cabal.sandbox.config
+*~
+**/*~
+
+

+ 20 - 0
LICENSE

@@ -0,0 +1,20 @@
+Copyright (c) 2016 Marcos Dumay de Medeiros
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 2 - 0
Setup.hs

@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain

+ 25 - 0
istext.cabal

@@ -0,0 +1,25 @@
+-- Initial istext.cabal generated by cabal init.  For further 
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name:                istext
+version:             0.1.0.0
+synopsis:            IsText type class for data that can be converted to and from Strings.
+-- description:         
+homepage:            https://sealgram.com/git/haskell/text-like/
+license:             MIT
+license-file:        LICENSE
+author:              Marcos Dumay de Medeiros
+maintainer:          marcos@marcosdumay.com
+-- copyright:           
+category:            Text
+build-type:          Simple
+-- extra-source-files:  
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     Data.Text.IsText
+  -- other-modules:       
+  other-extensions:    TypeSynonymInstances, FlexibleInstances
+  build-depends:       base >=4.7 && <4.8, bytestring >=0.10 && <0.11, utf8-string >=1 && <1.1, text >=1.2 && <1.3
+  hs-source-dirs:      src
+  default-language:    Haskell2010

+ 29 - 0
src/Data/Text/IsText.hs

@@ -0,0 +1,29 @@
+{-# LANGUAGE TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
+
+module Data.Text.IsText where
+
+import Data.String
+import qualified Data.ByteString as SB
+import qualified Data.ByteString.Lazy as LB
+import qualified Codec.Binary.UTF8.String as UTF8
+import qualified Data.Text as T
+
+{- | Type class for data structures that are logically text -}
+class IsString a => IsText a where
+  toString :: a -> String
+
+instance IsText String where
+  toString = id
+{- | The UTF-8 encoding is assumed for ByteStrings -}
+instance IsText SB.ByteString where
+  toString = UTF8.decode . SB.unpack
+{- | The UTF-8 encoding is assumed for ByteStrings -}
+instance IsText LB.ByteString where
+  toString = UTF8.decode . LB.unpack
+instance IsText T.Text where
+  toString = T.unpack
+
+{- | Converts between instances of IsText -}
+fromText :: (IsText a, IsText b) => a -> b
+fromText = fromString . toString