haskell / haskell/text

Add function that remembers whitespace

Open
#294 0 comments 0 reactions 0 assignees View on GitHub
feature request
Dominant language
Haskell
Stars
421
Forks
163
PR merge metrics
No merged PRs in 30d

Description

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:

```haskell
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
```

```terminal
> wsSplit "hello \t \n there! \n"
[Right "hello",Left " \t \n ",Right "there!",Left " \n"]
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.