Commits

Anonymous committed 4206508

implement strict Text to Builder encoder using BoundedEncodings

A first test of the infrastructure can be found in my 'aeson' branch.

  • Participants
  • Parent commits 0ad243a

Comments (0)

Files changed (2)

File Data/Text/Encoding.hs

     , encodeUtf16BE
     , encodeUtf32LE
     , encodeUtf32BE
+    
+    -- * Generic encoding of Text
+    , encodeStreamWithB
+    , encodeTextWithB
     ) where
 
 import Control.Exception (evaluate, try)
 import Data.Bits ((.&.))
 import Data.ByteString as B
 import Data.ByteString.Internal as B
+import Data.ByteString.Lazy.Builder.Internal as B
+import Data.ByteString.Lazy.Builder.BasicEncoding.Internal as B
 import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
 import Data.Text.Internal (Text(..), textP)
 import Data.Text.UnsafeChar (ord, unsafeWrite)
 foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
     :: MutableByteArray# s -> Ptr CSize
     -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
+
+-- | Encode all elements of a 'F.Stream' using a 'B.BoundedEncoding'.
+{-# INLINE encodeStreamWithB #-}
+encodeStreamWithB :: B.BoundedEncoding a -> F.Stream a -> B.Builder
+encodeStreamWithB be = 
+    \(F.Stream next s0 _) -> B.builder $ step next s0
+  where
+    bound = B.sizeBound be
+    step next s0 k (B.BufferRange op0 ope0) = 
+        go s0 op0
+      where
+        go s !op = case next s of
+          F.Done       -> k (B.BufferRange op ope0)
+          F.Skip s'    -> go s' op
+          F.Yield x s'
+            | op `plusPtr` bound <= ope0 -> B.runB be x op >>= go s'
+            | otherwise                  -> 
+                return $ B.bufferFull bound op (step next s k)
+
+
+-- | 
+-- | /Subject to fusion./
+-- Encode all 'Char's of a 'T.Text' using a 'B.BoundedEncoding'.
+{-# INLINE encodeTextWithB #-}
+encodeTextWithB :: B.BoundedEncoding Char -> Text -> B.Builder
+encodeTextWithB be = encodeStreamWithB be . F.stream
 name:           text
-version:        0.11.1.12
+version:        0.11.2.0
 homepage:       https://github.com/bos/text
 bug-reports:    https://github.com/bos/text/issues
 synopsis:       An efficient packed Unicode text type.
   build-depends:
     array,
     base       < 5,
-    bytestring >= 0.9 && < 1.0
+    bytestring >= 0.10 && < 1.0
   if impl(ghc >= 6.10)
     build-depends:
       ghc-prim, base >= 4, deepseq >= 1.1.0.0
     -DASSERTS -DHAVE_DEEPSEQ
 
   build-depends:
-    base                       >= 4   && < 5,
-    bytestring                 >= 0.9 && < 0.10,
+    base                       >= 4    && < 5,
+    bytestring                 >= 0.10 && < 1.0,
     deepseq                    >= 1.1,
-    directory                  >= 1.0 && < 1.2,
-    random                     >= 1.0 && < 1.1,
-    QuickCheck                 >= 2.4 && < 2.5,
-    HUnit                      >= 1.2 && < 1.3,
-    test-framework             >= 0.4 && < 0.5,
-    test-framework-quickcheck2 >= 0.2 && < 0.3,
-    test-framework-hunit       >= 0.2 && < 0.3
+    directory                  >= 1.0  && < 1.2,
+    random                     >= 1.0  && < 1.1,
+    QuickCheck                 >= 2.4  && < 2.5,
+    HUnit                      >= 1.2  && < 1.3,
+    test-framework             >= 0.4  && < 0.5,
+    test-framework-quickcheck2 >= 0.2  && < 0.3,
+    test-framework-hunit       >= 0.2  && < 0.3
 
 source-repository head
   type:     git