text
text copied to clipboard
Add function that remembers whitespace
It would be nice to have a function like words/lines that remembers the relevant whitespace information. One implementation (which I haven't tested very thoroughly and is probably not the best one could do) is as follows:
wsSplit :: Text -> [Either Text Text]
wsSplit t@(Text arr off len)
| Text.null t = []
| isSpace (Text.head t) = loop 0 0 True
| otherwise = loop 0 0 False
where
loop !start !n !wasSpace
| n >= len = if wasSpace
then [Left $ Text arr (start+off) (n-start)]
else [Right $ Text arr (start+off) (n-start)]
| isSpace c && wasSpace = loop start (n + d) True
| wasSpace = Left (Text arr (start+off) (n-start)) : loop n (n+d) False
| isSpace c = Right (Text arr (start+off) (n-start)) : loop n (n+d) True
| otherwise = loop start (n+d) False
where Iter c d = iter t n
> wsSplit "hello \t \n there! \n"
[Right "hello",Left " \t \n ",Right "there!",Left " \n"]