Skip to content

split function for Data.Conduit.Text #205

Closed

Description

@MaxGabriel

Hi, I needed a split function for some log parsing I was doing with Conduit, would you be interested in me adding this to Data.Conduit.Text? If so, I can add tests and remove the Safe dependency that my code is using, then submit a PR.

This is the code I'm currently using; it's modeled closely after lines and I believe lines could be implemented in terms of split.

split :: Monad m => Text -> Conduit Text m Text
split splitText = awaitText T.empty
    where
      awaitText buf = await >>= maybe (finish buf) (process buf)

      finish buf = unless (T.null buf) (yield buf)

      process buf text = yieldSplits $ buf `T.append` text

      yieldSplits text = do
        let splits = T.splitOn splitText text
            lastSplit = lastDef T.empty splits
        mapM_ yield (initSafe splits)
        awaitText lastSplit

Activity

  1. mgsloan commented on Mar 27, 2015

    @mgsloan
    Contributor

    Just for info / reference, I did something similar when implementing fusion in conduit-combinators: https://github.com/fpco/conduit-combinators/blob/master/Data/Conduit/Combinators.hs#L1889

    The main difference is that yours takes a Text rather than Element seq -> Bool.

  2. MaxGabriel commented on Mar 27, 2015

    @MaxGabriel
    ContributorAuthor

    Cool @mgsloan. I guess the conduit-combinators version of this would take an EqSequence and split its input using that?

  3. mgsloan commented on Mar 27, 2015

    @mgsloan
    Contributor

    Yup, that seems reasonable to me. I'm not seeing an implementation of split for EqSequence, though.

  4. snoyberg commented on Mar 28, 2015

    @snoyberg
    Owner

    I think the code above is subtly broken; if the broken-upon string occurs over a chunk boundary, it won't fire. E.g.:

    mapM_ yield ["123x", "x456"] $= split "xx"

    doesn't look like it would result in the output you'd expect.

  5. snoyberg commented on Jul 20, 2015

    @snoyberg
    Owner

    Tracked by PR #207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions