Skip to content

Commit c185856

Browse files
committed
Swift 4.2
1 parent 29bc206 commit c185856

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

Dir.swift

+10-10
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#7 $
8+
// $Id: //depot/SwiftRuby/Dir.swift#14 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -87,15 +87,15 @@ open class Dir: RubyObject, array_like {
8787
return String( validatingUTF8: cwd )
8888
}
8989

90-
open class func glob( _ pattern: string_like, _ root: String = ".", file: StaticString = #file, line: UInt = #line ) -> [String]? {
91-
let regex = pattern.to_s
92-
.replacingOccurrences( of: ".", with: "\\." )
93-
.replacingOccurrences( of: "**", with: "___" )
94-
.replacingOccurrences( of: "*", with: "[^/]*" )
95-
.replacingOccurrences( of: "?", with: "[^/]" )
96-
.replacingOccurrences( of: "___", with: ".*" )
97-
let command = "cd \"\(root)\" && find -E . -regex \"^(./)?\(regex)$\"| sed -e s/^.\\\\///"
98-
return IO.popen( command, file: file, line: line )?.readlines()
90+
open class func glob( _ pattern: string_like, _ flags: Int32 = 0, file: StaticString = #file, line: UInt = #line ) -> [String]? {
91+
return pattern.to_s.withCString {
92+
var pglob = glob_t()
93+
if (unixOK("Dir.glob", Darwin.glob($0, flags, nil, &pglob), file: file, line: line)) {
94+
defer { globfree(&pglob) }
95+
return (0..<Int(pglob.gl_matchc)).map { String(cString: U(U(pglob.gl_pathv)[$0])) }
96+
}
97+
return nil
98+
}
9999
}
100100

101101
open class func home( _ user: string_like? = nil, file: StaticString = #file, line: UInt = #line ) -> String? {

File.swift

+4-4
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/File.swift#11 $
8+
// $Id: //depot/SwiftRuby/File.swift#12 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -437,9 +437,9 @@ open class File : IO {
437437
return birthtime
438438
}
439439

440-
open func flock( _ locking_constant: Int, file: StaticString = #file, line: UInt = #line ) -> Bool {
441-
return unixOK( "File.flock '\(filepath)' \(locking_constant)", Darwin.flock( Int32(fileno), Int32(locking_constant) ), file: file, line: line )
442-
}
440+
// open func flock( _ locking_constant: Int, file: StaticString = #file, line: UInt = #line ) -> Bool {
441+
// return unixOK( "File.flock '\(filepath)' \(locking_constant)", Darwin.flock( Int32(fileno), Int32(locking_constant) ), file: file, line: line )
442+
// }
443443

444444
open var lstat: Stat? {
445445
return Stat( filepath, statLink: true, file: #file, line: #line )

Regexp.swift

+10-10
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/Regexp.swift#10 $
8+
// $Id: //depot/SwiftRuby/Regexp.swift#11 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -14,13 +14,13 @@
1414

1515
import Foundation
1616

17-
infix operator =~ { associativity left precedence 135 }
17+
infix operator =~ : ComparisonPrecedence
1818

1919
public func =~ ( lhs: String, rhs: String ) -> Regexp {
2020
return Regexp( target: lhs as NSString, pattern: rhs )
2121
}
2222

23-
infix operator !~ { associativity left precedence 135 }
23+
infix operator !~ : ComparisonPrecedence
2424

2525
public func !~ ( lhs: String, rhs: String ) -> NotRegexp {
2626
return NotRegexp( target: lhs as NSString, pattern: rhs )
@@ -184,16 +184,16 @@ open class Regexp: RubyObject {
184184
func groupsForMatch( _ match: NSTextCheckingResult ) -> [String?] {
185185
var groups = [String?]()
186186
for groupno in 0...regexp.numberOfCaptureGroups {
187-
groups.append( substring( match.rangeAt( groupno ) ) )
187+
groups.append( substring( match.range( at: groupno ) ) )
188188
}
189189
return groups
190190
}
191191

192192
open func dictionary( _ options: NSRegularExpression.MatchingOptions? = nil ) -> [String:String] {
193193
var out = [String:String]()
194194
for match in matchResults(options) {
195-
out[substring(match.rangeAt(1))!] =
196-
substring(match.rangeAt(2))!
195+
out[substring(match.range(at: 1))!] =
196+
substring(match.range(at: 2))!
197197
}
198198
return out
199199
}
@@ -215,14 +215,14 @@ open class Regexp: RubyObject {
215215

216216
open subscript ( groupno: Int ) -> String? {
217217
if let match = regexp.firstMatch( in: target as String, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange ) {
218-
return substring( match.rangeAt( groupno ) )
218+
return substring( match.range( at: groupno ) )
219219
}
220220
return nil
221221
}
222222

223223
open subscript ( groupno: Int, options: NSRegularExpression.MatchingOptions ) -> String? {
224224
if let match = regexp.firstMatch( in: target as String, options: options, range: targetRange ) {
225-
return substring( match.rangeAt( groupno ) )
225+
return substring( match.range( at: groupno ) )
226226
}
227227
return nil
228228
}
@@ -254,7 +254,7 @@ open class MutableRegexp: Regexp {
254254
override open subscript ( groupno: Int ) -> String? {
255255
get {
256256
if let match = regexp.firstMatch( in: target as String, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange ) {
257-
return substring( match.rangeAt( groupno ) )
257+
return substring( match.range( at: groupno ) )
258258
}
259259
return nil
260260
}
@@ -264,7 +264,7 @@ open class MutableRegexp: Regexp {
264264
for match in Array(matchResults().reversed()) {
265265
let replacement = regexp.replacementString( for: match,
266266
in: target as String, offset: 0, template: newValue )
267-
mutableTarget.replaceCharacters( in: match.rangeAt(groupno), with: replacement )
267+
mutableTarget.replaceCharacters( in: match.range(at: groupno), with: replacement )
268268
}
269269
} else {
270270
SRLog( "Group modify on non-mutable" )

SwiftRuby.xcodeproj/project.pbxproj

+10-6
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@
263263
TargetAttributes = {
264264
BB3F78901BACAB9B00435430 = {
265265
CreatedOnToolsVersion = 7.0;
266-
LastSwiftMigration = 0820;
266+
LastSwiftMigration = 1010;
267267
};
268268
BB45AF591BB0CC8A00428322 = {
269269
CreatedOnToolsVersion = 7.0;
270270
};
271271
BBB3FE8D1BBBFDA5009E718C = {
272272
CreatedOnToolsVersion = 7.0;
273-
LastSwiftMigration = 0820;
273+
LastSwiftMigration = 1010;
274274
};
275275
};
276276
};
@@ -484,7 +484,8 @@
484484
STRIP_INSTALLED_PRODUCT = NO;
485485
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator";
486486
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
487-
SWIFT_VERSION = 3.0;
487+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
488+
SWIFT_VERSION = 4.2;
488489
};
489490
name = Debug;
490491
};
@@ -503,7 +504,8 @@
503504
PRODUCT_NAME = "$(TARGET_NAME)";
504505
STRIP_INSTALLED_PRODUCT = NO;
505506
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator";
506-
SWIFT_VERSION = 3.0;
507+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
508+
SWIFT_VERSION = 4.2;
507509
};
508510
name = Release;
509511
};
@@ -532,7 +534,8 @@
532534
INFOPLIST_FILE = SwiftRubyTests/Info.plist;
533535
PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRubyTests;
534536
PRODUCT_NAME = "$(TARGET_NAME)";
535-
SWIFT_VERSION = 3.0;
537+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
538+
SWIFT_VERSION = 4.2;
536539
};
537540
name = Debug;
538541
};
@@ -543,7 +546,8 @@
543546
INFOPLIST_FILE = SwiftRubyTests/Info.plist;
544547
PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRubyTests;
545548
PRODUCT_NAME = "$(TARGET_NAME)";
546-
SWIFT_VERSION = 3.0;
549+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
550+
SWIFT_VERSION = 4.2;
547551
};
548552
name = Release;
549553
};

SwiftRubyTests/SwiftRubyTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by John Holdsworth on 30/09/2015.
66
// Copyright © 2015 John Holdsworth. All rights reserved.
77
//
8-
// $Id: //depot/SwiftRuby/SwiftRubyTests/SwiftRubyTests.swift#14 $
8+
// $Id: //depot/SwiftRuby/SwiftRubyTests/SwiftRubyTests.swift#15 $
99
//
1010
// Repo: https://github.com/RubyNative/SwiftRuby
1111
//
@@ -85,7 +85,7 @@ class RubyNativeTests: XCTestCase {
8585
}
8686

8787
let files = ["diff1.txt", "same1.txt", "same2.txt"]
88-
XCTAssertEqual( Dir.glob( "*.txt", testdir )!.sorted(), files, "glob directory" )
88+
XCTAssertEqual( Dir.glob( testdir+"/*.txt" )!.sorted(), files.map { testdir+"/"+$0 }, "glob directory" )
8989
XCTAssertEqual( Dir.open( "." )!.to_a.sorted(), [".", ".."]+files, "read directory" )
9090
XCTAssertEqual( Kernel.open( "| ls \(testdir)" )!.to_a, files, "read popen" )
9191

0 commit comments

Comments
 (0)