-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCodeUnits.purs
More file actions
304 lines (275 loc) · 9.75 KB
/
CodeUnits.purs
File metadata and controls
304 lines (275 loc) · 9.75 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
module Data.String.NonEmpty.CodeUnits
( fromCharArray
, fromNonEmptyCharArray
, singleton
, cons
, snoc
, fromFoldable1
, toCharArray
, toNonEmptyCharArray
, charAt
, toChar
, indexOf
, indexOf'
, lastIndexOf
, lastIndexOf'
, uncons
, length
, take
, takeRight
, takeWhile
, drop
, dropRight
, dropWhile
, countPrefix
, splitAt
) where
import Prelude
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NEA
import Data.Maybe (Maybe(..), fromJust)
import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Foldable as F1
import Data.String.CodeUnits as CU
import Data.String.NonEmpty.Internal (NonEmptyString(..), fromString)
import Data.String.Pattern (Pattern)
import Data.String.Unsafe as U
import Partial.Unsafe (unsafePartial)
-- For internal use only. Do not export.
toNonEmptyString :: String -> NonEmptyString
toNonEmptyString = NonEmptyString
-- For internal use only. Do not export.
fromNonEmptyString :: NonEmptyString -> String
fromNonEmptyString (NonEmptyString s) = s
-- For internal use only. Do not export.
liftS :: forall r. (String -> r) -> NonEmptyString -> r
liftS f (NonEmptyString s) = f s
-- | Creates a `NonEmptyString` from a character array `String`, returning
-- | `Nothing` if the input is empty.
-- |
-- | ```purescript
-- | fromCharArray [] = Nothing
-- | fromCharArray ['a', 'b', 'c'] = Just (NonEmptyString "abc")
-- | ```
fromCharArray :: Array Char -> Maybe NonEmptyString
fromCharArray = case _ of
[] -> Nothing
cs -> Just (toNonEmptyString (CU.fromCharArray cs))
fromNonEmptyCharArray :: NonEmptyArray Char -> NonEmptyString
fromNonEmptyCharArray = unsafePartial fromJust <<< fromCharArray <<< NEA.toArray
-- | Creates a `NonEmptyString` from a character.
singleton :: Char -> NonEmptyString
singleton = toNonEmptyString <<< CU.singleton
-- | Creates a `NonEmptyString` from a string by prepending a character.
-- |
-- | ```purescript
-- | cons 'a' "bc" = NonEmptyString "abc"
-- | cons 'a' "" = NonEmptyString "a"
-- | ```
cons :: Char -> String -> NonEmptyString
cons c s = toNonEmptyString (CU.singleton c <> s)
-- | Creates a `NonEmptyString` from a string by appending a character.
-- |
-- | ```purescript
-- | snoc 'c' "ab" = NonEmptyString "abc"
-- | snoc 'a' "" = NonEmptyString "a"
-- | ```
snoc :: Char -> String -> NonEmptyString
snoc c s = toNonEmptyString (s <> CU.singleton c)
-- | Creates a `NonEmptyString` from a `Foldable1` container carrying
-- | characters.
fromFoldable1 :: forall f. Foldable1 f => f Char -> NonEmptyString
fromFoldable1 = F1.foldMap1 singleton
-- | Converts the `NonEmptyString` into an array of characters.
-- |
-- | ```purescript
-- | toCharArray (NonEmptyString "Hello☺\n") == ['H','e','l','l','o','☺','\n']
-- | ```
toCharArray :: NonEmptyString -> Array Char
toCharArray = CU.toCharArray <<< fromNonEmptyString
-- | Converts the `NonEmptyString` into a non-empty array of characters.
toNonEmptyCharArray :: NonEmptyString -> NonEmptyArray Char
toNonEmptyCharArray = unsafePartial fromJust <<< NEA.fromArray <<< toCharArray
-- | Returns the character at the given index, if the index is within bounds.
-- |
-- | ```purescript
-- | charAt 2 (NonEmptyString "Hello") == Just 'l'
-- | charAt 10 (NonEmptyString "Hello") == Nothing
-- | ```
charAt :: Int -> NonEmptyString -> Maybe Char
charAt = liftS <<< CU.charAt
-- | Converts the `NonEmptyString` to a character, if the length of the string
-- | is exactly `1`.
-- |
-- | ```purescript
-- | toChar "H" == Just 'H'
-- | toChar "Hi" == Nothing
-- | ```
toChar :: NonEmptyString -> Maybe Char
toChar = CU.toChar <<< fromNonEmptyString
-- | Returns the index of the first occurrence of the pattern in the
-- | given string. Returns `Nothing` if there is no match.
-- |
-- | ```purescript
-- | indexOf (Pattern "c") (NonEmptyString "abcdc") == Just 2
-- | indexOf (Pattern "c") (NonEmptyString "aaa") == Nothing
-- | ```
indexOf :: Pattern -> NonEmptyString -> Maybe Int
indexOf = liftS <<< CU.indexOf
-- | Returns the index of the first occurrence of the pattern in the
-- | given string, starting at the specified index. Returns `Nothing` if there is
-- | no match.
-- |
-- | ```purescript
-- | indexOf' (Pattern "a") 2 (NonEmptyString "ababa") == Just 2
-- | indexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 4
-- | ```
indexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int
indexOf' pat = liftS <<< CU.indexOf' pat
-- | Returns the index of the last occurrence of the pattern in the
-- | given string. Returns `Nothing` if there is no match.
-- |
-- | ```purescript
-- | lastIndexOf (Pattern "c") (NonEmptyString "abcdc") == Just 4
-- | lastIndexOf (Pattern "c") (NonEmptyString "aaa") == Nothing
-- | ```
lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int
lastIndexOf = liftS <<< CU.lastIndexOf
-- | Returns the index of the last occurrence of the pattern in the
-- | given string, starting at the specified index and searching
-- | backwards towards the beginning of the string.
-- |
-- | Starting at a negative index is equivalent to starting at 0 and
-- | starting at an index greater than the string length is equivalent
-- | to searching in the whole string.
-- |
-- | Returns `Nothing` if there is no match.
-- |
-- | ```purescript
-- | lastIndexOf' (Pattern "a") (-1) (NonEmptyString "ababa") == Just 0
-- | lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
-- | lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
-- | lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4
-- | lastIndexOf' (Pattern "a") 5 (NonEmptyString "ababa") == Just 4
-- | ```
lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int
lastIndexOf' pat = liftS <<< CU.lastIndexOf' pat
-- | Returns the first character and the rest of the string.
-- |
-- | ```purescript
-- | uncons "a" == { head: 'a', tail: Nothing }
-- | uncons "Hello World" == { head: 'H', tail: Just (NonEmptyString "ello World") }
-- | ```
uncons :: NonEmptyString -> { head :: Char, tail :: Maybe NonEmptyString }
uncons nes =
let
s = fromNonEmptyString nes
in
{ head: U.charAt 0 s
, tail: fromString (CU.drop 1 s)
}
-- | Returns the number of characters the string is composed of.
-- |
-- | ```purescript
-- | length (NonEmptyString "Hello World") == 11
-- | ```
length :: NonEmptyString -> Int
length = CU.length <<< fromNonEmptyString
-- | Returns the first `n` characters of the string. Returns `Nothing` if `n` is
-- | less than 1.
-- |
-- | ```purescript
-- | take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
-- | take 0 (NonEmptyString "Hello World") == Nothing
-- | ```
take :: Int -> NonEmptyString -> Maybe NonEmptyString
take i nes =
let
s = fromNonEmptyString nes
in
if i < 1
then Nothing
else Just (toNonEmptyString (CU.take i s))
-- | Returns the last `n` characters of the string. Returns `Nothing` if `n` is
-- | less than 1.
-- |
-- | ```purescript
-- | take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
-- | take 0 (NonEmptyString "Hello World") == Nothing
-- | ```
takeRight :: Int -> NonEmptyString -> Maybe NonEmptyString
takeRight i nes =
let
s = fromNonEmptyString nes
in
if i < 1
then Nothing
else Just (toNonEmptyString (CU.takeRight i s))
-- | Returns the longest prefix of characters that satisfy the predicate.
-- | `Nothing` is returned if there is no matching prefix.
-- |
-- | ```purescript
-- | takeWhile (_ /= ':') (NonEmptyString "http://purescript.org") == Just (NonEmptyString "http")
-- | takeWhile (_ == 'a') (NonEmptyString "xyz") == Nothing
-- | ```
takeWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString
takeWhile f = fromString <<< liftS (CU.takeWhile f)
-- | Returns the string without the first `n` characters. Returns `Nothing` if
-- | more characters are dropped than the string is long.
-- |
-- | ```purescript
-- | drop 6 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
-- | drop 20 (NonEmptyString "Hello World") == Nothing
-- | ```
drop :: Int -> NonEmptyString -> Maybe NonEmptyString
drop i nes =
let
s = fromNonEmptyString nes
in
if i >= CU.length s
then Nothing
else Just (toNonEmptyString (CU.drop i s))
-- | Returns the string without the last `n` characters. Returns `Nothing` if
-- | more characters are dropped than the string is long.
-- |
-- | ```purescript
-- | dropRight 6 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
-- | dropRight 20 (NonEmptyString "Hello World") == Nothing
-- | ```
dropRight :: Int -> NonEmptyString -> Maybe NonEmptyString
dropRight i nes =
let
s = fromNonEmptyString nes
in
if i >= CU.length s
then Nothing
else Just (toNonEmptyString (CU.dropRight i s))
-- | Returns the suffix remaining after `takeWhile`.
-- |
-- | ```purescript
-- | dropWhile (_ /= '.') (NonEmptyString "Test.purs") == Just (NonEmptyString ".purs")
-- | ```
dropWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString
dropWhile f = fromString <<< liftS (CU.dropWhile f)
-- | Returns the number of contiguous characters at the beginning of the string
-- | for which the predicate holds.
-- |
-- | ```purescript
-- | countPrefix (_ /= 'o') (NonEmptyString "Hello World") == 4
-- | ```
countPrefix :: (Char -> Boolean) -> NonEmptyString -> Int
countPrefix = liftS <<< CU.countPrefix
-- | Returns the substrings of a split at the given index, if the index is
-- | within bounds.
-- |
-- | ```purescript
-- | splitAt 2 (NonEmptyString "Hello World") == Just { before: Just (NonEmptyString "He"), after: Just (NonEmptyString "llo World") }
-- | splitAt 10 (NonEmptyString "Hi") == Nothing
-- | ```
splitAt
:: Int
-> NonEmptyString
-> { before :: Maybe NonEmptyString, after :: Maybe NonEmptyString }
splitAt i nes =
case CU.splitAt i (fromNonEmptyString nes) of
{ before, after } -> { before: fromString before, after: fromString after }