Skip to content

Commit c9f47ff

Browse files
committed
Refresh for Swift 5.0
1 parent c185856 commit c9f47ff

22 files changed

+1221
-1228
lines changed

Array.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by John Holdsworth on 26/09/2015.
66
// Copyright © 2015 John Holdsworth. All rights reserved.
77
//
8-
// $Id: //depot/SwiftRuby/Array.swift#5 $
8+
// $Id: //depot/SwiftRuby/Array.swift#6 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -21,19 +21,19 @@ public protocol array_like {
2121
extension Array: array_like {
2222

2323
public var to_a: [String] {
24-
return map { String( describing: $0 ) }
24+
return map { String(describing: $0) }
2525
}
2626

27-
// public func join( sep: String = " " ) -> String {
28-
// return joinWithSeparator( sep )
27+
// public func join(sep: String = " ") -> String {
28+
// return joinWithSeparator(sep)
2929
// }
3030

3131
}
3232

3333
extension Collection {
3434

35-
public func each( _ block: (Iterator.Element) -> () ) {
36-
forEach( block )
35+
public func each(_ block: (Iterator.Element) -> ()) {
36+
forEach(block)
3737
}
3838

3939
}

Data.swift

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by John Holdsworth on 26/09/2015.
66
// Copyright © 2015 John Holdsworth. All rights reserved.
77
//
8-
// $Id: //depot/SwiftRuby/Data.swift#10 $
8+
// $Id: //depot/SwiftRuby/Data.swift#11 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -21,7 +21,7 @@ public protocol data_like {
2121
}
2222

2323
public func ==(lhs: Data, rhs: Data) -> Bool {
24-
return lhs.length == rhs.length && memcmp( lhs.bytes, rhs.bytes, lhs.length ) == 0
24+
return lhs.length == rhs.length && memcmp(lhs.bytes, rhs.bytes, lhs.length) == 0
2525
}
2626

2727
open class Data: RubyObject, string_like, array_like, char_like, data_like {
@@ -31,44 +31,44 @@ open class Data: RubyObject, string_like, array_like, char_like, data_like {
3131
open var length = 0 {
3232
didSet {
3333
if length > capacity {
34-
SRFatal( "Data length \(length) > capacity \(capacity)", file: #file, line: #line )
34+
SRFatal("Data length \(length) > capacity \(capacity)", file: #file, line: #line)
3535
}
3636
bytes[length] = 0
3737
}
3838
}
3939

4040
open var capacity = 0 {
4141
didSet {
42-
bytes = realloc( bytes, capacity+1 )!.assumingMemoryBound(to: Int8.self)
42+
bytes = realloc(bytes, capacity+1)!.assumingMemoryBound(to: Int8.self)
4343
}
4444
}
4545

46-
public init( bytes: UnsafeMutablePointer<Int8>, length: Int = 0 ) {
46+
public init(bytes: UnsafeMutablePointer<Int8>, length: Int = 0) {
4747
self.bytes = bytes
4848
self.length = length
4949
super.init()
5050
}
5151

52-
public convenience init( capacity: Int? = 0 ) {
52+
public convenience init(capacity: Int? = 0) {
5353
let capacity = capacity ?? 10 * 1024
54-
self.init( bytes: malloc( capacity+1 )!.assumingMemoryBound(to: Int8.self) )
54+
self.init(bytes: malloc(capacity+1)!.assumingMemoryBound(to: Int8.self))
5555
self.capacity = capacity
5656
}
5757

58-
public convenience init( array: [CChar] ) {
58+
public convenience init(array: [CChar]) {
5959
let alen = array.count
60-
self.init( capacity: alen )
61-
memcpy( bytes, array, alen )
60+
self.init(capacity: alen)
61+
memcpy(bytes, array, alen)
6262
length = alen-1
6363
}
6464

65-
open func append( _ extra: data_like ) -> Int {
65+
open func append(_ extra: data_like) -> Int {
6666
let extra = extra.to_d
6767
let required = length + extra.length
6868
if required + 1 > capacity {
69-
capacity += max( required - capacity, 10_000 )
69+
capacity += max(required - capacity, 10_000)
7070
}
71-
memcpy( bytes+length, extra.bytes, extra.length )
71+
memcpy(bytes+length, extra.bytes, extra.length)
7272
length += extra.length
7373
return extra.length
7474
}
@@ -78,8 +78,8 @@ open class Data: RubyObject, string_like, array_like, char_like, data_like {
7878
}
7979

8080
open var to_c: [CChar] {
81-
var data = [CChar]( repeating: 0, count: length+1 ) ///
82-
memcpy( &data, bytes, data.count )
81+
var data = [CChar](repeating: 0, count: length+1) ///
82+
memcpy(&data, bytes, data.count)
8383
return data
8484
}
8585

@@ -88,17 +88,17 @@ open class Data: RubyObject, string_like, array_like, char_like, data_like {
8888
}
8989

9090
open var to_s: String {
91-
if let string = String( cString: bytes, encoding: STRING_ENCODING ) {
91+
if let string = String(cString: bytes, encoding: STRING_ENCODING) {
9292
return string
9393
}
9494

95-
SRLog( "Data.to_s: Could not decode string from input" )
96-
return U(String( cString: bytes, encoding: FALLBACK_INPUT_ENCODING ))
95+
SRLog("Data.to_s: Could not decode string from input")
96+
return U(String(cString: bytes, encoding: FALLBACK_INPUT_ENCODING))
9797
}
9898

9999
deinit {
100100
if capacity != 0 {
101-
free( bytes )
101+
free(bytes)
102102
}
103103
}
104104

Dir.swift

+52-52
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by John Holdsworth on 28/09/2015.
66
// Copyright © 2015 John Holdsworth. All rights reserved.
77
//
8-
// $Id: //depot/SwiftRuby/Dir.swift#14 $
8+
// $Id: //depot/SwiftRuby/Dir.swift#15 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -21,73 +21,73 @@ open class Dir: RubyObject, array_like {
2121

2222
// Dir[ string [, string ...] ] → array
2323

24-
init?( dirname: string_like, file: StaticString = #file, line: UInt = #line ) {
24+
init?(dirname: string_like, file: StaticString = #file, line: UInt = #line) {
2525
dirpath = dirname.to_s
26-
unixDIR = opendir( dirpath )
26+
unixDIR = opendir(dirpath)
2727
super.init()
2828
if unixDIR == nil {
29-
SRError( "opendir '\(dirpath.to_s)' failed", file: file, line: line )
29+
SRError("opendir '\(dirpath.to_s)' failed", file: file, line: line)
3030
return nil
3131
}
3232
}
3333

3434
// MARK: Class Methods
3535

36-
open class func new( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Dir? {
37-
return Dir( dirname: string, file: file, line: line )
36+
open class func new(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? {
37+
return Dir(dirname: string, file: file, line: line)
3838
}
3939

40-
open class func open( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Dir? {
41-
return new( string, file: file, line: line )
40+
open class func open(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? {
41+
return new(string, file: file, line: line)
4242
}
4343

44-
open class func chdir( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
45-
return unixOK( "Dir.chdir '\(string.to_s)", Darwin.chdir( string.to_s ), file: file, line: line )
44+
open class func chdir(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
45+
return unixOK("Dir.chdir '\(string.to_s)", Darwin.chdir(string.to_s), file: file, line: line)
4646
}
4747

48-
open class func chroot( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
49-
return unixOK( "Dir.chroot '\(string.to_s)", Darwin.chroot( string.to_s ), file: file, line: line )
48+
open class func chroot(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
49+
return unixOK("Dir.chroot '\(string.to_s)", Darwin.chroot(string.to_s), file: file, line: line)
5050
}
5151

52-
open class func delete( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
53-
return unixOK( "Dir.rmdir '\(string.to_s)", Darwin.rmdir( string.to_s ), file: file, line: line )
52+
open class func delete(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
53+
return unixOK("Dir.rmdir '\(string.to_s)", Darwin.rmdir(string.to_s), file: file, line: line)
5454
}
5555

56-
open class func entries( _ dirname: string_like, file: StaticString = #file, line: UInt = #line ) -> [String] {
56+
open class func entries(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> [String] {
5757
var out = [String]()
58-
foreach( dirname ) {
58+
foreach(dirname) {
5959
(name) in
60-
out.append( name )
60+
out.append(name)
6161
}
6262
return out
6363
}
6464

65-
open class func exist( _ dirname: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
66-
return File.exist( dirname, file: file, line: line )
65+
open class func exist(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
66+
return File.exist(dirname, file: file, line: line)
6767
}
6868

69-
open class func exists( _ dirname: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
70-
return exist( dirname, file: file, line: line )
69+
open class func exists(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
70+
return exist(dirname, file: file, line: line)
7171
}
7272

73-
open class func foreach( _ dirname: string_like, _ block: (String) -> () ) {
74-
if let dir = Dir( dirname: dirname, file: #file, line: #line ) {
73+
open class func foreach(_ dirname: string_like, _ block: (String) -> ()) {
74+
if let dir = Dir(dirname: dirname, file: #file, line: #line) {
7575
dir.each {
7676
(name) in
77-
block( name )
77+
block(name)
7878
}
7979
}
8080
}
8181

8282
open class var getwd: String? {
83-
var cwd = [Int8]( repeating: 0, count: Int(PATH_MAX) )
84-
if !unixOK( "Dir.getwd", Darwin.getcwd( &cwd, cwd.count ) != nil ? 0 : 1, file: #file, line: #line ) {
83+
var cwd = [Int8](repeating: 0, count: Int(PATH_MAX))
84+
if !unixOK("Dir.getwd", Darwin.getcwd(&cwd, cwd.count) != nil ? 0 : 1, file: #file, line: #line) {
8585
return nil
8686
}
87-
return String( validatingUTF8: cwd )
87+
return String(validatingUTF8: cwd)
8888
}
8989

90-
open class func glob( _ pattern: string_like, _ flags: Int32 = 0, file: StaticString = #file, line: UInt = #line ) -> [String]? {
90+
open class func glob(_ pattern: string_like, _ flags: Int32 = 0, file: StaticString = #file, line: UInt = #line) -> [String]? {
9191
return pattern.to_s.withCString {
9292
var pglob = glob_t()
9393
if (unixOK("Dir.glob", Darwin.glob($0, flags, nil, &pglob), file: file, line: line)) {
@@ -98,61 +98,61 @@ open class Dir: RubyObject, array_like {
9898
}
9999
}
100100

101-
open class func home( _ user: string_like? = nil, file: StaticString = #file, line: UInt = #line ) -> String? {
101+
open class func home(_ user: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? {
102102
var user = user?.to_s
103-
var buff = [Int8]( repeating: 0, count: Int(PATH_MAX) )
103+
var buff = [Int8](repeating: 0, count: Int(PATH_MAX))
104104
var ret: UnsafeMutablePointer<passwd>?
105105
var info = passwd()
106106

107107
if user == nil || user == "" {
108-
if !unixOK( "Dir.getpwuid", getpwuid_r( geteuid(), &info, &buff, buff.count, &ret ), file: file, line: line ) {
108+
if !unixOK("Dir.getpwuid", getpwuid_r(geteuid(), &info, &buff, buff.count, &ret), file: file, line: line) {
109109
return nil
110110
}
111-
user = String( validatingUTF8: info.pw_name )
111+
user = String(validatingUTF8: info.pw_name)
112112
}
113113

114-
if !unixOK( "Dir.getpwnam \(user!.to_s)", getpwnam_r( user!, &info, &buff, buff.count, &ret ), file: file, line: line ) {
114+
if !unixOK("Dir.getpwnam \(user!.to_s)", getpwnam_r(user!, &info, &buff, buff.count, &ret), file: file, line: line) {
115115
return nil
116116
}
117117

118-
return String( validatingUTF8: info.pw_dir )
118+
return String(validatingUTF8: info.pw_dir)
119119
}
120120

121-
open class func mkdir( _ string: string_like, _ mode: Int = 0o755, file: StaticString = #file, line: UInt = #line ) -> Bool {
122-
return unixOK( "Dir.mkdir '\(string.to_s)", Darwin.mkdir( string.to_s, mode_t(mode) ), file: file, line: line )
121+
open class func mkdir(_ string: string_like, _ mode: Int = 0o755, file: StaticString = #file, line: UInt = #line) -> Bool {
122+
return unixOK("Dir.mkdir '\(string.to_s)", Darwin.mkdir(string.to_s, mode_t(mode)), file: file, line: line)
123123
}
124124

125125
open class var pwd: String? {
126126
return getwd
127127
}
128128

129-
open class func rmdir( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
130-
return delete( string, file: file, line: line )
129+
open class func rmdir(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
130+
return delete(string, file: file, line: line)
131131
}
132132

133-
open class func unlink( _ string: string_like, file: StaticString = #file, line: UInt = #line ) -> Bool {
134-
return delete( string, file: file, line: line )
133+
open class func unlink(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool {
134+
return delete(string, file: file, line: line)
135135
}
136136

137137
// MARK: Instance methods
138138

139139
@discardableResult
140-
open func close( _ file: StaticString = #file, line: UInt = #line ) -> Bool {
141-
let ok = unixOK( "Dir.closedir '\(dirpath)'", closedir( unixDIR ), file: file, line: line )
140+
open func close(_ file: StaticString = #file, line: UInt = #line) -> Bool {
141+
let ok = unixOK("Dir.closedir '\(dirpath)'", closedir(unixDIR), file: file, line: line)
142142
unixDIR = nil
143143
return ok
144144
}
145145

146146
@discardableResult
147-
open func each( _ block: (String) -> () ) -> Dir {
147+
open func each(_ block: (String) -> ()) -> Dir {
148148
while let name = read() {
149-
block( name )
149+
block(name)
150150
}
151151
return self
152152
}
153153

154154
open var fileno: Int {
155-
return Int(dirfd( unixDIR ))
155+
return Int(dirfd(unixDIR))
156156
}
157157

158158
open var inspect: String {
@@ -164,25 +164,25 @@ open class Dir: RubyObject, array_like {
164164
}
165165

166166
open var pos: Int {
167-
return Int(telldir( unixDIR ))
167+
return Int(telldir(unixDIR))
168168
}
169169

170170
open func read() -> String? {
171-
let ent = readdir( unixDIR )
171+
let ent = readdir(unixDIR)
172172
if ent != nil {
173173
return withUnsafeMutablePointer (to: &ent!.pointee.d_name) {
174-
String( validatingUTF8: UnsafeMutableRawPointer($0).assumingMemoryBound(to: CChar.self) )
174+
String(validatingUTF8: UnsafeMutableRawPointer($0).assumingMemoryBound(to: CChar.self))
175175
}
176176
}
177177
return nil
178178
}
179179

180180
open func rewind() {
181-
Darwin.rewinddir( unixDIR )
181+
Darwin.rewinddir(unixDIR)
182182
}
183183

184-
open func seek( _ pos: Int ) {
185-
seekdir( unixDIR, pos )
184+
open func seek(_ pos: Int) {
185+
seekdir(unixDIR, pos)
186186
}
187187

188188
open var tell: Int {
@@ -193,7 +193,7 @@ open class Dir: RubyObject, array_like {
193193
var out = [String]()
194194
each {
195195
(entry) in
196-
out .append( entry )
196+
out .append(entry)
197197
}
198198
return out
199199
}

0 commit comments

Comments
 (0)