std:Text
lib/Text.omg
lib/Text.omg178 lines
1{- Text.
2
3 `Text` is a builtin type: literals with interpolation (`"Hello, ${name}!"`),
4 the append operator `++`, and the builtins `Text/show`, `Text/replace`,
5 `Text/length`, `Text/equal` and `Text/chars`.
6
7 A character is a Unicode code point: `"é"` written as an `e` followed by
8 a combining accent is two characters, and is not equal to the `"é"` of
9 one. Texts are compared character by character, as they are written.
10-}
11
12let L = std:List
13
14let empty : Text = ""
15
16let isEmpty : Text -> Bool = \t -> Natural/isZero (Text/length t)
17
18-- The number of characters (code points).
19let length : Text -> Natural = Text/length
20
21-- UTF-8 bytes, independently of the number of Unicode scalar values.
22let utf8Length : Text -> Natural = Text/utf8Length
23
24let equal : Text -> Text -> Bool = Text/equal
25
26-- A text as a source literal: quoted, with special characters escaped.
27let show : Text -> Text = Text/show
28
29-- replace needle replacement haystack
30let replace : Text -> Text -> Text -> Text = Text/replace
31
32-- The characters (code points) of a text, each as a text of length one.
33let chars : Text -> List Text = Text/chars
34
35let concat
36 : List Text -> Text
37 = List/elim
38 (\(_ : List Text) -> Text)
39 ""
40 (\(t : Text) (_ : List Text) (rest : Text) -> t ++ rest)
41
42-- concatSep ", " ["a", "b", "c"] = "a, b, c"
43let concatSep
44 : Text -> List Text -> Text
45 = \separator ->
46 List/elim
47 (\(_ : List Text) -> Text)
48 ""
49 (\(t : Text) (others : List Text) (rest : Text) ->
50 List/elim
51 (\(_ : List Text) -> Text)
52 t
53 (\(_ : Text) (_ : List Text) (_ : Text) -> t ++ separator ++ rest)
54 others)
55
56let concatMap
57 : forall {l : Level} {a : Type l} -> (a -> Text) -> List a -> Text
58 = \{l} {a} f ->
59 List/elim (\(_ : List a) -> Text) "" (\(x : a) (_ : List a) (rest : Text) -> f x ++ rest)
60
61-- replicate 3 "ab" = "ababab"
62let replicate
63 : Natural -> Text -> Text
64 = \n t -> Natural/elim (\(_ : Natural) -> Text) "" (\(_ : Natural) (rest : Text) -> t ++ rest) n
65
66-- Does the text start with the prefix? The prefix comes first:
67-- startsWith "#" "# a comment" = True
68let startsWith
69 : Text -> Text -> Bool
70 = \prefix text ->
71 L.equal Text/equal (Text/chars prefix) (L.take (Text/length prefix) (Text/chars text))
72
73-- Does the text end with the suffix? The suffix comes first.
74let endsWith
75 : Text -> Text -> Bool
76 = \suffix text ->
77 L.equal
78 Text/equal
79 (Text/chars suffix)
80 (L.drop (Natural/subtract (Text/length suffix) (Text/length text)) (Text/chars text))
81
82-- Does the needle occur in the text? The needle comes first, as in
83-- `replace`; every text contains the empty one.
84let contains
85 : Text -> Text -> Bool
86 = \needle text -> isEmpty needle || Text/equal (Text/replace needle "" text) text == False
87
88-- The pieces of a text between the occurrences of a separator, which comes
89-- first, as in `replace`: split ", " "a, b, c" = ["a", "b", "c"]. There is
90-- one piece more than there are separators, so split "," "" = [""]; an
91-- empty separator leaves the text whole.
92--
93-- The characters are read from the left, and the piece being read is kept
94-- backwards: it ends with the separator when it starts with the separator
95-- backwards.
96let split
97 : Text -> Text -> List Text
98 = \separator text ->
99 let backwards = L.reverse (Text/chars separator)
100
101 let size = L.length backwards
102
103 let Reading = { pieces : List Text, current : List Text }
104
105 let piece = \(current : List Text) -> concat (L.reverse current)
106
107 let step =
108 \(state : Reading) (c : Text) ->
109 let current = [ c ] # state.current
110 in if L.equal Text/equal backwards (L.take size current)
111 then { pieces = state.pieces # [ piece (L.drop size current) ]
112 , current = ([] : List Text)
113 }
114 else { pieces = state.pieces, current }
115
116 let read =
117 L.foldLeft
118 step
119 { pieces = ([] : List Text), current = ([] : List Text) }
120 (Text/chars text)
121
122 in if Natural/isZero size then [ text ] else read.pieces # [ piece read.current ]
123
124-- The lines of a text: the pieces between line breaks, without the empty
125-- one after a final line break. lines "a\nb\n" = ["a", "b"], lines "" = []
126-- (A carriage return before a line break stays at the end of its line:
127-- `trim` removes it.)
128let lines
129 : Text -> List Text
130 = \text ->
131 let pieces = split "\n" text
132 in if endsWith "\n" text || isEmpty text
133 then L.take (Natural/subtract 1 (L.length pieces)) pieces
134 else pieces
135
136-- The words of a text: the pieces between spaces, tabs and line breaks,
137-- however many there are. words " a b\n" = ["a", "b"]
138let words
139 : Text -> List Text
140 = \text ->
141 let spaced = Text/replace "\t" " " (Text/replace "\r" " " (Text/replace "\n" " " text))
142 in L.filter (\(word : Text) -> isEmpty word == False) (split " " spaced)
143
144-- Each line followed by a line break: the opposite of `lines`.
145let unlines : List Text -> Text = concatMap (\(line : Text) -> line ++ "\n")
146
147-- The words separated by spaces.
148let unwords : List Text -> Text = concatSep " "
149
150-- The text without the spaces, tabs and line breaks at its ends.
151let trim
152 : Text -> Text
153 = \text ->
154 let blank = \(c : Text) -> L.elem Text/equal c [ " ", "\t", "\n", "\r" ]
155 in concat (L.reverse (L.dropWhile blank (L.reverse (L.dropWhile blank (Text/chars text)))))
156
157in { empty
158 , isEmpty
159 , length
160 , utf8Length
161 , equal
162 , show
163 , replace
164 , chars
165 , concat
166 , concatSep
167 , concatMap
168 , replicate
169 , startsWith
170 , endsWith
171 , contains
172 , split
173 , lines
174 , words
175 , unlines
176 , unwords
177 , trim
178 }