-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathUtils.swift
More file actions
88 lines (76 loc) · 2.8 KB
/
Utils.swift
File metadata and controls
88 lines (76 loc) · 2.8 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Foundation
extension InputStream {
/// Stream for reading from stdin.
public static var standardInput: InputStream {
InputStream(fileAtPath: "/dev/stdin")!
}
}
extension Data {
/// Initialize Data by reading entire input stream.
/// - parameter stream: Stream to read.
/// - parameter chunk: Chunk size.
/// - throws: An `NSError` representing the stream error.
init(stream: InputStream, chunk: Int) throws {
stream.open()
defer { stream.close() }
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: chunk)
defer { buffer.deallocate() }
self.init()
while stream.hasBytesAvailable {
let count = stream.read(buffer, maxLength: chunk)
guard count > 0 else {
if let error = stream.streamError { throw error }
break
}
self.append(buffer, count: count)
}
}
}
extension DateFormatter {
/// `DateFormatter` for ISO 8610 date formats.
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.calendar = Calendar(identifier: .iso8601)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
return formatter
}()
}
infix operator ?>
extension Optional {
/// Checks whether the value exists. If so, it returns the value; if not, it throws the given error.
/// - parameter lhs: Optional value to check for existance.
/// - parameter rhs: Swift error to throw in case of no value.
/// - returns: The value (non-optional) passed as parameter.
/// - throws: The Swift error returned on the right hand-side autoclosure.
@_transparent static func ?>(lhs: Self, rhs: @autoclosure ()->Swift.Error) throws -> Wrapped {
switch lhs {
case .some(let v): return v
case .none: throw rhs()
}
}
}
extension Array where Element:Hashable {
/// Creates a lookup dictionary for the receiving row.
///
/// In case of two array element with the same hash value, the closure is executed and the generated error is thrown.
/// - parameter error: The error being thrown on hash value collisions.
func lookupDictionary(onCollision error: ()->Swift.Error) throws -> [Int:Int] {
var lookup: [Int:Int] = Dictionary(minimumCapacity: self.count)
for (index, header) in self.enumerated() {
let hash = header.hashValue
guard case .none = lookup.updateValue(index, forKey: hash) else { throw error() }
}
return lookup
}
}
extension Sequence where Element : Hashable {
/// Creates a dictionary mapping elements of the sequence to the number of times they occur in the sequence.
/// - returns: The dictionary of occurence counts.
func occurenceCounts() -> [Element: Int] {
reduce(into: [:]) { partialResult, element in
partialResult[element, default: 0] += 1
}
}
}