@@ -21,9 +21,19 @@ extension UTF8Span {
21
21
. init( self )
22
22
}
23
23
24
- // **TODO**: Examples in below doc
25
-
26
- /// Iterate the `Unicode.Scalar`s contents of a `UTF8Span`.
24
+ /// Iterate the `Unicode.Scalar`s contents of a `UTF8Span`.
25
+ ///
26
+ /// func printScalarValues(_ string: borrowing String) {
27
+ /// var iterator = string.utf8Span.makeUnicodeScalarIterator()
28
+ /// while let scalar = iterator.next() {
29
+ /// print(scalar.escaped(asASCII: true))
30
+ /// }
31
+ /// }
32
+ ///
33
+ /// let string = "A🎉"
34
+ /// printScalarValues(string)
35
+ /// // Prints "A"
36
+ /// // Prints "\u{0001F389}"
27
37
@frozen
28
38
public struct UnicodeScalarIterator : ~ Escapable {
29
39
public let codeUnits : UTF8Span
@@ -156,10 +166,18 @@ extension UTF8Span {
156
166
return numSkipped
157
167
}
158
168
159
- // TODO: Example for reset docs
160
-
161
169
/// Reset to the nearest scalar-aligned code unit offset `<= i`.
162
170
///
171
+ /// func printScalarAfterReset(_ string: borrowing String) {
172
+ /// var iterator = string.utf8Span.makeUnicodeScalarIterator()
173
+ /// iterator.reset(roundingBackwardsFrom: 8) // Position 8 is mid-emoji, rounds back to 6
174
+ /// if let scalar = iterator.next() {
175
+ /// print(scalar) // Prints "🌍" (emoji starts at byte 6)
176
+ /// }
177
+ /// }
178
+ /// let string = "Hello 🌍"
179
+ /// printScalarAfterReset(string)
180
+ ///
163
181
/// - Complexity: O(1)
164
182
@lifetime ( self : copy self )
165
183
public mutating func reset( roundingBackwardsFrom i: Int ) {
@@ -240,9 +258,26 @@ extension UTF8Span {
240
258
. init( self )
241
259
}
242
260
243
- // **TODO**: Examples in below doc
244
-
245
261
/// Iterate the `Character` contents of a `UTF8Span`.
262
+ ///
263
+ /// func countCharacters(_ string: borrowing String) {
264
+ /// var iterator = string.utf8Span.makeCharacterIterator()
265
+ /// var count = 0
266
+ /// while let character = iterator.next() {
267
+ /// count += 1
268
+ /// print("Character \(count): \(character)")
269
+ /// }
270
+ /// print("Total: \(count) characters")
271
+ /// }
272
+ ///
273
+ /// let string = "لاهور"
274
+ /// countCharacters(string)
275
+ /// // Prints "Character 1: ل"
276
+ /// // Prints "Character 2: ا"
277
+ /// // Prints "Character 3: ه"
278
+ /// // Prints "Character 4: و"
279
+ /// // Prints "Character 5: ر"
280
+ /// // Prints "Total: 5 characters"
246
281
public struct CharacterIterator : ~ Escapable {
247
282
public let codeUnits : UTF8Span
248
283
0 commit comments