You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Peek: return the next portion of input, if it exists and matches the given criteria
39
-
- Match: like peek, but also consumes the portion of input
40
-
- Parse: like match, but produces a value by interpreting the portion of input
45
+
@available(FoundationSpan 6.2,*)
46
+
extensionUTF8Span{
47
+
// This is just an iterator style type, though for UTF8 we can
48
+
// load scalars and Characters, presumably.
49
+
//
50
+
// NOTE: I'm calling this "Cursor" temporarily as "Iterator" might
51
+
// end up being taken for other reasons.
52
+
structCursor:~Escapable {
53
+
varspan:UTF8Span
54
+
varcurrentOffset:Int
55
+
56
+
@lifetime(copy span)
57
+
init(_ span:UTF8Span){
58
+
self.span = span
59
+
self.currentOffset =0
60
+
}
61
+
}
62
+
63
+
@lifetime(copy self) // copy or borrow?
64
+
func makeCursor()->Cursor{
65
+
.init(self)
66
+
}
67
+
}
68
+
69
+
@available(FoundationSpan 6.2,*)
70
+
extensionUTF8Span.Cursor{
71
+
@lifetime(self: copy self)
72
+
mutatingfunc uncheckedAdvance(){
73
+
assert(self.currentOffset < span.count)
74
+
self.currentOffset +=1
75
+
}
76
+
77
+
func peek()->UInt8?{
78
+
guard !isEmpty else{returnnil}
79
+
return span.span[unchecked:self.currentOffset]
80
+
}
41
81
42
-
Notes on return types:
43
-
`peek(_:(UInt8) -> Bool) -> UInt8?` is more descriptive than returning a `Bool`, but slighlty less ergonomic if you only care about the `Bool`. If we don't return the `UInt8`, some callers may need to store it from the function somehow or else double-load it.
44
-
Match functions have different return types, depending on whether they always succeed, whether they match a variable length, etc. Since they also advance as part of matching, the return lengths are dicardable. They can also be retroactively calculated by the caller, we just return it because we can.
45
-
Finally, the parse functions just return the value, as there's no way to have a discardable return value alongside a non-discardable one. Again, lengths can be retroactively calculated by the caller based on the iterator's new offset.
0 commit comments