forked from swiftlang/swift-experimental-string-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc_2.swift
128 lines (111 loc) · 3.04 KB
/
Misc_2.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
extension CustomStringConvertible {
@_alwaysEmitIntoClient
public var halfWidthCornerQuoted: String {
"「\(self)」"
}
}
//extension String: Error {}
extension Array {
@_alwaysEmitIntoClient
public init(singleElement e: Element) {
self.init(repeating: e, count: 1)
}
}
@_alwaysEmitIntoClient
public func joined<T>(_ elements: [[T]]) -> [T] {
Array(elements.joined())
}
extension Array {
@_alwaysEmitIntoClient
public init(reservingCapacity cap: Int) {
self.init()
self.reserveCapacity(cap)
}
}
extension Sequence {
@_alwaysEmitIntoClient
public func all(_ f: (Element) -> Bool) -> Bool {
for element in self {
guard f(element) else { return false }
}
return true
}
@_alwaysEmitIntoClient
public func none(_ f: (Element) -> Bool) -> Bool {
return self.all { !f($0) }
}
@_alwaysEmitIntoClient
public func any(_ f: (Element) -> Bool) -> Bool {
for element in self {
if f(element) { return true }
}
return false
}
}
extension Range {
public var destructure: (
lowerBound: Bound, upperBound: Bound
) {
(lowerBound, upperBound)
}
}
public typealias Offsets = (lower: Int, upper: Int)
extension BidirectionalCollection {
public func mapOffsets(_ offsets: Offsets) -> Range<Index> {
assert(offsets.lower >= 0 && offsets.upper <= 0)
let lower = index(startIndex, offsetBy: offsets.lower)
let upper = index(endIndex, offsetBy: offsets.upper)
return lower ..< upper
}
// Is this the right name?
public func flatmapOffsets(_ offsets: Offsets?) -> Range<Index> {
if let o = offsets {
return mapOffsets(o)
}
return startIndex ..< endIndex
}
}
extension Collection {
public func index(atOffset i: Int) -> Index {
index(startIndex, offsetBy: i)
}
public func offset(ofIndex index: Index) -> Int {
distance(from: startIndex, to: index)
}
public func split(
around r: Range<Index>
) -> (prefix: SubSequence, SubSequence, suffix: SubSequence) {
(self[..<r.lowerBound], self[r], self[r.upperBound...])
}
public func offset(of i: Index) -> Int {
distance(from: startIndex, to: i)
}
public func convertByOffset<
C: Collection
>(_ range: Range<Index>, in c: C) -> Range<C.Index> {
convertByOffset(range.lowerBound, in: c) ..<
convertByOffset(range.upperBound, in: c)
}
public func convertByOffset<
C: Collection
>(_ idx: Index, in c: C) -> C.Index {
c.index(atOffset: offset(of: idx))
}
}
extension UnsafeMutableRawPointer {
public func roundedUp<T>(toAlignmentOf type: T.Type) -> Self {
let alignmentMask = MemoryLayout<T>.alignment - 1
let rounded = (Int(bitPattern: self) + alignmentMask) & ~alignmentMask
return UnsafeMutableRawPointer(bitPattern: rounded).unsafelyUnwrapped
}
}
extension String {
public func isOnGraphemeClusterBoundary(_ i: Index) -> Bool {
String.Index(i, within: self) != nil
}
public init<Scalars: Collection>(
_ scs: Scalars
) where Scalars.Element == Unicode.Scalar {
self.init(decoding: scs.map { $0.value }, as: UTF32.self)
}
}