readable-0.3.1/0000755000000000000000000000000012465520545011450 5ustar0000000000000000readable-0.3.1/README.md0000644000000000000000000000052412465520545012730 0ustar0000000000000000Readable ======== The Read type class is very useful for building data types from String representations. But String has high overhead, so sometimes it isn't suitable for applications where space usage and performance are important. This library provides a simpler version of Read's functionality for Text and UTF8 encoded ByteStrings. readable-0.3.1/LICENSE0000644000000000000000000000271212465520545012457 0ustar0000000000000000Copyright (c) 2012, Doug Beardsley 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 the Snap Framework authors nor the names of its 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 HOLDER 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. readable-0.3.1/readable.cabal0000644000000000000000000000162312465520545014175 0ustar0000000000000000name: readable version: 0.3.1 synopsis: Reading from Text and ByteString description: Provides a Readable type class for reading data types from ByteString and Text. Also includes efficient implementations for common data types. license: BSD3 license-file: LICENSE author: Doug Beardsley maintainer: mightybyte@gmail.com build-type: Simple cabal-version: >= 1.6 homepage: https://github.com/mightybyte/readable category: Text extra-source-files: CHANGELOG.md LICENSE, README.md, Setup.hs Library hs-source-dirs: src exposed-modules: Data.Readable build-depends: base >= 4 && < 5, bytestring >= 0.9 && < 0.11, text >= 0.11 && < 1.3 ghc-prof-options: -prof -auto-all ghc-options: -Wall -fwarn-tabs source-repository head type: git location: git://github.com/mightybyte/readable.git readable-0.3.1/Setup.hs0000644000000000000000000000005712465520545013106 0ustar0000000000000000import Distribution.Simple main = defaultMain readable-0.3.1/CHANGELOG.md0000644000000000000000000000115412465520545013262 0ustar00000000000000000.4 --- * Reinstate Text and ByteString instances because some users require them. 0.3.0.1 ------- * Add yes/no and y/n parsing to the Bool instance 0.3 --- * Remove Readable instances for ByteString and Text because they are probably not what the user wants and could cause compilation to succeed when you probably want to see an error. 0.2.0.2 ------- * Fix fromBS for ByteString so it is a correct passthrough instead of doing encodeUtf8 . decodeUtf8 under the hood. 0.2.0.1 ------- * Use decodeUtf8' so that we can catch character encoding exceptions and return mzero instead of generating an exception. readable-0.3.1/src/0000755000000000000000000000000012465520545012237 5ustar0000000000000000readable-0.3.1/src/Data/0000755000000000000000000000000012465520545013110 5ustar0000000000000000readable-0.3.1/src/Data/Readable.hs0000644000000000000000000000734312465520545015152 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-| The Read type class is very useful for building data types from String representations. But String has high overhead, so sometimes it isn't suitable for applications where space usage and performance are important. This library provides a simpler version of Read's functionality for Text and ByteStrings. -} module Data.Readable ( Readable(..) ) where ------------------------------------------------------------------------------ import Control.Monad import Data.ByteString.Char8 (ByteString) import Data.Int import Data.Text (Text) import qualified Data.Text as T import Data.Text.Encoding import Data.Text.Read import Data.Word ------------------------------------------------------------------------------ -- | ByteString and Text reading using MonadPlus to handle parse failure. On -- error, fromText and fromBS will return mzero. You can use mplus to provide -- fallback defaults. class Readable a where -- | Reads data from a Text representation. fromText :: MonadPlus m => Text -> m a -- | Reads data from a UTF8 encoded ByteString. The default -- implementation of this function simply decodes with UTF-8 and then -- calls the fromText function. If decoding fails, mzero will be -- returned. You can provide your own implementation if you need -- different behavior such as not decoding to UTF8. fromBS :: MonadPlus m => ByteString -> m a fromBS = fromText <=< hushPlus . decodeUtf8' hushPlus :: MonadPlus m => Either a b -> m b hushPlus (Left _) = mzero hushPlus (Right b) = return b ------------------------------------------------------------------------------ -- | Fails if the input wasn't parsed completely. checkComplete :: MonadPlus m => (t, Text) -> m t checkComplete (a,rest) | T.null rest = return a | otherwise = mzero -- Leaving out these instances breaks users who depend on having a unified -- constraint for parsing, so we need to keep them around. instance Readable ByteString where fromText = return . encodeUtf8 fromBS = return instance Readable Text where fromText = return instance Readable Int where fromText = either (const mzero) checkComplete . signed decimal instance Readable Integer where fromText = either (const mzero) checkComplete . signed decimal instance Readable Float where fromText = either (const mzero) checkComplete . rational instance Readable Double where fromText = either (const mzero) checkComplete . double instance Readable Bool where fromText t = case T.toLower t of "1" -> return True "0" -> return False "t" -> return True "f" -> return False "true" -> return True "false" -> return False "y" -> return True "n" -> return False "yes" -> return True "no" -> return False _ -> mzero instance Readable Int8 where fromText = either (const mzero) checkComplete . signed decimal instance Readable Int16 where fromText = either (const mzero) checkComplete . signed decimal instance Readable Int32 where fromText = either (const mzero) checkComplete . signed decimal instance Readable Int64 where fromText = either (const mzero) checkComplete . signed decimal instance Readable Word8 where fromText = either (const mzero) checkComplete . decimal instance Readable Word16 where fromText = either (const mzero) checkComplete . decimal instance Readable Word32 where fromText = either (const mzero) checkComplete . decimal instance Readable Word64 where fromText = either (const mzero) checkComplete . decimal