Commits

Bryan O'Sullivan  committed a8fac16

Remove more duplicated Builder code

This time, I also changed the return value from UnsafeChar.unsafeWrite
to return the number of Word16 values written, instead of the next offset
in the array to use.

  • Participants
  • Parent commits 6764b20

Comments (0)

Files changed (4)

File Data/Text/Fusion.hs

                                let top' = (top + 1) `shiftL` 1
                                arr' <- A.unsafeNew top'
                                A.copy arr' arr >> outer arr' top' s i
-                | otherwise -> unsafeWrite arr i x >>= loop s'
+                | otherwise -> do d <- unsafeWrite arr i x
+                                  loop s' (i+d)
                 where j | ord x < 0x10000 = i
                         | otherwise       = i + 1
 {-# INLINE [0] unstream #-}

File Data/Text/Lazy/Builder.hs

 #endif
 import Control.Monad.ST (ST, runST)
 import Data.Bits ((.&.))
-import Data.Char (ord)
 import Data.Monoid (Monoid(..))
 import Data.Text.Internal (Text(..))
 import Data.Text.Lazy.Internal (smallChunkSize)
 import Data.Text.Unsafe (inlineInterleaveST)
+import Data.Text.UnsafeChar (ord, unsafeWrite)
 import Data.Text.UnsafeShift (shiftR)
 import Prelude hiding (map, putChar)
 
     return $! Buffer arr 0 0 size
 {-# INLINE newBuffer #-}
 
--- Write a character to the array, starting at the specified offset
--- @i@.  Returns the number of elements written.
-unsafeWrite :: A.MArray s -> Int -> Char -> ST s Int
-unsafeWrite marr i c
-    | n < 0x10000 = do
-#if defined(ASSERTS)
-        assert (i >= 0) . assert (i < A.length marr) $ return ()
-#endif
-        A.unsafeWrite marr i (fromIntegral n)
-        return 1
-    | otherwise = do
-#if defined(ASSERTS)
-        assert (i >= 0) . assert (i < A.length marr - 1) $ return ()
-#endif
-        A.unsafeWrite marr i lo
-        A.unsafeWrite marr (i+1) hi
-        return 2
-    where n = ord c
-          m = n - 0x10000
-          lo = fromIntegral $ (m `shiftR` 10) + 0xD800
-          hi = fromIntegral $ (m .&. 0x3FF) + 0xDC00
-{-# INLINE unsafeWrite #-}
-
 ------------------------------------------------------------------------
 -- Some nice rules for Builder
 

File Data/Text/Lazy/Fusion.hs

+{-# LANGUAGE BangPatterns #-}
 -- |
 -- Module      : Data.Text.Lazy.Fusion
 -- Copyright   : (c) 2009, 2010 Bryan O'Sullivan
                 Yield x s' -> I.Text arr 0 len `chunk` outer s''
                   where (arr,(s'',len)) = A.run2 fill
                         fill = do a <- A.unsafeNew unknownLength
-                                  i <- unsafeWrite a 0 x
-                                  inner a unknownLength s' i
+                                  unsafeWrite a 0 x >>= inner a unknownLength s'
                         unknownLength = 4
-    inner marr len s i
+    inner marr len s !i
         | i + 1 >= chunkSize = return (marr, (s,i))
         | i + 1 >= len       = do
             let newLen = min (len `shiftL` 1) chunkSize
             case next s of
               Done        -> return (marr,(s,i))
               Skip s'     -> inner marr len s' i
-              Yield x s'  -> unsafeWrite marr i x >>= inner marr len s'
+              Yield x s'  -> do d <- unsafeWrite marr i x
+                                inner marr len s' (i+d)
 {-# INLINE [0] unstreamChunks #-}
 
 -- | /O(n)/ Convert a 'Stream Char' into a 'Text', using

File Data/Text/UnsafeChar.hs

 unsafeChr32 (W32# w#) = C# (chr# (word2Int# w#))
 {-# INLINE unsafeChr32 #-}
 
+-- | Write a character into the array at the given offset.  Returns
+-- the number of 'Word16's written.
 unsafeWrite :: A.MArray s -> Int -> Char -> ST s Int
 unsafeWrite marr i c
     | n < 0x10000 = do
         assert (i >= 0) . assert (i < A.length marr) $ return ()
 #endif
         A.unsafeWrite marr i (fromIntegral n)
-        return $! i+1
+        return 1
     | otherwise = do
 #if defined(ASSERTS)
         assert (i >= 0) . assert (i < A.length marr - 1) $ return ()
 #endif
         A.unsafeWrite marr i lo
         A.unsafeWrite marr (i+1) hi
-        return $! i+2
+        return 2
     where n = ord c
           m = n - 0x10000
           lo = fromIntegral $ (m `shiftR` 10) + 0xD800