@@ -11,10 +11,11 @@ extension UTF8Span {
11
11
public struct ScalarIterator : ~ Escapable {
12
12
public var codeUnits : UTF8Span
13
13
14
- /// The byte offset of the start of the next scalar. This is
14
+ /// The byte offset of the start of the next scalar. This is
15
15
/// always scalar-aligned.
16
16
///
17
17
/// **TODO**: private(set)?
18
+ fileprivate( set)
18
19
public var currentCodeUnitOffset : Int
19
20
20
21
// TODO: underscored init?
@@ -26,7 +27,7 @@ extension UTF8Span {
26
27
/// Decode and return the scalar starting at `currentCodeUnitOffset`.
27
28
/// After the function returns, `currentCodeUnitOffset` holds the
28
29
/// position at the end of the returned scalar, which is also the start
29
- /// of the next scalar.
30
+ /// of the next scalar.
30
31
///
31
32
/// Returns `nil` if at the end of the `UTF8Span`.
32
33
public mutating func next( ) -> Unicode . Scalar ? {
@@ -41,7 +42,7 @@ extension UTF8Span {
41
42
/// Decode and return the scalar ending at `currentCodeUnitOffset`. After
42
43
/// the function returns, `currentCodeUnitOffset` holds the position at
43
44
/// the start of the returned scalar, which is also the end of the
44
- /// previous scalar.
45
+ /// previous scalar.
45
46
///
46
47
/// Returns `nil` if at the start of the `UTF8Span`.
47
48
public mutating func previous( ) -> Unicode . Scalar ? {
@@ -53,42 +54,43 @@ extension UTF8Span {
53
54
return result
54
55
}
55
56
56
- // **QUESTION**: How should skip(by: Int) APIs be defined? Should they
57
- // implicitly clamp to start/end? Should they return the number of code
58
- // units skipped? number of scalars skipped?
59
- //
60
- // Code units skipped can be calculated by the caller, but scalars
61
- // skipped (if < n) is harder to figure out. For now, I just return a
62
- // Bool signaling if there weren't enough scalars.
63
57
64
58
/// Advance `codeUnitOffset` to the end of the current scalar, without
65
59
/// decoding it.
66
- public mutating func skipForward( ) -> Bool {
60
+ ///
61
+ /// Returns the number of `Unicode.Scalar`s skipped over, which can be 0
62
+ /// if at the end of the UTF8Span.
63
+ public mutating func skipForward( ) -> Int {
67
64
fatalError ( )
68
65
}
69
66
70
67
/// Advance `codeUnitOffset` to the end of `n` scalars, without decoding
71
68
/// them.
72
- public mutating func skipForward( by n: Int ) -> Bool {
69
+ ///
70
+ /// Returns the number of `Unicode.Scalar`s skipped over, which can be
71
+ /// fewer than `n` if at the end of the UTF8Span.
72
+ public mutating func skipForward( by n: Int ) -> Int {
73
73
fatalError ( )
74
74
}
75
75
76
76
/// Move `codeUnitOffset` to the start of the previous scalar, without
77
77
/// decoding it.
78
+ ///
79
+ /// Returns the number of `Unicode.Scalar`s skipped over, which can be 0
80
+ /// if at the start of the UTF8Span.
78
81
public mutating func skipBack( ) -> Bool {
79
82
fatalError ( )
80
83
}
81
84
82
85
/// Move `codeUnitOffset` to the start of the previous `n` scalars,
83
86
/// without decoding them.
87
+ ///
88
+ /// Returns the number of `Unicode.Scalar`s skipped over, which can be
89
+ /// fewer than `n` if at the start of the UTF8Span.
84
90
public mutating func skipBack( by n: Int ) -> Bool {
85
91
fatalError ( )
86
92
}
87
93
88
- // **QUESTION**: For reset rounding, should we return the rounded position as
89
- // a discardable result? That would make checking if rounding occurred
90
- // easier, though that might be better served by a isScalarAligned API.
91
-
92
94
/// Reset to the nearest scalar-aligned code unit offset `<= i`.
93
95
///
94
96
/// **TODO**: Example
@@ -124,13 +126,6 @@ extension UTF8Span {
124
126
self . currentCodeUnitOffset = i
125
127
}
126
128
127
- // **QUESTION**: Since UTF8Span can only be sliced on scalar-aligned
128
- // positions, and there are multiple levels of semantics to positions
129
- // (e.g. scalar-aligned, `Character`-aligned, `Grapheme-breaking
130
- // aligned`, I'm proposing having slicing be API on iterators rather
131
- // than `_extracting` on UTF8Span. But, is this the most useful
132
- // formulation?
133
-
134
129
/// Returns the UTF8Span containing all the content up to the iterator's
135
130
/// current position.
136
131
public func _prefix( ) -> UTF8Span {
@@ -146,8 +141,8 @@ extension UTF8Span {
146
141
}
147
142
148
143
@available ( SwiftStdlib 6 . 1 , * )
144
+ @_unavailableInEmbedded
149
145
extension UTF8Span {
150
-
151
146
public func _makeCharacterIterator( ) -> CharacterIterator {
152
147
. init( self )
153
148
}
@@ -168,24 +163,17 @@ extension UTF8Span {
168
163
/// occasionally be useful, but can yield counter intuitive results.
169
164
///
170
165
/// While we talk about code unit offsets always being scalar-aligned,
171
- /// should this type claim them to also be `Character` aligned as
172
- /// defined by the behavior of the iterator itself (i.e. the span is
173
- /// the entirety of the content)?
174
- ///
175
- /// You can get split-the-character behavior by getting the UTF8Span
176
- /// formed by `prefix/suffix` on the scalar iterator if you really want
177
- /// to, so I'm going with this is always `Character`-aligned under the
178
- /// intrepretation of `UTF8Span` as holding the entirety of the
179
- /// content.
166
+ /// we could go further to talk about `Character` aligned indices
167
+ /// (where `Character`-alignment is relative to the start of the
168
+ /// `UTF8Span`) and have API for those.
180
169
181
- /// The byte offset of the start of the next `Character`. This is
170
+ /// The byte offset of the start of the next `Character`. This is
182
171
/// always scalar-aligned and `Character`-aligned.
183
172
///
184
173
/// **TODO**: How to talk about the
185
174
/// assuming-the-UTF8Span-is-the-entire-content interpretation of
186
175
/// `Character`-aligned?
187
- ///
188
- /// **TODO**: private(set)?
176
+ fileprivate( set)
189
177
public var currentCodeUnitOffset : Int
190
178
191
179
// TODO: underscored init?
@@ -197,7 +185,7 @@ extension UTF8Span {
197
185
/// Return the `Character` starting at `currentCodeUnitOffset`. After the
198
186
/// function returns, `currentCodeUnitOffset` holds the position at the
199
187
/// end of the `Character`, which is also the start of the next
200
- /// `Character`.
188
+ /// `Character`.
201
189
///
202
190
/// Returns `nil` if at the end of the `UTF8Span`.
203
191
public mutating func next( ) -> Character ? {
@@ -215,7 +203,7 @@ extension UTF8Span {
215
203
/// Return the `Character` ending at `currentCodeUnitOffset`. After the
216
204
/// function returns, `currentCodeUnitOffset` holds the position at the
217
205
/// start of the returned `Character`, which is also the end of the
218
- /// previous `Character`.
206
+ /// previous `Character`.
219
207
///
220
208
/// Returns `nil` if at the start of the `UTF8Span`.
221
209
public mutating func previous( ) -> Character ? {
@@ -231,39 +219,56 @@ extension UTF8Span {
231
219
232
220
/// Advance `codeUnitOffset` to the end of the current `Character`,
233
221
/// without constructing it.
222
+ ///
223
+ /// Returns the number of `Character`s skipped over, which can be 0
224
+ /// if at the end of the UTF8Span.
234
225
public mutating func skipForward( ) {
235
226
fatalError ( )
236
227
}
237
228
238
229
/// Advance `codeUnitOffset` to the end of `n` `Characters`, without
239
230
/// constructing them.
240
- public mutating func skipForward( by n: Int ) { }
231
+ ///
232
+ /// Returns the number of `Character`s skipped over, which can be
233
+ /// fewer than `n` if at the end of the UTF8Span.
234
+ public mutating func skipForward( by n: Int ) {
235
+ fatalError ( )
236
+ }
241
237
242
238
/// Move `codeUnitOffset` to the start of the previous `Character`,
243
239
/// without constructing it.
240
+ ///
241
+ /// Returns the number of `Character`s skipped over, which can be 0
242
+ /// if at the start of the UTF8Span.
244
243
public mutating func skipBack( ) {
245
244
fatalError ( )
246
245
}
247
246
248
247
/// Move `codeUnitOffset` to the start of the previous `n` `Character`s,
249
248
/// without constructing them.
249
+ ///
250
+ /// Returns the number of `Character`s skipped over, which can be
251
+ /// fewer than `n` if at the start of the UTF8Span.
250
252
public mutating func skipBack( by n: Int ) {
253
+ fatalError ( )
251
254
}
252
255
253
256
/// Reset to the nearest character-aligned position `<= i`.
254
257
public mutating func reset( roundingBackwardsFrom i: Int ) {
258
+ fatalError ( )
255
259
}
256
260
257
261
/// Reset to the nearest character-aligned position `>= i`.
258
262
public mutating func reset( roundingForwardsFrom i: Int ) {
263
+ fatalError ( )
259
264
}
260
265
261
266
/// Reset this iterator to code unit offset `i`, skipping _all_ safety
262
267
/// checks.
263
268
///
264
269
/// Note: This is only for very specific, low-level use cases. If
265
270
/// `codeUnitOffset` is not properly scalar-aligned, this function can
266
- /// result in undefined behavior when, e.g., `next()` is called.
271
+ /// result in undefined behavior when, e.g., `next()` is called.
267
272
///
268
273
/// If `i` is scalar-aligned, but not `Character`-aligned, you may get
269
274
/// different results from running `Character` iteration.
@@ -272,6 +277,7 @@ extension UTF8Span {
272
277
/// known-valid previous position.
273
278
///
274
279
public mutating func reset( uncheckedAssumingAlignedTo i: Int ) {
280
+ fatalError ( )
275
281
}
276
282
277
283
/// Returns the UTF8Span containing all the content up to the iterator's
0 commit comments