text icon indicating copy to clipboard operation
text copied to clipboard

Add spanEnd and breakEnd to Data.Text

Open TheMatten opened this issue 4 years ago • 16 comments

Provides part of #244 for Data.Text.

TheMatten avatar Mar 02 '21 22:03 TheMatten

This looks good to me. Would any other @haskell/text maintainers like to chime in? Let's merge this with one more approval. This PR seems to actually close #244, or did I miss something else in that issue?

Lysxia avatar Mar 15 '21 15:03 Lysxia

It doesn't actually add corresponding functions to Data.Text.Lazy - while I guess that doesn't really matter for this specific PR, it means that #244 isn't technically fully resolved. I can try to add those if you're interested, but I imagine they are going to be slightly less trivial because of list-like nature of lazy Text.

TheMatten avatar Mar 15 '21 17:03 TheMatten

Looking at breakOnEnd, would similar implementation using reverse make sense? I'm not familiar enough with performance characteristics of text to tell whether it would break existing optimizations.

TheMatten avatar Mar 15 '21 17:03 TheMatten

I've locally prepared commit with

-- | /O(n)/ Similar to 'break', but searches from the end of the string.
--
-- >>> T.breakEnd (=='0') "180cm"
-- ("180","cm")
breakEnd :: (Char -> Bool) -> Text -> (Text, Text)
breakEnd p src = let (a,b) = break p (reverse src)
                 in  (reverse b, reverse a)
{-# INLINE breakEnd #-}

, spanEnd inmplemented in terms of it, and some fixes of whitespace in spanEnd_.

TheMatten avatar Mar 15 '21 17:03 TheMatten

Would any other @haskell/text maintainers like to chime in?

@Lysxia you can request reviews from other maintainers, adding them under the gear in the top right corner of "Reviewers" panel.

Bodigrim avatar Mar 17 '21 21:03 Bodigrim

@TheMatten would it be possible to add tests for new functions?

Bodigrim avatar Mar 20 '21 19:03 Bodigrim

@Bodigrim Where would be a good place to add such tests? There doesn't seem to be a unit-testing suite, is there? It might be good to document this somewhere too.

Lysxia avatar Mar 20 '21 19:03 Lysxia

@Lysxia I suggest adding tests somewhere near https://github.com/haskell/text/blob/0998ad09e8ef566a5d6f2a69144b212ad3107bd6/tests/Tests/Properties.hs#L621-L622

Bodigrim avatar Mar 20 '21 19:03 Bodigrim

Thanks. I didn't realize we could (and were already) using Data.List as a reference implementation for property testing.

Lysxia avatar Mar 20 '21 19:03 Lysxia

@Bodigrim no problem. Should I include mentioned Lazy versions too?

TheMatten avatar Mar 20 '21 20:03 TheMatten

Yeah, it would be nice.

Bodigrim avatar Mar 20 '21 20:03 Bodigrim

Ping.

  1. Add tests.
  2. (optional) Add lazy versions.

Lysxia avatar May 12 '21 19:05 Lysxia

Oops, sorry, I forgot to upload changes - will do so today.

TheMatten avatar May 13 '21 07:05 TheMatten

Should I rebase it on current master?

TheMatten avatar May 13 '21 10:05 TheMatten

Yes please :)

Lysxia avatar May 13 '21 13:05 Lysxia

I've rebased on top of current master.

TheMatten avatar May 14 '21 10:05 TheMatten