1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ViewPatterns #-}
4 {-# OPTIONS_GHC -fno-warn-tabs #-}
5 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 module Hcompta.Lib.Data.Text where
8 import Data.Char (Char)
9 import Data.Eq (Eq(..))
10 import qualified Data.List as List
11 import Data.Maybe (Maybe(..))
12 import Data.String (String)
13 import Data.Text (Text)
14 import qualified Data.Text as Text
16 -- * Class 'SplitOnChar'
18 class SplitOnChar t where
19 splitOnChar :: Char -> t -> [t]
20 instance SplitOnChar Text where
26 then splitOnChar sep xs
28 let (chunk, rest) = Text.break (== sep) t in
29 chunk:splitOnChar sep rest
30 instance SplitOnChar String where
36 then splitOnChar sep xs
38 let (chunk, rest) = List.break (== sep) t in
39 chunk:splitOnChar sep rest
41 -- * Class 'SplitOnCharWithEmpty'
43 class SplitOnCharWithEmpty t where
44 splitOnCharWithEmpty :: Char -> t -> [t]
45 instance SplitOnCharWithEmpty Text where
46 splitOnCharWithEmpty sep t =
47 case Text.break (== sep) t of
48 (chunk, Text.uncons -> Just (_, rest)) -> chunk : splitOnCharWithEmpty sep rest
50 instance SplitOnCharWithEmpty String where
51 splitOnCharWithEmpty sep t =
52 case List.break (== sep) t of
53 (chunk, _:rest) -> chunk : splitOnCharWithEmpty sep rest
54 (chunk, []) -> [chunk]