Skip to content

Commit 8676e4f

Browse files
committed
Linux ping pong
1 parent b1324dc commit 8676e4f

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

Sources/PerfectLib/Utilities.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extension UInt8 {
120120
|| ( cc >= 123 && cc <= 126 )
121121
|| self == 43 )
122122
}
123-
123+
124124
// same as String(self, radix: 16)
125125
// but outputs two characters. i.e. 0 padded
126126
var hexString: String {
@@ -282,7 +282,7 @@ extension String {
282282

283283
public struct UUID {
284284
let uuid: uuid_t
285-
285+
286286
public init() {
287287
let u = UnsafeMutablePointer<UInt8>.allocate(capacity: sizeof(uuid_t.self))
288288
defer {
@@ -291,7 +291,7 @@ public struct UUID {
291291
uuid_generate_random(u)
292292
self.uuid = UUID.uuidFromPointer(u)
293293
}
294-
294+
295295
public init(_ string: String) {
296296
let u = UnsafeMutablePointer<UInt8>.allocate(capacity: sizeof(uuid_t.self))
297297
defer {
@@ -300,16 +300,16 @@ public struct UUID {
300300
uuid_parse(string, u)
301301
self.uuid = UUID.uuidFromPointer(u)
302302
}
303-
303+
304304
init(_ uuid: uuid_t) {
305305
self.uuid = uuid
306306
}
307-
307+
308308
private static func uuidFromPointer(_ u: UnsafeMutablePointer<UInt8>) -> uuid_t {
309309
// is there a better way?
310310
return uuid_t(u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15])
311311
}
312-
312+
313313
public var string: String {
314314
let u = UnsafeMutablePointer<UInt8>.allocate(capacity: sizeof(uuid_t.self))
315315
let unu = UnsafeMutablePointer<Int8>.allocate(capacity: 37) // as per spec. 36 + null
@@ -325,12 +325,12 @@ public struct UUID {
325325
}
326326

327327
extension String {
328-
328+
329329
@available(*, unavailable, message: "Use UUID(_:String)")
330330
public func asUUID() -> uuid_t {
331331
return UUID(self).uuid
332332
}
333-
333+
334334
@available(*, unavailable, message: "Use UUID.string")
335335
public static func fromUUID(uuid: uuid_t) -> String {
336336
return UUID(uuid).string
@@ -439,27 +439,30 @@ extension String {
439439

440440
extension String {
441441
var pathComponents: [String] {
442-
return URL(fileURLWithPath: self).pathComponents
442+
return URL(fileURLWithPath: self).pathComponents ?? [String]()
443443
}
444444

445445
var lastPathComponent: String {
446-
return URL(fileURLWithPath: self).lastPathComponent
446+
return URL(fileURLWithPath: self).lastPathComponent ?? ""
447447
}
448448

449449
var deletingLastPathComponent: String {
450-
return URL(fileURLWithPath: self).deletingLastPathComponent().path
450+
let pth = try? URL(fileURLWithPath: self).deletingLastPathComponent()
451+
return pth?.path ?? ""
451452
}
452-
453+
453454
var deletingPathExtension: String {
454-
return URL(fileURLWithPath: self).deletingPathExtension().path
455+
let pth = try? URL(fileURLWithPath: self).deletingPathExtension()
456+
return pth?.path ?? ""
455457
}
456458

457459
var pathExtension: String {
458-
return URL(fileURLWithPath: self).pathExtension
460+
return URL(fileURLWithPath: self).pathExtension ?? ""
459461
}
460462

461463
var resolvingSymlinksInPath: String {
462-
return URL(fileURLWithPath: self).resolvingSymlinksInPath().path
464+
let pth = try? URL(fileURLWithPath: self).resolvingSymlinksInPath()
465+
return pth?.path ?? ""
463466
}
464467
}
465468

@@ -524,7 +527,7 @@ public func formatDate(_ date: Double, format: String, timezone inTimezone: Stri
524527
}
525528

526529
extension UnicodeScalar {
527-
530+
528531
/// Returns true if the UnicodeScalar is a white space character
529532
public func isWhiteSpace() -> Bool {
530533
return isspace(Int32(self.value)) != 0
@@ -559,14 +562,14 @@ public extension NetNamedPipe {
559562
public func sendFile(_ file: File, callBack: (Bool) -> ()) throws {
560563
try self.sendFd(Int32(file.fd), callBack: callBack)
561564
}
562-
565+
563566
/// Receive an existing opened `File` descriptor from the sender
564567
/// - parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `File` object or nil.
565568
/// - throws: `PerfectError.NetworkError`
566569
public func receiveFile(callBack: (File?) -> ()) throws {
567570
try self.receiveFd {
568571
fd in
569-
572+
570573
if fd == invalidSocket {
571574
callBack(nil)
572575
} else {
@@ -582,9 +585,9 @@ extension String.UTF8View {
582585
var sha1: [UInt8] {
583586
let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(SHA_DIGEST_LENGTH))
584587
defer { bytes.deallocate(capacity: Int(SHA_DIGEST_LENGTH)) }
585-
588+
586589
SHA1(Array<UInt8>(self), (self.count), bytes)
587-
590+
588591
var r = [UInt8]()
589592
for idx in 0..<Int(SHA_DIGEST_LENGTH) {
590593
r.append(bytes[idx])

0 commit comments

Comments
 (0)