on master help sign in
implement strict Text to Builder encoder using BoundedEncodings

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

commitfdb303d72b825028b14361d25f842f2c312666d7 authorSimon Meier <iridcode@gmail.com> dateSat 28 Jan 2012, 00:12 (15 years ago) parent073662c
2 files changed+43−11
Data/Text/Encoding.hs+32 text.cabal+11−11
Data/Text/Encoding.hs+32
@@ -45,6 +45,10 @@ module Data.Text.Encoding
4545 , encodeUtf16BE
4646 , encodeUtf32LE
4747 , encodeUtf32BE
48+
49+ -- * Generic encoding of Text
50+ , encodeStreamWithB
51+ , encodeTextWithB
4852 ) where
4953
5054import Control.Exception (evaluate, try)
@@ -56,6 +60,8 @@ import Control.Monad.ST (unsafeIOToST, unsafeSTToIO)
5660import Data.Bits ((.&.))
5761import Data.ByteString as B
5862import Data.ByteString.Internal as B
63+import Data.ByteString.Lazy.Builder.Internal as B
64+import Data.ByteString.Lazy.Builder.BasicEncoding.Internal as B
5965import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
6066import Data.Text.Internal (Text(..), textP)
6167import Data.Text.UnsafeChar (ord, unsafeWrite)
@@ -278,3 +284,29 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
278284foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
279285 :: MutableByteArray# s -> Ptr CSize
280286 -> 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 @@
11name: text
2−version: 0.11.1.12
2+version: 0.11.2.0
33homepage: https://github.com/bos/text
44bug-reports: https://github.com/bos/text/issues
55synopsis: An efficient packed Unicode text type.
@@ -111,7 +111,7 @@ library
111111 build-depends:
112112 array,
113113 base < 5,
114− bytestring >= 0.9 && < 1.0
114+ bytestring >= 0.10 && < 1.0
115115 if impl(ghc >= 6.10)
116116 build-depends:
117117 ghc-prim, base >= 4, deepseq >= 1.1.0.0
@@ -149,16 +149,16 @@ test-suite tests
149149 -DASSERTS -DHAVE_DEEPSEQ
150150
151151 build-depends:
152− base >= 4 && < 5,
153− bytestring >= 0.9 && < 0.10,
152+ base >= 4 && < 5,
153+ bytestring >= 0.10 && < 1.0,
154154 deepseq >= 1.1,
155− directory >= 1.0 && < 1.2,
156− random >= 1.0 && < 1.1,
157− QuickCheck >= 2.4 && < 2.5,
158− HUnit >= 1.2 && < 1.3,
159− test-framework >= 0.4 && < 0.5,
160− test-framework-quickcheck2 >= 0.2 && < 0.3,
161− test-framework-hunit >= 0.2 && < 0.3
155+ directory >= 1.0 && < 1.2,
156+ random >= 1.0 && < 1.1,
157+ QuickCheck >= 2.4 && < 2.5,
158+ HUnit >= 1.2 && < 1.3,
159+ test-framework >= 0.4 && < 0.5,
160+ test-framework-quickcheck2 >= 0.2 && < 0.3,
161+ test-framework-hunit >= 0.2 && < 0.3
162162
163163source-repository head
164164 type: git