forked from swiftlang/swift-experimental-string-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.swift
48 lines (40 loc) · 1.09 KB
/
Misc.swift
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
extension FixedWidthInteger {
var hexStr: String {
String(self, radix: 16, uppercase: true)
}
}
func unreachable(_ s: @autoclosure () -> String) -> Never {
fatalError("unreachable \(s())")
}
func unreachable() -> Never {
fatalError("unreachable")
}
// From Algorithms...
extension Array /* for enumerated */ {
/// Returns a collection of subsequences of this collection, chunked by the
/// given predicate.
///
/// - Complexity: O(*n*), where *n* is the length of this collection.
public func chunked(
by belongInSameGroup: (Element, Element) throws -> Bool
) rethrows -> [SubSequence] {
guard !isEmpty else { return [] }
var result: [SubSequence] = []
var start = startIndex
var current = self[start]
for (index, element) in enumerated().dropFirst() {
if try !belongInSameGroup(current, element) {
result.append(self[start..<index])
start = index
}
current = element
}
if start != endIndex {
result.append(self[start...])
}
return result
}
}
extension Substring {
var string: String { String(self) }
}