readable (empty) → 0.1
raw patch · 5 files changed
+153/−0 lines, 5 filesdep +basedep +bytestringdep +textsetup-changed
Dependencies added: base, bytestring, text
Files
- LICENSE +27/−0
- README.md +9/−0
- Setup.hs +3/−0
- readable.cabal +39/−0
- src/Data/Readable.hs +75/−0
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (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.
+ README.md view
@@ -0,0 +1,9 @@+Readable+========++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.+
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ readable.cabal view
@@ -0,0 +1,39 @@+name: readable+version: 0.1+synopsis: Reading from Text and ByteString++description:+ Provides a Readable type class for reading data types from ByteString and+ Text.++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:+ LICENSE,+ README.md,+ Setup.hs++Library+ hs-source-dirs: src++ exposed-modules:+ Data.Readable++ build-depends:+ base >= 4 && < 5,+ bytestring >= 0.9 && < 0.10,+ text >= 0.11 && < 0.12++ ghc-prof-options: -prof -auto-all+ ghc-options: -Wall -fwarn-tabs++source-repository head+ type: git+ location: git://github.com/mightybyte/readable.git
+ src/Data/Readable.hs view
@@ -0,0 +1,75 @@+{-|++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.++-}++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.+ fromBS :: MonadPlus m => ByteString -> m a+ fromBS = fromText . decodeUtf8+++------------------------------------------------------------------------------+-- | 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+++instance Readable ByteString where+ fromText = return . encodeUtf8+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 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