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
44 lines (40 loc) · 1.38 KB
/
Modification.swift
File metadata and controls
44 lines (40 loc) · 1.38 KB
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
import Foundation
public enum Modification: Codable, Equatable {
case deleted(ClosedRange<Int>)
case inserted(Int, [String])
}
public extension [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: Swift.min(endIndex, index))
}
}
}
func applying(_ modifications: [Modification]) -> Array {
var newArray = self
newArray.apply(modifications)
return newArray
}
}
public extension NSMutableArray {
func apply(_ modifications: [Modification]) {
for modification in modifications {
switch modification {
case let .deleted(range):
if count == 0 { break }
let newRange = range.clamped(to: 0...(count - 1))
removeObjects(in: NSRange(newRange))
case let .inserted(index, strings):
for string in strings.reversed() {
insert(string, at: Swift.min(count, index))
}
}
}
}
}