Bryan O'Sullivan avatar Bryan O'Sullivan committed d05c4be

Refactor the helper function from break up to the top level.

Comments (0)

Files changed (1)


 import qualified Data.Text.Fusion.Common as S
 import qualified Data.Text.Unsafe as T
 import qualified Data.Text.Lazy.Fusion as S
+import Data.Text.Fusion.Internal (PairS(..))
 import Data.Text.Lazy.Fusion (stream, unstream)
 import Data.Text.Lazy.Internal (Text(..), chunk, empty, foldlChunks, foldrChunks)
 import Data.Text.Internal (textP)
                            in (Chunk t ts', ts'')
              where len = fromIntegral (T.length t)
+-- | /O(n)/ 'splitAtWord' @n t@ returns a strict pair whose first
+-- element is a prefix of @t@ whose chunks contain @n@ 'Word16'
+-- values, and whose second is the remainder of the string.
+splitAtWord :: Int64 -> Text -> PairS Text Text
+splitAtWord _ Empty = empty :*: empty
+splitAtWord x (Chunk c@(T.Text arr off len) cs)
+    | y >= len  = let h :*: t = splitAtWord (x-fromIntegral len) cs
+                  in  Chunk c h :*: t
+    | otherwise = chunk (textP arr off y) empty :*:
+                  chunk (textP arr (off+y) (len-y)) cs
+    where y = fromIntegral x
 -- | /O(n+m)/ Find the first instance of @needle@ (which must be
 -- non-'null') in @haystack@.  The first element of the returned tuple
 -- is the prefix of @haystack@ before @needle@ is matched.  The second
     | null pat  = emptyError "break"
     | otherwise = case indices pat src of
                     []    -> (src, empty)
-                    (x:_) -> go x src
-  where
-    go _ Empty = (empty, empty)
-    go x (Chunk c@(T.Text arr off len) cs)
-       | y >= len  = let (h,t) = go (x-fromIntegral len) cs
-                     in (Chunk c h, t)
-       | otherwise = (chunk (textP arr off y) empty,
-                      chunk (textP arr (off+y) (len-y)) cs)
-       where y = fromIntegral x
+                    (x:_) -> let h :*: t = splitAtWord x src
+                             in  (h, t)
 -- | /O(n)/ 'breakBy' is like 'spanBy', but the prefix returned is over
 -- elements that fail the predicate @p@.
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
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.