@@ -95,7 +95,7 @@ module Data.Text
9595 , drop
9696 , takeWhile
9797 , dropWhile
98− -- , splitAt
98+ , splitAt
9999 -- , span
100100 -- , break
101101 -- , group
@@ -633,6 +633,22 @@ dropWhile p (Text arr off len) = loop off 0
633633 unstream (S.dropWhile p (stream t)) = dropWhile p t
634634 #-}
635635
636+-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
637+-- prefix of @t@ of length @n@ and second element is the remainder of
638+-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
639+splitAt :: Int -> Text -> (Text, Text)
640+splitAt n t@(Text arr off len)
641+ | n <= 0 = (empty, t)
642+ | n >= len = (t, empty)
643+ | otherwise = (Text arr off k, Text arr (off+k) (len-k))
644+ where k = loop off 0
645+ end = off + len
646+ loop !i !count
647+ | i >= end || count >= n = i - off
648+ | otherwise = loop (i+d) (count+1)
649+ where d = iter_ arr i
650+{-# INLINE splitAt #-}
651+
636652-- | /O(n)/ Return all initial segments of the given 'Text', shortest
637653-- first.
638654inits :: Text -> [Text]