forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModification.swift
More file actions
27 lines (24 loc) · 846 Bytes
/
Modification.swift
File metadata and controls
27 lines (24 loc) · 846 Bytes
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
import Foundation
public enum Modification: Codable, Equatable {
case deleted(ClosedRange<Int>)
case inserted(Int, [String])
}
public extension Array where Element == String {
mutating func apply(_ modifications: [Modification]) {
for modification in modifications {
switch modification {
case let .deleted(range):
if isEmpty { break }
let removingRange = range.lowerBound ..< (range.upperBound + 1)
removeSubrange(removingRange.clamped(to: 0 ..< endIndex))
case let .inserted(index, strings):
insert(contentsOf: strings, at: index)
}
}
}
func applying(_ modifications: [Modification]) -> Array {
var newArray = self
newArray.apply(modifications)
return newArray
}
}