on master help sign in
Make span slightly faster and simpler
f0158d3Bryan O'Sullivan
Data/Text.hs+7−5
@@ -1081,11 +1081,13 @@ splitAt n t@(Text arr off len)
1081-- of @t@ of elements that satisfy @p@, and whose second is the
1081-- of @t@ of elements that satisfy @p@, and whose second is the
1082-- remainder of the list.
1082-- remainder of the list.
1083span :: (Char -> Bool) -> Text -> (Text, Text)
1083span :: (Char -> Bool) -> Text -> (Text, Text)
1084−span p t@(Text arr off len) = (textP arr off k, textP arr (off+k) (len-k))
1084+span p t@(Text arr off len) = (hd,tl)
1085+ where hd = textP arr off k
1086+ tl = textP arr (off+k) (len-k)
1085− where k = loop 0
1087+ !k = loop 0
1086− loop !i | i >= len || not (p c) = i
1088+ loop !i | i < len && p c = loop (i+d)
1087− | otherwise = loop (i+d)
1089+ | otherwise = i
1088− where Iter c d = iter t i
1090+ where Iter c d = iter t i
1089{-# INLINE span #-}
1091{-# INLINE span #-}
1090
1092
1091-- | /O(n)/ 'break' is like 'span', but the prefix returned is
1093-- | /O(n)/ 'break' is like 'span', but the prefix returned is