2
2
-- | A String represents a sequence of characters.
3
3
-- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
4
4
module Data.String
5
- ( charAt
5
+ ( Pattern (..)
6
+ , Replacement (..)
7
+ , charAt
6
8
, charCodeAt
7
9
, fromCharArray
8
10
, toChar
@@ -17,6 +19,7 @@ module Data.String
17
19
, singleton
18
20
, localeCompare
19
21
, replace
22
+ , replaceAll
20
23
, take
21
24
, takeWhile
22
25
, drop
@@ -36,17 +39,39 @@ module Data.String
36
39
import Prelude
37
40
38
41
import Data.Maybe (Maybe (..), isJust )
42
+ import Data.Newtype (class Newtype )
39
43
import Data.String.Unsafe as U
40
44
45
+ -- | A newtype used in cases where there is a string to be matched.
46
+ newtype Pattern = Pattern String
47
+
48
+ derive instance eqPattern :: Eq Pattern
49
+ derive instance ordPattern :: Ord Pattern
50
+ derive instance newtypePattern :: Newtype Pattern _
51
+
52
+ instance showPattern :: Show Pattern where
53
+ show (Pattern s) = " (Pattern " <> s <> " )"
54
+
55
+ -- | A newtype used in cases to specify a replacement for a pattern.
56
+ newtype Replacement = Replacement String
57
+
58
+ derive instance eqReplacement :: Eq Replacement
59
+ derive instance ordReplacement :: Ord Replacement
60
+ derive instance newtypeReplacement :: Newtype Replacement _
61
+
62
+ instance showReplacement :: Show Replacement where
63
+ show (Replacement s) = " (Replacement " <> s <> " )"
64
+
41
65
-- | Returns the character at the given index, if the index is within bounds.
42
66
charAt :: Int -> String -> Maybe Char
43
67
charAt = _charAt Just Nothing
44
68
45
- foreign import _charAt :: (forall a . a -> Maybe a )
46
- -> (forall a . Maybe a )
47
- -> Int
48
- -> String
49
- -> Maybe Char
69
+ foreign import _charAt
70
+ :: (forall a . a -> Maybe a )
71
+ -> (forall a . Maybe a )
72
+ -> Int
73
+ -> String
74
+ -> Maybe Char
50
75
51
76
-- | Returns a string of length `1` containing the given character.
52
77
foreign import singleton :: Char -> String
@@ -56,19 +81,21 @@ foreign import singleton :: Char -> String
56
81
charCodeAt :: Int -> String -> Maybe Int
57
82
charCodeAt = _charCodeAt Just Nothing
58
83
59
- foreign import _charCodeAt :: (forall a . a -> Maybe a )
60
- -> (forall a . Maybe a )
61
- -> Int
62
- -> String
63
- -> Maybe Int
84
+ foreign import _charCodeAt
85
+ :: (forall a . a -> Maybe a )
86
+ -> (forall a . Maybe a )
87
+ -> Int
88
+ -> String
89
+ -> Maybe Int
64
90
65
91
toChar :: String -> Maybe Char
66
92
toChar = _toChar Just Nothing
67
93
68
- foreign import _toChar :: (forall a . a -> Maybe a )
69
- -> (forall a . Maybe a )
70
- -> String
71
- -> Maybe Char
94
+ foreign import _toChar
95
+ :: (forall a . a -> Maybe a )
96
+ -> (forall a . Maybe a )
97
+ -> String
98
+ -> Maybe Char
72
99
73
100
-- | Returns `true` if the given string is empty.
74
101
null :: String -> Boolean
@@ -81,7 +108,7 @@ uncons "" = Nothing
81
108
uncons s = Just { head: U .charAt zero s, tail: drop one s }
82
109
83
110
-- | Returns the longest prefix (possibly empty) of characters that satisfy
84
- -- | the predicate:
111
+ -- | the predicate.
85
112
takeWhile :: (Char -> Boolean ) -> String -> String
86
113
takeWhile p s = take (count p s) s
87
114
@@ -91,80 +118,82 @@ dropWhile p s = drop (count p s) s
91
118
92
119
-- | If the string starts with the given prefix, return the portion of the
93
120
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
94
- -- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"`
95
- -- | * `stripPrefix "http:" "https://purescript.org" == Nothing`
96
- stripPrefix :: String -> String -> Maybe String
97
- stripPrefix prefix str =
121
+ -- | * `stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"`
122
+ -- | * `stripPrefix (Pattern "http:") "https://purescript.org" == Nothing`
123
+ stripPrefix :: Pattern -> String -> Maybe String
124
+ stripPrefix prefix@( Pattern prefixS) str =
98
125
case indexOf prefix str of
99
- Just 0 -> Just $ drop (length prefix ) str
100
- _ -> Nothing
126
+ Just 0 -> Just $ drop (length prefixS ) str
127
+ _ -> Nothing
101
128
102
129
-- | If the string ends with the given suffix, return the portion of the
103
130
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
104
- -- | * `stripSuffix ".exe" "psc.exe" == Just "psc"`
105
- -- | * `stripSuffix ".exe" "psc" == Nothing`
106
- stripSuffix :: String -> String -> Maybe String
107
- stripSuffix suffix str =
131
+ -- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
132
+ -- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
133
+ stripSuffix :: Pattern -> String -> Maybe String
134
+ stripSuffix suffix@( Pattern suffixS) str =
108
135
case lastIndexOf suffix str of
109
- Just x | x == length str - length suffix ->
110
- Just $ take x str
111
- _ ->
112
- Nothing
136
+ Just x | x == length str - length suffixS -> Just $ take x str
137
+ _ -> Nothing
113
138
114
139
-- | Converts an array of characters into a string.
115
140
foreign import fromCharArray :: Array Char -> String
116
141
117
142
-- | Checks whether the first string exists in the second string.
118
- contains :: String -> String -> Boolean
119
- contains x s = isJust ( indexOf x s)
143
+ contains :: Pattern -> String -> Boolean
144
+ contains pat = isJust <<< indexOf pat
120
145
121
146
-- | Returns the index of the first occurrence of the first string in the
122
147
-- | second string. Returns `Nothing` if there is no match.
123
- indexOf :: String -> String -> Maybe Int
148
+ indexOf :: Pattern -> String -> Maybe Int
124
149
indexOf = _indexOf Just Nothing
125
150
126
- foreign import _indexOf :: (forall a . a -> Maybe a )
127
- -> (forall a . Maybe a )
128
- -> String
129
- -> String
130
- -> Maybe Int
151
+ foreign import _indexOf
152
+ :: (forall a . a -> Maybe a )
153
+ -> (forall a . Maybe a )
154
+ -> Pattern
155
+ -> String
156
+ -> Maybe Int
131
157
132
158
-- | Returns the index of the first occurrence of the first string in the
133
159
-- | second string, starting at the given index. Returns `Nothing` if there is
134
160
-- | no match.
135
- indexOf' :: String -> Int -> String -> Maybe Int
161
+ indexOf' :: Pattern -> Int -> String -> Maybe Int
136
162
indexOf' = _indexOf' Just Nothing
137
163
138
- foreign import _indexOf' :: (forall a . a -> Maybe a )
139
- -> (forall a . Maybe a )
140
- -> String
141
- -> Int
142
- -> String
143
- -> Maybe Int
164
+ foreign import _indexOf'
165
+ :: (forall a . a -> Maybe a )
166
+ -> (forall a . Maybe a )
167
+ -> Pattern
168
+ -> Int
169
+ -> String
170
+ -> Maybe Int
144
171
145
172
-- | Returns the index of the last occurrence of the first string in the
146
173
-- | second string. Returns `Nothing` if there is no match.
147
- lastIndexOf :: String -> String -> Maybe Int
174
+ lastIndexOf :: Pattern -> String -> Maybe Int
148
175
lastIndexOf = _lastIndexOf Just Nothing
149
176
150
- foreign import _lastIndexOf :: (forall a . a -> Maybe a )
151
- -> (forall a . Maybe a )
152
- -> String
153
- -> String
154
- -> Maybe Int
177
+ foreign import _lastIndexOf
178
+ :: (forall a . a -> Maybe a )
179
+ -> (forall a . Maybe a )
180
+ -> Pattern
181
+ -> String
182
+ -> Maybe Int
155
183
156
184
-- | Returns the index of the last occurrence of the first string in the
157
185
-- | second string, starting at the given index. Returns `Nothing` if there is
158
186
-- | no match.
159
- lastIndexOf' :: String -> Int -> String -> Maybe Int
187
+ lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
160
188
lastIndexOf' = _lastIndexOf' Just Nothing
161
189
162
- foreign import _lastIndexOf' :: (forall a . a -> Maybe a )
163
- -> (forall a . Maybe a )
164
- -> String
165
- -> Int
166
- -> String
167
- -> Maybe Int
190
+ foreign import _lastIndexOf'
191
+ :: (forall a . a -> Maybe a )
192
+ -> (forall a . Maybe a )
193
+ -> Pattern
194
+ -> Int
195
+ -> String
196
+ -> Maybe Int
168
197
169
198
-- | Returns the number of characters the string is composed of.
170
199
foreign import length :: String -> Int
@@ -173,15 +202,19 @@ foreign import length :: String -> Int
173
202
localeCompare :: String -> String -> Ordering
174
203
localeCompare = _localeCompare LT EQ GT
175
204
176
- foreign import _localeCompare :: Ordering
177
- -> Ordering
178
- -> Ordering
179
- -> String
180
- -> String
181
- -> Ordering
205
+ foreign import _localeCompare
206
+ :: Ordering
207
+ -> Ordering
208
+ -> Ordering
209
+ -> String
210
+ -> String
211
+ -> Ordering
182
212
183
213
-- | Replaces the first occurence of the first argument with the second argument.
184
- foreign import replace :: String -> String -> String -> String
214
+ foreign import replace :: Pattern -> Replacement -> String -> String
215
+
216
+ -- | Replaces all occurences of the first argument with the second argument.
217
+ foreign import replaceAll :: Pattern -> Replacement -> String -> String
185
218
186
219
-- | Returns the first `n` characters of the string.
187
220
foreign import take :: Int -> String -> String
@@ -196,7 +229,7 @@ foreign import count :: (Char -> Boolean) -> String -> Int
196
229
-- | Returns the substrings of the second string separated along occurences
197
230
-- | of the first string.
198
231
-- | * `split " " "hello world" == ["hello", "world"]`
199
- foreign import split :: String -> String -> Array String
232
+ foreign import split :: Pattern -> String -> Array String
200
233
201
234
-- | Returns the substrings of split at the given index, if the index is within bounds.
202
235
splitAt :: Int -> String -> Maybe (Array String )
0 commit comments