{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -fno-warn-tabs #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Hcompta.Lib.Data.Text where import Data.Char (Char) import Data.Eq (Eq(..)) import qualified Data.List as List import Data.Maybe (Maybe(..)) import Data.String (String) import Data.Text (Text) import qualified Data.Text as Text -- * Class 'SplitOnChar' class SplitOnChar t where splitOnChar :: Char -> t -> [t] instance SplitOnChar Text where splitOnChar sep t = case Text.uncons t of Nothing -> [] Just (x, xs) -> if x == sep then splitOnChar sep xs else let (chunk, rest) = Text.break (== sep) t in chunk:splitOnChar sep rest instance SplitOnChar String where splitOnChar sep t = case t of [] -> [] x:xs -> if x == sep then splitOnChar sep xs else let (chunk, rest) = List.break (== sep) t in chunk:splitOnChar sep rest -- * Class 'SplitOnCharWithEmpty' class SplitOnCharWithEmpty t where splitOnCharWithEmpty :: Char -> t -> [t] instance SplitOnCharWithEmpty Text where splitOnCharWithEmpty sep t = case Text.break (== sep) t of (chunk, Text.uncons -> Just (_, rest)) -> chunk : splitOnCharWithEmpty sep rest (chunk, _) -> [chunk] instance SplitOnCharWithEmpty String where splitOnCharWithEmpty sep t = case List.break (== sep) t of (chunk, _:rest) -> chunk : splitOnCharWithEmpty sep rest (chunk, []) -> [chunk]