File: Text.hs

package info (click to toggle)
haskell-prettyprinter 1.7.1-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: haskell: 2,453; ansic: 16; makefile: 6
file content (46 lines) | stat: -rw-r--r-- 1,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- Provide a fake API, mimicking Data.Text from text package,
-- but actually backed by type Text = String. It is used only in rare
-- circumstances, when prettyprinter is built with -text flag.
--

{-# OPTIONS_GHC -fno-warn-missing-signatures #-}

module Data.Text where

import Prelude hiding (head, length, null, replicate)
import qualified Data.Char
import qualified Data.List

type Text = String
cons = (:)
dropWhileEnd = Data.List.dropWhileEnd
head = Data.List.head
intercalate = Data.List.intercalate
length = Data.List.length :: [Char] -> Int
lines = Data.List.lines
map = Data.List.map
null = Data.List.null :: [Char] -> Bool
pack = id
replicate = (Data.List.concat .) . Data.List.replicate
singleton = (:[])
snoc xs x = xs ++ [x]
stripEnd = dropWhileEnd Data.Char.isSpace
unlines = Data.List.unlines
unpack = id
words = Data.List.words

uncons :: Text -> Maybe (Char, Text)
uncons [] = Nothing
uncons (x : xs) = Just (x, xs)

splitOn :: Text -> Text -> [Text]
splitOn pat src
  | null pat = error "splitOn: empty pattern"
  | otherwise = go [] src
  where
    go acc [] = [reverse acc]
    go acc xs@(y : ys)
      | pat `Data.List.isPrefixOf` xs
      = reverse acc : go [] (drop (length pat) xs)
      | otherwise
      = go (y : acc) ys