@@ -32,8 +32,12 @@ import Data.Char as Char
3232import Data.Maybe (Maybe (Just, Nothing))
3333import Data.String as String
3434import 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.
3741import Data.String (Pattern (..), Replacement (..), charAt , charCodeAt , contains , fromCharArray , joinWith , localeCompare , null , replace , replaceAll , split , stripPrefix , stripSuffix , toChar , toCharArray , toLower , toUpper , trim ) as StringReExports
3842import Data.Tuple (Tuple (Tuple))
3943import Data.Unfoldable (unfoldr )
@@ -88,7 +92,7 @@ unsafeCodePointAt0Fallback s =
8892
8993-- | Returns the first code point of the string after dropping the given number
9094-- | 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 .
9296codePointAt :: Int -> String -> Maybe CodePoint
9397codePointAt n _ | n < 0 = Nothing
9498codePointAt 0 " " = Nothing
@@ -112,7 +116,7 @@ codePointAtFallback n s = case uncons s of
112116
113117-- | Returns the number of code points in the leading sequence of code points
114118-- | 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.
116120count :: (CodePoint -> Boolean ) -> String -> Int
117121count = _count countFallback unsafeCodePointAt0
118122
@@ -132,23 +136,22 @@ countFallback p s = countTail p s 0
132136 _ -> accum
133137
134138
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.
139142drop :: Int -> String -> String
140143drop n s = String .drop (String .length (take n s)) s
141144
142145
143146-- | 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.
146149dropWhile :: (CodePoint -> Boolean ) -> String -> String
147150dropWhile p s = drop (count p s) s
148151
149152
150153-- | 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.
152155fromCodePointArray :: Array CodePoint -> String
153156fromCodePointArray = _fromCodePointArray singletonFallback
154157
@@ -158,37 +161,37 @@ foreign import _fromCodePointArray
158161 -> String
159162
160163-- | 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.
162165indexOf :: String.Pattern -> String -> Maybe Int
163166indexOf p s = (\i -> length (String .take i s)) <$> String .indexOf p s
164167
165168
166169-- | 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.
169172indexOf' :: String.Pattern -> Int -> String -> Maybe Int
170173indexOf' p i s =
171174 let s' = drop i s in
172175 (\k -> i + length (String .take k s')) <$> String .indexOf p s'
173176
174177
175178-- | 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.
177180lastIndexOf :: String.Pattern -> String -> Maybe Int
178181lastIndexOf p s = (\i -> length (String .take i s)) <$> String .lastIndexOf p s
179182
180183
181184-- | 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.
184187lastIndexOf' :: String.Pattern -> Int -> String -> Maybe Int
185188lastIndexOf' p i s =
186189 let i' = String .length (take i s) in
187190 (\k -> length (String .take k s)) <$> String .lastIndexOf' p i' s
188191
189192
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.
192195length :: String -> Int
193196length = Array .length <<< toCodePointArray
194197
@@ -242,14 +245,14 @@ takeFallback n s = case uncons s of
242245
243246
244247-- | 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 .
247250takeWhile :: (CodePoint -> Boolean ) -> String -> String
248251takeWhile p s = take (count p s) s
249252
250253
251254-- | 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.
253256toCodePointArray :: String -> Array CodePoint
254257toCodePointArray = _toCodePointArray toCodePointArrayFallback unsafeCodePointAt0
255258
@@ -267,8 +270,8 @@ toCodePointArrayFallback s = unfoldr decode s
267270
268271
269272-- | 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.
272275uncons :: String -> Maybe { head :: CodePoint , tail :: String }
273276uncons s = case String .length s of
274277 0 -> Nothing
0 commit comments