text icon indicating copy to clipboard operation
text copied to clipboard

Add function that remembers whitespace

Open Boarders opened this issue 5 years ago • 0 comments

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"]

Boarders avatar Jul 26 '20 14:07 Boarders