packages feed

smallstring 0.1.0 → 0.2.0

raw patch · 2 files changed

+63/−13 lines, 2 filesdep +bytestringdep +text

Dependencies added: bytestring, text

Files

Data/SmallString.hs view
@@ -2,9 +2,15 @@  {-| - The SmallString type is for storing small identifiers. We do not provide fast operations- on strings - what we offer is low memory overhead.+ An immutable Unicode text type, optimized for low memory overhead.  A+ 'SmallString' typically uses less memory than the corresponding 'T.Text'.  Use+ 'SmallString' when storing a large number of short texts, for example when+ indexing a map using words or short phrases. + To manipulate a 'SmallString', first convert it into a 'T.Text'.  For more+ information on working with 'T.Text', see the @text@ package:+ http://hackage.haskell.org/package/text+  The Ord instance is not guaranteed to be the same as that of the corresponding  string. @@ -14,17 +20,28 @@     ( SmallString     , fromString     , toString+    , fromText+    , toText     ) where  import qualified Data.SmallArray as A+import qualified Data.SmallArray.Unsafe as A+ import qualified Codec.Binary.UTF8.String as UTF8 import Data.Word (Word8)+import qualified Data.String as S ( IsString(..) ) +import qualified Data.Text as T+import qualified Data.Text.Encoding as T+import qualified Data.ByteString as B+import qualified Data.ByteString.Internal as B+import qualified Data.ByteString.Unsafe as B+ import Control.DeepSeq+import Foreign.Ptr (Ptr, plusPtr)+import Foreign.Storable (poke) --- | A space efficient representation of text. This is like a strict ByteString, but--- with fewer features, and UTF preserving. Fow ASCII data, we're slightly smaller than--- ByteStrings for small strings.+-- | A space efficient representation of Unicode text. newtype SmallString = SmallString (A.Array Word8)  instance Eq SmallString where@@ -39,6 +56,9 @@ instance NFData SmallString where     rnf (SmallString arr) = rnf arr +instance S.IsString SmallString where+    fromString = fromString+ compareSmallString :: SmallString -> SmallString -> Ordering compareSmallString (SmallString lhsAry) (SmallString rhsAry)     = compare lhsAry rhsAry@@ -47,12 +67,42 @@ eqSmallString (SmallString lhs) (SmallString rhs)     = lhs == rhs --- | Convert a String into a SmallString.+-- | Convert a 'String' into a 'SmallString'. fromString :: String -> SmallString fromString     = SmallString . A.fromList . UTF8.encode --- | Convert a SmallString into a String.+-- | Convert a 'SmallString' into a 'String'. toString :: SmallString -> String toString (SmallString ary)     = UTF8.decode . A.toList $ ary++-- | Convert a 'T.Text' into a 'SmallString'.+fromText :: T.Text -> SmallString+fromText = fromBS . T.encodeUtf8++-- | Convert a 'SmallString' into a 'T.Text'.+toText :: SmallString -> T.Text+toText = T.decodeUtf8 . toBS++-- | Convert a 'B.ByteString' into a 'SmallString', assuming that the+-- 'B.ByteString' contains UTF-8 encoded text.  This assumption is not checked.+fromBS :: B.ByteString -> SmallString+fromBS bs = SmallString $+    let len = B.length bs+    in A.run $ do+      arr <- A.unsafeNew len+      mapM_ (\ix -> A.unsafeWrite arr ix (B.unsafeIndex bs ix)) [0 .. len]+      return arr++-- | Convert a 'SmallString' into a 'B.ByteString'.  The 'B.ByteString' will+-- contain UTF-8 encoded text.+toBS :: SmallString -> B.ByteString+toBS (SmallString ary) = B.unsafeCreate len (go ary 0)+  where+    len = A.length ary+    go :: A.Array Word8 -> Int -> Ptr Word8 -> IO ()+    go !ary !i !p+        | i < len = do poke p (A.unsafeIndex ary i)+                       go ary (i + 1) (p `plusPtr` 1)+        | otherwise = return ()
smallstring.cabal view
@@ -1,11 +1,11 @@ Name:                smallstring-Version:             0.1.0-Synopsis:            A string type optimized for size in memory-Description:         A string type offering very little memory overhead. No-                     string manipulation functions are offered.+Version:             0.2.0+Synopsis:            A Unicode text type, optimized for low memory overhead+Description:         A Unicode text type, optimized for low memory overhead.+                     No string manipulation functions are offered.                      .                      Equality and ordinal comparison aim to be competitive with-                     the String type.+                     the Text type. Homepage:            http://community.haskell.org/~aslatter/code/smallstring/ License:             BSD3 License-file:        LICENSE@@ -17,5 +17,5 @@  Library   Build-depends:       base == 4.*, utf8-string == 0.3.*, smallarray == 0.1.*,-                       deepseq == 1.1.*+                       deepseq == 1.1.*, bytestring == 0.9.*, text == 0.11.*   Exposed-modules:     Data.SmallString