on master help sign in
implement strict Text to Builder encoder using BoundedEncodings
fdb303dSimon Meier
Data/Text/Encoding.hs+32
@@ -45,6 +45,10 @@ module Data.Text.Encoding
45 , encodeUtf16BE
45 , encodeUtf16BE
46 , encodeUtf32LE
46 , encodeUtf32LE
47 , encodeUtf32BE
47 , encodeUtf32BE
48+
49+ -- * Generic encoding of Text
50+ , encodeStreamWithB
51+ , encodeTextWithB
48 ) where
52 ) where
49
53
50import Control.Exception (evaluate, try)
54import Control.Exception (evaluate, try)
@@ -56,6 +60,8 @@ import Control.Monad.ST (unsafeIOToST, unsafeSTToIO)
56import Data.Bits ((.&.))
60import Data.Bits ((.&.))
57import Data.ByteString as B
61import Data.ByteString as B
58import Data.ByteString.Internal as B
62import Data.ByteString.Internal as B
63+import Data.ByteString.Lazy.Builder.Internal as B
64+import Data.ByteString.Lazy.Builder.BasicEncoding.Internal as B
59import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
65import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60import Data.Text.Internal (Text(..), textP)
66import Data.Text.Internal (Text(..), textP)
61import Data.Text.UnsafeChar (ord, unsafeWrite)
67import Data.Text.UnsafeChar (ord, unsafeWrite)
@@ -278,3 +284,29 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
278foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
284foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
279 :: MutableByteArray# s -> Ptr CSize
285 :: MutableByteArray# s -> Ptr CSize
280 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
286 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
287+
288+-- | Encode all elements of a 'F.Stream' using a 'B.BoundedEncoding'.
289+{-# INLINE encodeStreamWithB #-}
290+encodeStreamWithB :: B.BoundedEncoding a -> F.Stream a -> B.Builder
291+encodeStreamWithB be =
292+ \(F.Stream next s0 _) -> B.builder $ step next s0
293+ where
294+ bound = B.sizeBound be
295+ step next s0 k (B.BufferRange op0 ope0) =
296+ go s0 op0
297+ where
298+ go s !op = case next s of
299+ F.Done -> k (B.BufferRange op ope0)
300+ F.Skip s' -> go s' op
301+ F.Yield x s'
302+ | op `plusPtr` bound <= ope0 -> B.runB be x op >>= go s'
303+ | otherwise ->
304+ return $ B.bufferFull bound op (step next s k)
305+
306+
307+-- |
308+-- | /Subject to fusion./
309+-- Encode all 'Char's of a 'T.Text' using a 'B.BoundedEncoding'.
310+{-# INLINE encodeTextWithB #-}
311+encodeTextWithB :: B.BoundedEncoding Char -> Text -> B.Builder
312+encodeTextWithB be = encodeStreamWithB be . F.stream
text.cabal+11−11
@@ -1,5 +1,5 @@
1name: text
1name: text
2−version: 0.11.1.12
2+version: 0.11.2.0
3homepage: https://github.com/bos/text
3homepage: https://github.com/bos/text
4bug-reports: https://github.com/bos/text/issues
4bug-reports: https://github.com/bos/text/issues
5synopsis: An efficient packed Unicode text type.
5synopsis: An efficient packed Unicode text type.
@@ -111,7 +111,7 @@ library
111 build-depends:
111 build-depends:
112 array,
112 array,
113 base < 5,
113 base < 5,
114− bytestring >= 0.9 && < 1.0
114+ bytestring >= 0.10 && < 1.0
115 if impl(ghc >= 6.10)
115 if impl(ghc >= 6.10)
116 build-depends:
116 build-depends:
117 ghc-prim, base >= 4, deepseq >= 1.1.0.0
117 ghc-prim, base >= 4, deepseq >= 1.1.0.0
@@ -149,16 +149,16 @@ test-suite tests
149 -DASSERTS -DHAVE_DEEPSEQ
149 -DASSERTS -DHAVE_DEEPSEQ
150
150
151 build-depends:
151 build-depends:
152− base >= 4 && < 5,
152+ base >= 4 && < 5,
153− bytestring >= 0.9 && < 0.10,
153+ bytestring >= 0.10 && < 1.0,
154 deepseq >= 1.1,
154 deepseq >= 1.1,
155− directory >= 1.0 && < 1.2,
155+ directory >= 1.0 && < 1.2,
156− random >= 1.0 && < 1.1,
156+ random >= 1.0 && < 1.1,
157− QuickCheck >= 2.4 && < 2.5,
157+ QuickCheck >= 2.4 && < 2.5,
158− HUnit >= 1.2 && < 1.3,
158+ HUnit >= 1.2 && < 1.3,
159− test-framework >= 0.4 && < 0.5,
159+ test-framework >= 0.4 && < 0.5,
160− test-framework-quickcheck2 >= 0.2 && < 0.3,
160+ test-framework-quickcheck2 >= 0.2 && < 0.3,
161− test-framework-hunit >= 0.2 && < 0.3
161+ test-framework-hunit >= 0.2 && < 0.3
162
162
163source-repository head
163source-repository head
164 type: git
164 type: git