Bryan O'Sullivan avatar 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.

Comments (0)

Files changed (4)

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 #-}

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
 

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

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.