1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
// TODO: Below should return an optional of what was eaten
extension Slice where Element: Equatable {
internal mutating func _eat(if p: (Element) -> Bool) -> Element? {
guard let s = self.first, p(s) else { return nil }
self = self.dropFirst()
return s
}
internal mutating func _eat(_ e: Element) -> Element? {
_eat(if: { $0 == e })
}
internal mutating func _eat(asserting e: Element) {
let p = _eat(e)
assert(p != nil)
}
internal mutating func _eat(count c: Int) -> Slice {
defer { self = self.dropFirst(c) }
return self.prefix(c)
}
internal mutating func _eatSequence<C: Collection>(_ es: C) -> Slice?
where C.Element == Element
{
guard self.starts(with: es) else { return nil }
return _eat(count: es.count)
}
internal mutating func _eatUntil(_ idx: Index) -> Slice {
precondition(idx >= startIndex && idx <= endIndex)
defer { self = self[idx...] }
return self[..<idx]
}
internal mutating func _eatThrough(_ idx: Index) -> Slice {
precondition(idx >= startIndex && idx <= endIndex)
guard idx != endIndex else {
defer { self = self[endIndex ..< endIndex] }
return self
}
defer { self = self[index(after: idx)...] }
return self[...idx]
}
// If `e` is present, eat up to first occurence of `e`
internal mutating func _eatUntil(_ e: Element) -> Slice? {
guard let idx = self.firstIndex(of: e) else { return nil }
return _eatUntil(idx)
}
// If `e` is present, eat up to and through first occurence of `e`
internal mutating func _eatThrough(_ e: Element) -> Slice? {
guard let idx = self.firstIndex(of: e) else { return nil }
return _eatThrough(idx)
}
// Eat any elements from the front matching the predicate
internal mutating func _eatWhile(_ p: (Element) -> Bool) -> Slice? {
let idx = firstIndex(where: { !p($0) }) ?? endIndex
guard idx != startIndex else { return nil }
return _eatUntil(idx)
}
}
|