-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNonEmpty+String.swift
108 lines (88 loc) · 3.71 KB
/
NonEmpty+String.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
public typealias NonEmptyString = NonEmpty<String>
extension NonEmptyString {
@_disfavoredOverload
public init?<T>(_ value: T) where T: LosslessStringConvertible {
self.init(String(value))
}
}
extension NonEmpty: ExpressibleByUnicodeScalarLiteral
where Collection: ExpressibleByUnicodeScalarLiteral {
public typealias UnicodeScalarLiteralType = Collection.UnicodeScalarLiteralType
public init(unicodeScalarLiteral value: Collection.UnicodeScalarLiteralType) {
self.init(rawValue: Collection(unicodeScalarLiteral: value))!
}
}
extension NonEmpty: ExpressibleByExtendedGraphemeClusterLiteral
where Collection: ExpressibleByExtendedGraphemeClusterLiteral {
public typealias ExtendedGraphemeClusterLiteralType = Collection
.ExtendedGraphemeClusterLiteralType
public init(extendedGraphemeClusterLiteral value: Collection.ExtendedGraphemeClusterLiteralType) {
self.init(rawValue: Collection(extendedGraphemeClusterLiteral: value))!
}
}
extension NonEmpty: ExpressibleByStringLiteral where Collection: ExpressibleByStringLiteral {
public typealias StringLiteralType = Collection.StringLiteralType
public init(stringLiteral value: Collection.StringLiteralType) {
self.init(rawValue: Collection(stringLiteral: value))!
}
}
extension NonEmpty: TextOutputStreamable where Collection: TextOutputStreamable {
public func write<Target>(to target: inout Target) where Target: TextOutputStream {
self.rawValue.write(to: &target)
}
}
extension NonEmpty: TextOutputStream where Collection: TextOutputStream {
public mutating func write(_ string: String) {
self.rawValue.write(string)
}
}
extension NonEmpty: LosslessStringConvertible where Collection: LosslessStringConvertible {
public init?(_ description: String) {
guard let string = Collection(description) else { return nil }
self.init(rawValue: string)
}
}
extension NonEmpty: ExpressibleByStringInterpolation
where
Collection: ExpressibleByStringInterpolation,
Collection.StringLiteralType == DefaultStringInterpolation.StringLiteralType
{}
extension NonEmpty where Collection: StringProtocol {
public typealias UTF8View = Collection.UTF8View
public typealias UTF16View = Collection.UTF16View
public typealias UnicodeScalarView = Collection.UnicodeScalarView
public var utf8: UTF8View { self.rawValue.utf8 }
public var utf16: UTF16View { self.rawValue.utf16 }
public var unicodeScalars: UnicodeScalarView { self.rawValue.unicodeScalars }
public init<C, Encoding>(
decoding codeUnits: C, as sourceEncoding: Encoding.Type
) where C: Swift.Collection, Encoding: _UnicodeEncoding, C.Element == Encoding.CodeUnit {
self.init(rawValue: Collection(decoding: codeUnits, as: sourceEncoding))!
}
public init(cString nullTerminatedUTF8: UnsafePointer<CChar>) {
self.init(rawValue: Collection(cString: nullTerminatedUTF8))!
}
public init<Encoding>(
decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>,
as sourceEncoding: Encoding.Type
) where Encoding: _UnicodeEncoding {
self.init(rawValue: Collection(decodingCString: nullTerminatedCodeUnits, as: sourceEncoding))!
}
public func withCString<Result>(_ body: (UnsafePointer<CChar>) throws -> Result) rethrows
-> Result
{
try self.rawValue.withCString(body)
}
public func withCString<Result, Encoding>(
encodedAs targetEncoding: Encoding.Type,
_ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result
) rethrows -> Result where Encoding: _UnicodeEncoding {
try self.rawValue.withCString(encodedAs: targetEncoding, body)
}
public func lowercased() -> NonEmptyString {
NonEmptyString(self.rawValue.lowercased())!
}
public func uppercased() -> NonEmptyString {
NonEmptyString(self.rawValue.uppercased())!
}
}