ascii (empty) → 0.0.0
raw patch · 4 files changed
+148/−0 lines, 4 filesdep +basedep +bytestringdep +textsetup-changed
Dependencies added: base, bytestring, text
Files
- Data/Ascii.hs +97/−0
- LICENSE +30/−0
- Setup.hs +3/−0
- ascii.cabal +18/−0
+ Data/Ascii.hs view
@@ -0,0 +1,97 @@+{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}+module Data.Ascii+ ( -- * Datatypes+ Ascii+ , CIAscii+ -- * Construction+ -- ** Safe+ , fromByteString+ , fromChars+ , fromText+ -- ** Unsafe+ , unsafeFromByteString+ , unsafeFromString+ , unsafeFromText+ -- * Extraction+ , toByteString+ , toString+ , toText+ -- * Case insensitive+ , toCIAscii+ , fromCIAscii+ , ciToByteString+ ) where++import Data.ByteString (ByteString)+import qualified Data.ByteString as S+import qualified Data.ByteString.Char8 as S8+import Data.Char (toLower, isAscii)+import Data.String (IsString (..))+import Data.Data (Data)+import Data.Typeable (Typeable)+import Data.Text (Text)+import qualified Data.Text as T+import qualified Data.Text.Encoding as TE++newtype Ascii = Ascii ByteString+ deriving (Show, Eq, Read, Ord, Data, Typeable, IsString)++data CIAscii = CIAscii+ { ciOriginal :: !ByteString+ , ciLowerCase :: !ByteString+ }+ deriving (Data, Typeable)++instance Show CIAscii where+ show = show . ciOriginal+instance Read CIAscii where+ readsPrec i = map (\(x, y) -> (toCIAscii x, y)) . readsPrec i+instance Eq CIAscii where+ x == y = ciLowerCase x == ciLowerCase y+instance Ord CIAscii where+ x <= y = ciLowerCase x <= ciLowerCase y+instance IsString CIAscii where+ fromString = toCIAscii . unsafeFromString++fromByteString :: ByteString -> Maybe Ascii+fromByteString bs+ | S.all (< 128) bs = Just $ Ascii bs+ | otherwise = Nothing++-- | Renamed to avoid clash with 'fromString'+fromChars :: String -> Maybe Ascii+fromChars s+ | all isAscii s = Just $ Ascii $ S8.pack s+ | otherwise = Nothing++fromText :: Text -> Maybe Ascii+fromText t+ | T.all isAscii t = Just $ Ascii $ TE.encodeUtf8 t+ | otherwise = Nothing++unsafeFromByteString :: ByteString -> Ascii+unsafeFromByteString = Ascii++unsafeFromString :: String -> Ascii+unsafeFromString = Ascii . S8.pack++unsafeFromText :: Text -> Ascii+unsafeFromText = Ascii . TE.encodeUtf8++toCIAscii :: Ascii -> CIAscii+toCIAscii (Ascii bs) = CIAscii bs $ S8.map toLower bs++fromCIAscii :: CIAscii -> Ascii+fromCIAscii = Ascii . ciOriginal++toByteString :: Ascii -> ByteString+toByteString (Ascii bs) = bs++toString :: Ascii -> String+toString (Ascii bs) = S8.unpack bs++toText :: Ascii -> Text+toText (Ascii bs) = TE.decodeASCII bs++ciToByteString :: CIAscii -> ByteString+ciToByteString = ciOriginal
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Michael Snoyman 2011++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 Michael Snoyman 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,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ ascii.cabal view
@@ -0,0 +1,18 @@+Name: ascii+Version: 0.0.0+Synopsis: Type-safe, bytestring-based ASCII values.+License: BSD3+License-file: LICENSE+Author: Michael Snoyman+Maintainer: michael@snoyman.com+Stability: Stable+Category: Data+Build-type: Simple+Cabal-version: >=1.2++Library+ Exposed-modules: Data.Ascii+ Build-depends: base >= 4 && < 5+ , bytestring >= 0.9 && < 0.10+ , text >= 0.11 && < 0.12+ Ghc-options: -Wall