-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathString+Convertible.swift
42 lines (37 loc) · 1.28 KB
/
String+Convertible.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
import CWebGPU
extension String: ConvertibleFromC, ConvertibleToCWithClosure {
typealias CType = WGPUStringView
init(cValue: CType) {
if cValue.length == WGPU_STRLEN {
self.init(cString: cValue.data)
} else {
let bytes = UnsafeRawBufferPointer(start: cValue.data, count: cValue.length)
self.init(decoding: bytes, as: UTF8.self)
}
}
func withCValue<R>(_ body: (CType) throws -> R) rethrows -> R {
var copy = self
return try copy.withUTF8 { utf8String in
return try utf8String.withMemoryRebound(to: CChar.self) { cString in
let cValue = CType(data: cString.baseAddress, length: cString.count)
return try body(cValue)
}
}
}
}
extension Optional<String> {
init(cValue: String.CType) {
if cValue.data == nil && cValue.length == WGPU_STRLEN {
self = nil
} else {
self = String(cValue: cValue)
}
}
func withCValue<R>(_ body: (String.CType) throws -> R) rethrows -> R {
if let value = self {
return try value.withCValue(body)
} else {
return try body(String.CType(data: nil, length: Int(bitPattern: UInt(WGPU_STRLEN))))
}
}
}