@@ -32,8 +32,12 @@ import Data.Char as Char
32
32
import Data.Maybe (Maybe (Just, Nothing))
33
33
import Data.String as String
34
34
import Data.String.Unsafe as Unsafe
35
- -- WARN: In order for this module to be a drop-in replacement for Data.String,
36
- -- this list must be updated to re-export any exports added to Data.String.
35
+ -- WARN: If a new function is added to Data.String, a version of that function
36
+ -- should be exported from this module, which should be the same except that it
37
+ -- should operate on the code point level rather than the code unit level. If
38
+ -- the function's behaviour does not change based on whether we consider
39
+ -- strings as sequences of code points or code units, it can simply be
40
+ -- re-exported from Data.String.
37
41
import Data.String (Pattern (..), Replacement (..), charAt , charCodeAt , contains , fromCharArray , joinWith , localeCompare , null , replace , replaceAll , split , stripPrefix , stripSuffix , toChar , toCharArray , toLower , toUpper , trim ) as StringReExports
38
42
import Data.Tuple (Tuple (Tuple))
39
43
import Data.Unfoldable (unfoldr )
@@ -88,7 +92,7 @@ unsafeCodePointAt0Fallback s =
88
92
89
93
-- | Returns the first code point of the string after dropping the given number
90
94
-- | of code points from the beginning, if there is such a code point. Operates
91
- -- | in constant space and in time linear to `n` .
95
+ -- | in constant space and in time linear to the given index .
92
96
codePointAt :: Int -> String -> Maybe CodePoint
93
97
codePointAt n _ | n < 0 = Nothing
94
98
codePointAt 0 " " = Nothing
@@ -112,7 +116,7 @@ codePointAtFallback n s = case uncons s of
112
116
113
117
-- | Returns the number of code points in the leading sequence of code points
114
118
-- | which all match the given predicate. Operates in constant space and in
115
- -- | time linear to the length of the given string.
119
+ -- | time linear to the length of the string.
116
120
count :: (CodePoint -> Boolean ) -> String -> Int
117
121
count = _count countFallback unsafeCodePointAt0
118
122
@@ -132,23 +136,22 @@ countFallback p s = countTail p s 0
132
136
_ -> accum
133
137
134
138
135
- -- | Drops the given number of code points from the beginning of the given
136
- -- | string. If the string does not have that many code points, returns the
137
- -- | empty string. Operates in space and time linear to the length of the given
138
- -- | string.
139
+ -- | Drops the given number of code points from the beginning of the string. If
140
+ -- | the string does not have that many code points, returns the empty string.
141
+ -- | Operates in space and time linear to the length of the string.
139
142
drop :: Int -> String -> String
140
143
drop n s = String .drop (String .length (take n s)) s
141
144
142
145
143
146
-- | Drops the leading sequence of code points which all match the given
144
- -- | predicate from the given string. Operates in space and time linear to the
145
- -- | length of the given string.
147
+ -- | predicate from the string. Operates in space and time linear to the
148
+ -- | length of the string.
146
149
dropWhile :: (CodePoint -> Boolean ) -> String -> String
147
150
dropWhile p s = drop (count p s) s
148
151
149
152
150
153
-- | Creates a string from an array of code points. Operates in space and time
151
- -- | linear to the length of the given array.
154
+ -- | linear to the length of the array.
152
155
fromCodePointArray :: Array CodePoint -> String
153
156
fromCodePointArray = _fromCodePointArray singletonFallback
154
157
@@ -158,37 +161,37 @@ foreign import _fromCodePointArray
158
161
-> String
159
162
160
163
-- | Returns the number of code points preceding the first match of the given
161
- -- | pattern in the given string. Returns Nothing when no matches are found.
164
+ -- | pattern in the string. Returns Nothing when no matches are found.
162
165
indexOf :: String.Pattern -> String -> Maybe Int
163
166
indexOf p s = (\i -> length (String .take i s)) <$> String .indexOf p s
164
167
165
168
166
169
-- | Returns the number of code points preceding the first match of the given
167
- -- | pattern in the given string. Pattern matches preceding the given index
168
- -- | will be ignored. Returns Nothing when no matches are found.
170
+ -- | pattern in the string. Pattern matches preceding the given index will be
171
+ -- | ignored. Returns Nothing when no matches are found.
169
172
indexOf' :: String.Pattern -> Int -> String -> Maybe Int
170
173
indexOf' p i s =
171
174
let s' = drop i s in
172
175
(\k -> i + length (String .take k s')) <$> String .indexOf p s'
173
176
174
177
175
178
-- | Returns the number of code points preceding the last match of the given
176
- -- | pattern in the given string. Returns Nothing when no matches are found.
179
+ -- | pattern in the string. Returns Nothing when no matches are found.
177
180
lastIndexOf :: String.Pattern -> String -> Maybe Int
178
181
lastIndexOf p s = (\i -> length (String .take i s)) <$> String .lastIndexOf p s
179
182
180
183
181
184
-- | Returns the number of code points preceding the first match of the given
182
- -- | pattern in the given string. Pattern matches following the given index
183
- -- | will be ignored. Returns Nothing when no matches are found.
185
+ -- | pattern in the string. Pattern matches following the given index will be
186
+ -- | ignored. Returns Nothing when no matches are found.
184
187
lastIndexOf' :: String.Pattern -> Int -> String -> Maybe Int
185
188
lastIndexOf' p i s =
186
189
let i' = String .length (take i s) in
187
190
(\k -> length (String .take k s)) <$> String .lastIndexOf' p i' s
188
191
189
192
190
- -- | Returns the number of code points in the given string. Operates in
191
- -- | constant space and time linear to the length of the string.
193
+ -- | Returns the number of code points in the string. Operates in constant
194
+ -- | space and in time linear to the length of the string.
192
195
length :: String -> Int
193
196
length = Array .length <<< toCodePointArray
194
197
@@ -242,14 +245,14 @@ takeFallback n s = case uncons s of
242
245
243
246
244
247
-- | Returns a string containing the leading sequence of code points which all
245
- -- | match the given predicate from the given string. Operates in space and
246
- -- | time linear to the given number .
248
+ -- | match the given predicate from the string. Operates in space and time
249
+ -- | linear to the length of the string .
247
250
takeWhile :: (CodePoint -> Boolean ) -> String -> String
248
251
takeWhile p s = take (count p s) s
249
252
250
253
251
254
-- | Creates an array of code points from a string. Operates in space and time
252
- -- | linear to the length of the given string.
255
+ -- | linear to the length of the string.
253
256
toCodePointArray :: String -> Array CodePoint
254
257
toCodePointArray = _toCodePointArray toCodePointArrayFallback unsafeCodePointAt0
255
258
@@ -267,8 +270,8 @@ toCodePointArrayFallback s = unfoldr decode s
267
270
268
271
269
272
-- | Returns a record with the first code point and the remaining code points
270
- -- | of the given string. Returns Nothing if the string is empty. Operates in
271
- -- | space and time linear to the length of the string .
273
+ -- | of the string. Returns Nothing if the string is empty. Operates in
274
+ -- | constant space and time.
272
275
uncons :: String -> Maybe { head :: CodePoint , tail :: String }
273
276
uncons s = case String .length s of
274
277
0 -> Nothing
0 commit comments