forked from johnno1962/SwiftTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftMeta.swift
686 lines (600 loc) · 25.4 KB
/
SwiftMeta.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//
// SwiftMeta.swift
// SwiftTwaceApp
//
// Created by John Holdsworth on 20/04/2020.
// Copyright © 2020 John Holdsworth. All rights reserved.
//
// Repo: https://github.com/johnno1962/SwiftTrace
// $Id: //depot/SwiftTrace/SwiftTrace/SwiftMeta.swift#89 $
//
// Requires https://github.com/johnno1962/StringIndex.git
//
// Assumptions made about Swift MetaData
// =====================================
//
import Foundation
#if SWIFT_PACKAGE
import SwiftTraceGuts
#endif
/**
Shenaniggans to be able to decorate any type linked into an app.
These functions have the SwiftMeta.FunctionTakingGenericValue
signature from a C perspective if you can find the address.
They need to be public at the top level when app is stripped.
*/
/// generic function to find the Optional type for a wrapped type
public func getOptionalType<Type>(value: Type, out: inout Any.Type?) {
out = Optional<Type>.self
}
/// generic function to find the Array type for an element type
public func getArrayType<Type>(value: Type, out: inout Any.Type?) {
out = Array<Type>.self
}
/// generic function to find the Array type for an element type
public func getPointerType<Type>(value: Type, out: inout Any.Type?) {
out = UnsafePointer<Type>.self
}
/// generic function to find the Array type for an element type
public func getMetaType<Type>(value: Type, out: inout Any.Type?) {
out = type(of: Type.self)
}
/// generic function to find the ArraySlice slice type for an element type
public func getArraySliceType<Type>(value: Type, out: inout Any.Type?) {
out = ArraySlice<Type>.self
}
/// generic function to find the Dictionary with String key for an element type
public func getDictionaryType<Type>(value: Type, out: inout Any.Type?) {
out = Dictionary<String, Type>.self
}
// generic function to find the MixedProperties type for a Type
public func getMixedType<Type>(value: Type, out: inout Any.Type?) {
out = SwiftMeta.MixedProperties<Type>.self
}
// generic function to find the MixedProperties type for a Type
public func getEnumType<Type>(value: Type, out: inout Any.Type?) {
out = SwiftMeta.EnumProperties<Type>.self
}
// generic function to find the Set type for a Hashable wrapped type
public func getSetType<Type: Hashable>(value: Type, out: inout Any.Type?) {
out = Set<Type>.self
}
/// generic function to find the Array type for an element type
public func getRangeType<Type: Comparable>(value: Type, out: inout Any.Type?) {
out = Range<Type>.self
}
// generic function to find the Foundation.Measurement type for a Unit
@available(OSX 10.12, iOS 10.0, tvOS 10.0, *)
public func getMeasurementType<Type: Unit>(value: Type, out: inout Any.Type?) {
out = Foundation.Measurement<Type>.self
}
public class SwiftMeta {
/// All types have this structure generated by the compiler
/// from which you can extract the size and stride.
struct ValueWitnessTable {
let initializeBufferWithCopyOfBuffer: IMP, destroy: IMP,
initializeWithCopy: IMP, assignWithCopy: IMP,
initializeWithTake: IMP, assignWithTake: IMP,
getEnumTagSinglePayload: IMP, storeEnumTagSinglePayload: IMP
let size: size_t, stride: size_t
let flags: uintptr_t
}
/**
Get the size in bytes of a type
*/
public class func sizeof(anyType: Any.Type) -> size_t {
let metaData = unsafeBitCast(anyType,
to: UnsafePointer<UnsafePointer<ValueWitnessTable>>.self)
return metaData[-1].pointee.size
}
/**
Get the stride in bytes of a type
*/
public class func strideof(anyType: Any.Type) -> size_t {
let metaData = unsafeBitCast(anyType,
to: UnsafePointer<UnsafePointer<ValueWitnessTable>>.self)
return metaData[-1].pointee.stride
}
/**
The signature of a function taking a generic type and an inout pointer.
The witnessTable is for when the type is constrained by a protocol.
*/
public typealias FunctionTakingGenericValue = @convention(c) (
_ valuePtr : UnsafeRawPointer?, _ outPtr: UnsafeMutableRawPointer,
_ metaType: UnsafeRawPointer, _ witnessTable: UnsafeRawPointer?) -> ()
/**
This can be used to call a Swift function with a generic value
argument when you have a pointer to the value and its type.
See: https://www.youtube.com/watch?v=ctS8FzqcRug
*/
public static func thunkToGeneric(funcPtr: FunctionTakingGenericValue,
valuePtr: UnsafeRawPointer?, outPtr: UnsafeMutableRawPointer,
type: Any.Type, witnessTable: UnsafeRawPointer? = nil) {
funcPtr(valuePtr, outPtr, autoBitCast(type), witnessTable)
}
/**
Definitions related to auto-tracability of types
*/
public static let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
public static func mangle(_ name: String) -> String {
return "\(name.utf8.count)\(name)"
}
/**
Find pointer for function processing type as generic
*/
public static func bindGeneric(name: String,
owner: Any.Type = SwiftMeta.self,
args: String = returnAnyType)
-> FunctionTakingGenericValue {
let module = _typeName(owner).components(separatedBy: ".")[0]
let symbol = "$s\(mangle(module))\(mangle(name))5value3outyx_\(args)lF"
guard let genericFunctionPtr = dlsym(RTLD_DEFAULT, symbol) else {
fatalError("Could lot locate generic function for symbol \(symbol)")
}
return autoBitCast(genericFunctionPtr)
}
/**
Generic function pointers that can be used to convert a type
into the Optional/Array/Set/Dictionary for that type.
*/
public static let returnAnyType = "ypXpSgzt"
static var getOptionalTypeFptr = bindGeneric(name: "getOptionalType")
static var getMixedTypeFptr = bindGeneric(name: "getMixedType")
static var getEnumTypeFptr = bindGeneric(name: "getEnumType")
static var getMetaTypeFptr = bindGeneric(name: "getMetaType")
/// Handled compund types
public static var wrapperHandlers = [
"Swift.Optional<": getOptionalTypeFptr,
"Swift.Array<": bindGeneric(name: "getArrayType"),
"Swift.ArraySlice<": bindGeneric(name: "getArraySliceType"),
"Swift.Set<": bindGeneric(name: "getSetType",
args: "ypXpSgztSHRz"),
"Swift.Range<": bindGeneric(name: "getRangeType",
args: "ypXpSgztSLRz"),
"Swift.UnsafePointer<": bindGeneric(name: "getPointerType"),
"Swift.UnsafeMutablePointer<": bindGeneric(name: "getPointerType"),
"Swift.Dictionary<Swift.String, ": bindGeneric(name: "getDictionaryType"),
"Foundation.Measurement<": bindGeneric(name: "getMeasurementType",
args: "ypXpSgztSo6NSUnitCRbz"),
]
public static var conformanceManglings = [
"Swift.Set<": "H",
"Swift.Range<": "L"
]
static func convert(type: Any.Type, handler: FunctionTakingGenericValue,
witnessTable: UnsafeRawPointer? = nil) -> Any.Type? {
var out: Any.Type?
thunkToGeneric(funcPtr: handler, valuePtr: nil, outPtr: &out,
type: type, witnessTable: witnessTable)
return out
}
public static var nameAbbreviations = [
"Swift": "s"
]
public static var typeLookupCache: [String: Any.Type?] = [
// These types have non-standard manglings
"Swift.String": String.self,
"Swift.Double": Double.self,
"Swift.Float": Float.self,
// Special cases
"Any": Any.self, // not reliable
"Any.Type": Any.Type.self,
"Swift.AnyObject": AnyObject.self,
"Swift.AnyObject.Type": AnyClass.self,
"Swift.Optional<Swift.Error>": Error?.self,
"Swift.Error": Error.self,
"()": Void.self,
"some": nil,
// Has private enum property containg a Locale
"Fruta.ContentView" : nil,
// Also uses Foundation type inside enum
"Kingfisher.ExpirationExtending": nil,
]
static var typeLookupCacheLock = OS_SPINLOCK_INIT
/**
Fake types used to prevent decorating when unsupported
*/
public struct MixedProperties<Type>: UnsupportedTyping,
CustomStringConvertible {
public var description: String {
return _typeName(Type.self)+"()"
}
}
public struct EnumProperties<Type>: UnsupportedTyping,
CustomStringConvertible {
public var description: String {
return _typeName(Type.self)+"()"
}
}
/**
Best effort recovery of type from a qualified name
*/
public static func lookupType(named: String,
exclude: NSRegularExpression? = nil) -> Any.Type? {
OSSpinLockLock(&typeLookupCacheLock)
defer { OSSpinLockUnlock(&typeLookupCacheLock) }
return lockedType(named: named, exclude: exclude)
}
static func lockedType(named: String,
exclude: NSRegularExpression? = nil) -> Any.Type? {
if exclude?.matches(named) == true {
return nil
}
if let type = typeLookupCache[named] {
return type
}
var out: Any.Type?
for (prefix, handler) in wrapperHandlers where named.hasPrefix(prefix) {
if let wrapped = named[safe: .start+prefix.count ..< .end-1],
let wrappedType = SwiftMeta.lockedType(named: wrapped,
exclude: exclude) {
if let enc = conformanceManglings[prefix] {
if let witnessTable =
getWitnessTable(enc: enc, for: wrappedType) {
out = convert(type: wrappedType, handler: handler,
witnessTable: witnessTable)
}
} else {
out = convert(type: wrappedType, handler: handler)
}
}
break
}
if named.hasSuffix("..."),
let element = named[safe: ..<(.end-3)] {
out = lockedType(named: "Swift.Array<\(element)>")
} else if named.hasSuffix(".Type"),
let element = named[safe: ..<(.end-5)],
let elementType = lockedType(named: element) {
out = convert(type: elementType, handler: getMetaTypeFptr)
} else if out == nil {
var mangled = ""
var first = true
for name in named.components(separatedBy: ".") {
mangled += nameAbbreviations[name] ?? mangle(name)
out = nil
if first {
first = false
continue
}
if let type = _typeByName(mangled+"C") {
mangled += "C" // class type
out = type
} else if let type = _typeByName(mangled+"V") {
mangled += "V" // value type
out = type
} else if let type = _typeByName(mangled+"O") {
mangled += "O" // enum type
out = type
// } else if let type = _typeByName(mangled+"P") {
// mangled += "P" // prptocol
// out = type
} else {
break
}
}
}
typeLookupCache[named] = out
return out
}
/**
Take the symbol name for the metaType address and remove the "N" suffix
*/
public static func mangledName(for type: Any.Type) -> String? {
var info = Dl_info()
if dladdr(autoBitCast(type), &info) != 0,
let metaTypeSymbol = info.dli_sname {
return String(cString: metaTypeSymbol)[safe: ..<(.end-1)]
}
return nil
}
/**
Find the witness table for the conformance of elementType to Hashable
*/
static func getWitnessTable(enc: String, for elementType: Any.Type)
-> UnsafeRawPointer? {
var witnessTable: UnsafeRawPointer?
if let mangledName = mangledName(for: elementType) {
if let theEasyWay = dlsym(RTLD_DEFAULT, mangledName+"S\(enc)sWP") {
witnessTable = UnsafeRawPointer(theEasyWay)
} else {
let witnessSuffix = "ACS\(enc)AAWl"
(mangledName + witnessSuffix).withCString { getWitnessSymbol in
typealias GetWitness = @convention(c) () -> UnsafeRawPointer
findHiddenSwiftSymbols(nil, witnessSuffix, ST_HIDDEN_VISIBILITY) {
(address, symbol, _, _) in
if strcmp(symbol, getWitnessSymbol) == 0,
let witnessFptr: GetWitness = autoBitCast(address) {
witnessTable = witnessFptr()
}
}
}
}
}
return witnessTable
}
/**
Information about a field of a struct or class
*/
public struct FieldInfo {
let name: String
let type: Any.Type
let offset: size_t
}
/**
Get approximate nformation about the fields of a type
*/
public static func fieldInfo(forAnyType: Any.Type) -> [FieldInfo]? {
_ = structsPassedByReference
return approximateFieldInfoByTypeName[_typeName(forAnyType)]
}
static var approximateFieldInfoByTypeName = [String: [FieldInfo]]()
static var doesntHaveStorage = Set<String>()
public static var structsPassedByReference: Set<UnsafeRawPointer> = {
var problemTypes = Set<UnsafeRawPointer>()
func passedByReference(_ type: Any.Type) {
problemTypes.insert(autoBitCast(type))
if let type = convert(type: type, handler: getOptionalTypeFptr) {
problemTypes.insert(autoBitCast(type))
}
}
for type: Any.Type in [URL.self, UUID.self, Date.self,
IndexPath.self, IndexSet.self, URLRequest.self] {
passedByReference(type)
}
for iOS15ResilientTypeName in ["Foundation.AttributedString",
"Foundation.AttributedString.Index"] {
if let resilientType = lookupType(named: iOS15ResilientTypeName) {
passedByReference(resilientType)
}
}
#if true // Attempts to determine which getters have storage
// properties that have key path getters are not stored??
findHiddenSwiftSymbols(nil, "pACTK", ST_HIDDEN_VISIBILITY) { (_, symbol, _, _) in
doesntHaveStorage.insert(String(cString: symbol)
.replacingOccurrences(of: "pACTK", with: "g"))
}
// ...unless they have a field offset
// ...or property wrapper backing initializer ??
for suffix in ["pWvd", "pfP"] {
findSwiftSymbols(nil, suffix) { (_, symbol, _, _) in
doesntHaveStorage.remove(String(cString: symbol)
.replacingOccurrences(of: suffix, with: "g"))
}
}
// print(doesntHaveStorage)
#endif
if let swiftUIFramework = swiftUIBundlePath() {
process(bundlePath: swiftUIFramework, problemTypes: &problemTypes)
}
appBundleImages { bundlePath, _, _ in
process(bundlePath: bundlePath, problemTypes: &problemTypes)
}
passedByReference(Any.self)
// print(problemTypes.map {unsafeBitCast($0, to: Any.Type.self)}, approximateFieldInfoByTypeName)
return problemTypes
}()
/**
Structs that have only fields that conform to .SwiftTraceFloatArg
*/
static var structsAllFloats = Set<UnsafeRawPointer>()
/**
Ferforms a one time scan of all property getters at a bundlePath to
look out for structs that are or contain bridged(?) values such as URL
or UUID and are passed by reference by the compiler for some reason.
*/
public static func process(bundlePath: UnsafePointer<Int8>,
problemTypes: UnsafeMutablePointer<Set<UnsafeRawPointer>>) {
var offset = 0
var currentType = ""
var wasFloatType = false
var symbols = [(symval: UnsafeRawPointer, symname: UnsafePointer<Int8>)]()
findSwiftSymbols(bundlePath, "g") { (symval, symbol, _, _) in
symbols.append((symval, symbol))
}
// Need to process symbols in emitted order if we are
// to have any hope of recovering type memory layout.
for (_, symbol) in symbols.sorted(by: { $0.symval < $1.symval }) {
guard let demangled = SwiftMeta.demangle(symbol: symbol) else {
print("Could not demangle: \(String(cString: symbol))")
continue
}
func debug(_ str: @autoclosure () -> String) {
// print(demangled)
// print(str())
}
guard let fieldStart = demangled.index(of: .first(of: " : ")+3),
let nameEnd = demangled.index(of: fieldStart + .last(of: ".")),
let typeEnd = demangled.index(of: nameEnd + .last(of: ".")),
let typeName = demangled[..<typeEnd][safe:
(.last(of: ":")+1 || .start)...],
let fieldName = demangled[safe: typeEnd+1 ..< nameEnd],
let fieldTypeName = demangled[safe: (fieldStart+0)...] else {
debug("Could not parse: \(demangled)")
continue
}
guard let type = SwiftMeta.lookupType(named: typeName) else {
debug("Could not lookup type: \(typeName)")
continue
}
// debug("\(typeName).\(fieldName): \(fieldTypeName)")
let typeIsClass = type is AnyClass
let symend = symbol+strlen(symbol)
if strcmp(symend-3, "Ovg") == 0 || // enum
strcmp(symend-5, "OSgvg") == 0, !typeIsClass {
debug("\(typeName) enum prop \(fieldTypeName)")
if let _ = typeLookupCache[typeName] {} else {
if !(type is UnsupportedTyping.Type) {
typeLookupCache[typeName] =
convert(type: type, handler: getEnumTypeFptr)
}
}
continue
}
func nextType(floatField: Bool) {
if currentType != typeName {
currentType = typeName
wasFloatType = floatField
approximateFieldInfoByTypeName[typeName] = [FieldInfo]()
offset = type is AnyClass ? 8 * 3 : 0
if floatField && !typeIsClass {
structsAllFloats.insert(autoBitCast(type))
}
}
}
guard let fieldType = SwiftMeta.lookupType(named: fieldTypeName) else {
debug("Could not lookup field type: \"\(fieldTypeName)\", \(demangled) ")
nextType(floatField: false)
continue
}
let isFloatField = fieldType is SwiftTraceFloatArg.Type
nextType(floatField: isFloatField)
// Ignore non stored properties
if doesntHaveStorage.contains(String(cString: symbol)) {
debug("No Storage \(typeName).\(fieldName)")
continue
}
let strideMinus1 = strideof(anyType: fieldType) - 1
offset = (offset + strideMinus1) & ~strideMinus1
approximateFieldInfoByTypeName[typeName]?.append(
FieldInfo(name: fieldName, type: fieldType, offset: offset))
offset += sizeof(anyType: fieldType)
if !isFloatField {
structsAllFloats.remove(autoBitCast(type))
}
if typeIsClass {
continue
}
if isFloatField != wasFloatType &&
!(type is SwiftTraceFloatArg.Type) &&
!problemTypes.pointee.contains(autoBitCast(type)) {
debug("\(typeName) Mixed properties")
if !(type is UnsupportedTyping.Type) {
typeLookupCache[typeName] =
convert(type: type, handler: getMixedTypeFptr)
}
}
func passedByReference(_ type: Any.Type) {
problemTypes.pointee.insert(autoBitCast(type))
}
if problemTypes.pointee.contains(autoBitCast(fieldType)) ||
fieldTypeName.hasPrefix("Foundation.Measurement<") {
debug("\(typeName) Reference prop \(fieldTypeName)")
// typeLookupCache[typeName] = PreventLookup
passedByReference(type)
passedByReference(fieldType)
} else if let optional = fieldType as? OptionalTyping.Type,
problemTypes.pointee.contains(autoBitCast(optional.wrappedType)) {
debug("\(typeName) Reference optional prop \(fieldTypeName)")
passedByReference(type)
passedByReference(fieldType)
}
}
}
/** pointer to a function implementing a Swift method */
public typealias SIMP = @convention(c) () -> Void
/**
Value that crops up as a ClassSize since 5.2 runtime
*/
static let invalidClassSize = 0x50AF17B0
/**
Layout of a class instance. Needs to be kept in sync with ~swift/include/swift/Runtime/Metadata.h
*/
public struct TargetClassMetadata {
let MetaClass: uintptr_t = 0, SuperClass: uintptr_t = 0
let CacheData1: uintptr_t = 0, CacheData2: uintptr_t = 0
public let Data: uintptr_t = 0
/// Swift-specific class flags.
public let Flags: UInt32 = 0
/// The address point of instances of this type.
public let InstanceAddressPoint: UInt32 = 0
/// The required size of instances of this type.
/// 'InstanceAddressPoint' bytes go before the address point;
/// 'InstanceSize - InstanceAddressPoint' bytes go after it.
public let InstanceSize: UInt32 = 0
/// The alignment mask of the address point of instances of this type.
public let InstanceAlignMask: UInt16 = 0
/// Reserved for runtime use.
public let Reserved: UInt16 = 0
/// The total size of the class object, including prefix and suffix
/// extents.
public let ClassSize: UInt32 = 0
/// The offset of the address point within the class object.
public let ClassAddressPoint: UInt32 = 0
/// An out-of-line Swift-specific description of the type, or null
/// if this is an artificial subclass. We currently provide no
/// supported mechanism for making a non-artificial subclass
/// dynamically.
public let Description: uintptr_t = 0
/// A function for destroying instance variables, used to clean up
/// after an early return from a constructor.
public var IVarDestroyer: SIMP? = nil
// After this come the class members, laid out as follows:
// - class members for the superclass (recursively)
// - metadata reference for the parent, if applicable
// - generic parameters for this class
// - class variables (if we choose to support these)
// - "tabulated" virtual methods
}
/**
Convert a executable symbol name "mangled" according to Swift's
conventions into a human readable Swift language form
*/
@objc open class func demangle(symbol: UnsafePointer<Int8>) -> String? {
if let demangledNamePtr = _stdlib_demangleImpl(
symbol, mangledNameLength: UInt(strlen(symbol)),
outputBuffer: nil, outputBufferSize: nil, flags: 0) {
let demangledName = String(cString: demangledNamePtr)
free(demangledNamePtr)
return demangledName
}
return nil
}
}
// Taken from stdlib, not public Swift3+
@_silgen_name("swift_demangle")
private
func _stdlib_demangleImpl(
_ mangledName: UnsafePointer<CChar>?,
mangledNameLength: UInt,
outputBuffer: UnsafeMutablePointer<UInt8>?,
outputBufferSize: UnsafeMutablePointer<UInt>?,
flags: UInt32
) -> UnsafeMutablePointer<CChar>?
/**
Used to deocrate optioanals without wrapping value in Opotional()
*/
protocol OptionalTyping {
static var wrappedType: Any.Type { get }
static func describe(optionalPtr: UnsafeRawPointer, out: inout String)
}
extension Optional: OptionalTyping {
static var wrappedType: Any.Type { return Wrapped.self }
static func describe(optionalPtr: UnsafeRawPointer, out: inout String) {
if var value = optionalPtr.load(as: Wrapped?.self) {
// Slight coupling to SwiftArgs.swift here alas
SwiftTrace.Decorated.describe(&value, type: Wrapped.self, out: &out)
} else {
out += "nil"
}
}
}
protocol UnsupportedTyping {}
/**
Convenience extension to trap regex errors and report them
*/
extension NSRegularExpression {
convenience init(regexp: String) {
do {
try self.init(pattern: regexp)
}
catch let error as NSError {
fatalError("Invalid regexp: \(regexp): \(error.localizedDescription)")
}
}
func matches(_ string: String) -> Bool {
return rangeOfFirstMatch(in: string,
range: NSRange(string.startIndex ..< string.endIndex,
in: string)).location != NSNotFound
}
}