Skip to content

Commit 370af7c

Browse files
add comments and complexity notes
1 parent 0d81e0b commit 370af7c

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/Data/String/CodePoints.purs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ isTrail cu = 0xDC00 <= cu && cu <= 0xDFFF
6868
fromCharCode :: Int -> String
6969
fromCharCode = String.singleton <<< Char.fromCharCode
7070

71+
-- WARN: this function expects the String parameter to be non-empty
7172
unsafeCodePointAt0 :: String -> CodePoint
7273
unsafeCodePointAt0 = _unsafeCodePointAt0 unsafeCodePointAt0Fallback
7374

@@ -86,6 +87,9 @@ unsafeCodePointAt0Fallback s =
8687
cu1 = Unsafe.charCodeAt 1 s
8788

8889

90+
-- | Returns the first code point of the string after dropping the given number
91+
-- | of code points from the beginning, if there is such a code point. Operates
92+
-- | in constant space and in time linear to `n`.
8993
codePointAt :: Int -> String -> Maybe CodePoint
9094
codePointAt 0 "" = Nothing
9195
codePointAt 0 s = Just (unsafeCodePointAt0 s)
@@ -104,6 +108,9 @@ codePointAtFallback :: Int -> String -> Maybe CodePoint
104108
codePointAtFallback n s = Array.index (toCodePointArray s) n
105109

106110

111+
-- | Returns the number of code points in the leading sequence of code points
112+
-- | which all match the given predicate. Operates in constant space and in
113+
-- | time linear to the length of the given string.
107114
count :: (CodePoint -> Boolean) -> String -> Int
108115
count = _count isLead isTrail unsurrogate
109116

@@ -116,14 +123,23 @@ foreign import _count
116123
-> Int
117124

118125

126+
-- | Drops the given number of code points from the beginning of the given
127+
-- | string. If the string does not have that many code points, returns the
128+
-- | empty string. Operates in space and time linear to the length of the given
129+
-- | string.
119130
drop :: Int -> String -> String
120131
drop n s = fromCodePointArray (Array.drop n (toCodePointArray s))
121132

122133

134+
-- | Drops the leading sequence of code points which all match the given
135+
-- | predicate from the given string. Operates in space and time linear to the
136+
-- | length of the given string.
123137
dropWhile :: (CodePoint -> Boolean) -> String -> String
124138
dropWhile p s = drop (count p s) s
125139

126140

141+
-- | Creates a string from an array of code points. Operates in space and time
142+
-- | linear to the length of the given array.
127143
fromCodePointArray :: Array CodePoint -> String
128144
fromCodePointArray = _fromCodePointArray singletonFallback
129145

@@ -132,31 +148,44 @@ foreign import _fromCodePointArray
132148
-> Array CodePoint
133149
-> String
134150

135-
151+
-- | Returns the number of code points preceding the first match of the given
152+
-- | pattern in the given string. Returns Nothing when no matches are found.
136153
indexOf :: String.Pattern -> String -> Maybe Int
137154
indexOf p s = (\i -> length (String.take i s)) <$> String.indexOf p s
138155

139156

157+
-- | Returns the number of code points preceding the first match of the given
158+
-- | pattern in the given string. Pattern matches preceding the given index
159+
-- | will be ignored. Returns Nothing when no matches are found.
140160
indexOf' :: String.Pattern -> Int -> String -> Maybe Int
141161
indexOf' p i s =
142162
let s' = drop i s in
143163
(\k -> i + length (String.take k s')) <$> String.indexOf p s'
144164

145165

166+
-- | Returns the number of code points preceding the last match of the given
167+
-- | pattern in the given string. Returns Nothing when no matches are found.
146168
lastIndexOf :: String.Pattern -> String -> Maybe Int
147169
lastIndexOf p s = (\i -> length (String.take i s)) <$> String.lastIndexOf p s
148170

149171

172+
-- | Returns the number of code points preceding the first match of the given
173+
-- | pattern in the given string. Pattern matches following the given index
174+
-- | will be ignored. Returns Nothing when no matches are found.
150175
lastIndexOf' :: String.Pattern -> Int -> String -> Maybe Int
151176
lastIndexOf' p i s =
152177
let i' = String.length (take i s) in
153178
(\k -> length (String.take k s)) <$> String.lastIndexOf' p i' s
154179

155180

181+
-- | Returns the number of code points in the given string. Operates in
182+
-- | constant space and time linear to the length of the string.
156183
length :: String -> Int
157184
length = Array.length <<< toCodePointArray
158185

159186

187+
-- | Creates a string containing just the given code point. Operates in
188+
-- | constant space and time.
160189
singleton :: CodePoint -> String
161190
singleton = _singleton singletonFallback
162191

@@ -173,6 +202,9 @@ singletonFallback (CodePoint cp) = fromCharCode lead <> fromCharCode trail
173202
trail = (cp - 0x10000) `mod` 0x400 + 0xDC00
174203

175204

205+
-- | Returns a record with strings created from the code points on either side
206+
-- | of the given index. If the index is not within the string, Nothing is
207+
-- | returned.
176208
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
177209
splitAt i s =
178210
let cps = toCodePointArray s in
@@ -184,6 +216,10 @@ splitAt i s =
184216
}
185217

186218

219+
-- | Returns a string containing the given number of code points from the
220+
-- | beginning of the given string. If the string does not have that many code
221+
-- | points, returns the empty string. Operates in space and time linear to the
222+
-- | given number.
187223
take :: Int -> String -> String
188224
take = _take takeFallback
189225

@@ -193,10 +229,15 @@ takeFallback :: Int -> String -> String
193229
takeFallback n s = fromCodePointArray (Array.take n (toCodePointArray s))
194230

195231

232+
-- | Returns a string containing the leading sequence of code points which all
233+
-- | match the given predicate from the given string. Operates in space and
234+
-- | time linear to the given number.
196235
takeWhile :: (CodePoint -> Boolean) -> String -> String
197236
takeWhile p s = take (count p s) s
198237

199238

239+
-- | Creates an array of code points from a string. Operates in space and time
240+
-- | linear to the length of the given string.
200241
toCodePointArray :: String -> Array CodePoint
201242
toCodePointArray = _toCodePointArray toCodePointArrayFallback unsafeCodePointAt0
202243

@@ -216,5 +257,8 @@ toCodePointArrayFallback s = unfoldr decode (fromFoldable (Char.toCharCode <$> S
216257
decode Nil = Nothing
217258

218259

260+
-- | Returns a record with the first code point and the remaining code points
261+
-- | of the given string. Returns Nothing if the string is empty. Operates in
262+
-- | space and time linear to the length of the string.
219263
uncons :: String -> Maybe { head :: CodePoint, tail :: String }
220264
uncons s = { head: _, tail: drop 1 s } <$> codePointAt 0 s

0 commit comments

Comments
 (0)