├── .gitignore ├── Array.swift ├── Data.swift ├── Dir.swift ├── File.swift ├── FileUtils.swift ├── Float.swift ├── IO.swift ├── Info.plist ├── Integer.swift ├── Kernel.swift ├── LICENSE ├── MISSING.md ├── Object.swift ├── README.md ├── Rational.swift ├── Regexp.swift ├── Stat.swift ├── String.swift ├── StringIO.swift ├── SwiftRuby.podspec ├── SwiftRuby.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── SwiftRuby └── SwiftRuby.h ├── SwiftRubyTests ├── Info.plist └── SwiftRubyTests.swift ├── Time.swift ├── Utilities.h ├── Utilities.m └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | */build/* 2 | *Library/* 3 | *xcuserdata* 4 | -------------------------------------------------------------------------------- /Array.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Array.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Array.swift#6 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Array.html 13 | // 14 | 15 | public protocol array_like { 16 | 17 | var to_a: [String] { get } 18 | 19 | } 20 | 21 | extension Array: array_like { 22 | 23 | public var to_a: [String] { 24 | return map { String(describing: $0) } 25 | } 26 | 27 | // public func join(sep: String = " ") -> String { 28 | // return joinWithSeparator(sep) 29 | // } 30 | 31 | } 32 | 33 | extension Collection { 34 | 35 | public func each(_ block: (Iterator.Element) -> ()) { 36 | forEach(block) 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Data.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Data.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Data.swift#11 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Data.html 13 | // 14 | 15 | import Darwin 16 | 17 | public protocol data_like { 18 | 19 | var to_d: Data { get } 20 | 21 | } 22 | 23 | public func ==(lhs: Data, rhs: Data) -> Bool { 24 | return lhs.length == rhs.length && memcmp(lhs.bytes, rhs.bytes, lhs.length) == 0 25 | } 26 | 27 | open class Data: RubyObject, string_like, array_like, char_like, data_like { 28 | 29 | open var bytes: UnsafeMutablePointer 30 | 31 | open var length = 0 { 32 | didSet { 33 | if length > capacity { 34 | SRFatal("Data length \(length) > capacity \(capacity)", file: #file, line: #line) 35 | } 36 | bytes[length] = 0 37 | } 38 | } 39 | 40 | open var capacity = 0 { 41 | didSet { 42 | bytes = realloc(bytes, capacity+1)!.assumingMemoryBound(to: Int8.self) 43 | } 44 | } 45 | 46 | public init(bytes: UnsafeMutablePointer, length: Int = 0) { 47 | self.bytes = bytes 48 | self.length = length 49 | super.init() 50 | } 51 | 52 | public convenience init(capacity: Int? = 0) { 53 | let capacity = capacity ?? 10 * 1024 54 | self.init(bytes: malloc(capacity+1)!.assumingMemoryBound(to: Int8.self)) 55 | self.capacity = capacity 56 | } 57 | 58 | public convenience init(array: [CChar]) { 59 | let alen = array.count 60 | self.init(capacity: alen) 61 | memcpy(bytes, array, alen) 62 | length = alen-1 63 | } 64 | 65 | open func append(_ extra: data_like) -> Int { 66 | let extra = extra.to_d 67 | let required = length + extra.length 68 | if required + 1 > capacity { 69 | capacity += max(required - capacity, 10_000) 70 | } 71 | memcpy(bytes+length, extra.bytes, extra.length) 72 | length += extra.length 73 | return extra.length 74 | } 75 | 76 | open var to_a: [String] { 77 | return [to_s] 78 | } 79 | 80 | open var to_c: [CChar] { 81 | var data = [CChar](repeating: 0, count: length+1) /// 82 | memcpy(&data, bytes, data.count) 83 | return data 84 | } 85 | 86 | open var to_d: Data { 87 | return self 88 | } 89 | 90 | open var to_s: String { 91 | if let string = String(cString: bytes, encoding: STRING_ENCODING) { 92 | return string 93 | } 94 | 95 | SRLog("Data.to_s: Could not decode string from input") 96 | return U(String(cString: bytes, encoding: FALLBACK_INPUT_ENCODING)) 97 | } 98 | 99 | deinit { 100 | if capacity != 0 { 101 | free(bytes) 102 | } 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /Dir.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Dir.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 28/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Dir.swift#15 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Dir.html 13 | // 14 | 15 | import Darwin 16 | 17 | open class Dir: RubyObject, array_like { 18 | 19 | let dirpath: String 20 | var unixDIR: UnsafeMutablePointer? 21 | 22 | // Dir[ string [, string ...] ] → array 23 | 24 | init?(dirname: string_like, file: StaticString = #file, line: UInt = #line) { 25 | dirpath = dirname.to_s 26 | unixDIR = opendir(dirpath) 27 | super.init() 28 | if unixDIR == nil { 29 | SRError("opendir '\(dirpath.to_s)' failed", file: file, line: line) 30 | return nil 31 | } 32 | } 33 | 34 | // MARK: Class Methods 35 | 36 | open class func new(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? { 37 | return Dir(dirname: string, file: file, line: line) 38 | } 39 | 40 | open class func open(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Dir? { 41 | return new(string, file: file, line: line) 42 | } 43 | 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) 46 | } 47 | 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) 50 | } 51 | 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) 54 | } 55 | 56 | open class func entries(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> [String] { 57 | var out = [String]() 58 | foreach(dirname) { 59 | (name) in 60 | out.append(name) 61 | } 62 | return out 63 | } 64 | 65 | open class func exist(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 66 | return File.exist(dirname, file: file, line: line) 67 | } 68 | 69 | open class func exists(_ dirname: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 70 | return exist(dirname, file: file, line: line) 71 | } 72 | 73 | open class func foreach(_ dirname: string_like, _ block: (String) -> ()) { 74 | if let dir = Dir(dirname: dirname, file: #file, line: #line) { 75 | dir.each { 76 | (name) in 77 | block(name) 78 | } 79 | } 80 | } 81 | 82 | 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) { 85 | return nil 86 | } 87 | return String(validatingUTF8: cwd) 88 | } 89 | 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.. String? { 102 | var user = user?.to_s 103 | var buff = [Int8](repeating: 0, count: Int(PATH_MAX)) 104 | var ret: UnsafeMutablePointer? 105 | var info = passwd() 106 | 107 | if user == nil || user == "" { 108 | if !unixOK("Dir.getpwuid", getpwuid_r(geteuid(), &info, &buff, buff.count, &ret), file: file, line: line) { 109 | return nil 110 | } 111 | user = String(validatingUTF8: info.pw_name) 112 | } 113 | 114 | if !unixOK("Dir.getpwnam \(user!.to_s)", getpwnam_r(user!, &info, &buff, buff.count, &ret), file: file, line: line) { 115 | return nil 116 | } 117 | 118 | return String(validatingUTF8: info.pw_dir) 119 | } 120 | 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) 123 | } 124 | 125 | open class var pwd: String? { 126 | return getwd 127 | } 128 | 129 | open class func rmdir(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 130 | return delete(string, file: file, line: line) 131 | } 132 | 133 | open class func unlink(_ string: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 134 | return delete(string, file: file, line: line) 135 | } 136 | 137 | // MARK: Instance methods 138 | 139 | @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) 142 | unixDIR = nil 143 | return ok 144 | } 145 | 146 | @discardableResult 147 | open func each(_ block: (String) -> ()) -> Dir { 148 | while let name = read() { 149 | block(name) 150 | } 151 | return self 152 | } 153 | 154 | open var fileno: Int { 155 | return Int(dirfd(unixDIR)) 156 | } 157 | 158 | open var inspect: String { 159 | return dirpath 160 | } 161 | 162 | open var path: String { 163 | return dirpath 164 | } 165 | 166 | open var pos: Int { 167 | return Int(telldir(unixDIR)) 168 | } 169 | 170 | open func read() -> String? { 171 | let ent = readdir(unixDIR) 172 | if ent != nil { 173 | return withUnsafeMutablePointer (to: &ent!.pointee.d_name) { 174 | String(validatingUTF8: UnsafeMutableRawPointer($0).assumingMemoryBound(to: CChar.self)) 175 | } 176 | } 177 | return nil 178 | } 179 | 180 | open func rewind() { 181 | Darwin.rewinddir(unixDIR) 182 | } 183 | 184 | open func seek(_ pos: Int) { 185 | seekdir(unixDIR, pos) 186 | } 187 | 188 | open var tell: Int { 189 | return pos 190 | } 191 | 192 | open var to_a: [String] { 193 | var out = [String]() 194 | each { 195 | (entry) in 196 | out .append(entry) 197 | } 198 | return out 199 | } 200 | 201 | open var to_path: String { 202 | return dirpath 203 | } 204 | 205 | deinit { 206 | if unixDIR != nil { 207 | close() 208 | } 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /File.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/File.swift#13 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/File.html 13 | // 14 | 15 | import Foundation 16 | 17 | public let ALT_SEPARATOR = "/" 18 | public let PATH_SEPARATOR = ":" 19 | public let SEPARATOR = "/" 20 | public let Separator = "/" 21 | 22 | @discardableResult 23 | public func unixOK(_ what: string_like, _ returnValue: Int32, file: StaticString?, line: UInt = 0) -> Bool { 24 | if returnValue != 0 { 25 | if file != nil { 26 | SRError("\(what.to_s) failed, returning \(returnValue)", file: file!, line: line) 27 | } 28 | return false 29 | } 30 | return true 31 | } 32 | 33 | open class File : IO { 34 | 35 | let filepath: String 36 | 37 | var umask: Int { 38 | get { 39 | let omask = Darwin.umask(0) 40 | _ = Darwin.umask(omask) 41 | return Int(omask) 42 | } 43 | set { 44 | _ = Darwin.umask(mode_t(newValue)) 45 | } 46 | } 47 | 48 | public init?(filepath: string_like, mode: string_like = "r", file: StaticString, line: UInt) { 49 | self.filepath = filepath.to_s 50 | super.init(what: "fopen '\(filepath.to_s)', mode '\(mode.to_s)'", unixFILE: fopen(filepath.to_s, mode.to_s), file: file, line: line) 51 | if ifValid() == nil { 52 | return nil 53 | } 54 | } 55 | 56 | // MARK: Class Methods 57 | 58 | open class func new(_ file_name: string_like, _ mode: string_like = "r", _ perm: Int? = nil, file: StaticString = #file, line: UInt = #line) -> File? { 59 | let newFile = File(filepath: file_name, mode: mode, file: file, line: line) 60 | if perm != nil { 61 | _ = newFile?.chmod(perm!, file: file, line: line) 62 | } 63 | return newFile 64 | } 65 | 66 | open class func open(_ file_name: string_like, _ mode: string_like = "r", _ perm: Int? = nil, file: StaticString = #file, line: UInt = #line) -> File? { 67 | return new(file_name, mode, perm, file: file, line: line) 68 | } 69 | 70 | open class func absolute_path(_ file_name: string_like, _ dir_string: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 71 | var baseURL: URL? 72 | 73 | if dir_string != nil { 74 | baseURL = URL(fileURLWithPath: dir_string!.to_s) 75 | } 76 | 77 | let fileURL = URL(fileURLWithPath: file_name.to_s) 78 | #if swift(>=2.3) 79 | return URL(string: fileURL.absoluteString, relativeTo: baseURL)?.absoluteURL.path //// 80 | #else 81 | return NSURL(string: fileURL.absoluteString, relativeToURL: baseURL)?.absoluteURL.path //// 82 | #endif 83 | } 84 | 85 | open class func basename(_ file_name: string_like, _ suffix: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 86 | var file_name = file_name.to_s 87 | if suffix != nil { 88 | if suffix!.to_s == ".*" { 89 | file_name = extremoved(file_name)! 90 | } 91 | else { 92 | SRNotImplemented("File.basename with suffix ofer than '.*'", file: file, line: line) 93 | } 94 | } 95 | return URL(fileURLWithPath: file_name).lastPathComponent //// 96 | } 97 | 98 | open class func birthtime(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Time? { 99 | return Stat(file_name, file: file, line: line)?.ctime 100 | } 101 | 102 | open class func blockdev(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 103 | return Stat(file_name, file: file, line: line)?.blockdev 104 | } 105 | 106 | open class func chardev(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 107 | return Stat(file_name, file: file, line: line)?.chardev 108 | } 109 | 110 | @discardableResult 111 | open class func chmod(_ mode_int: Int, _ file_names: array_like, file: StaticString = #file, line: UInt = #line) -> Bool { 112 | var ok = true 113 | for file_name in file_names.to_a { 114 | ok = ok && unixOK("File.chmod '\(file_name)'", Darwin.chmod(file_name, mode_t(mode_int)), file: file, line: line) 115 | } 116 | return ok 117 | } 118 | 119 | open class func chown(_ owner_s: string_like?, _ group_s: string_like?, _ file_names: array_like, file: StaticString = #file, line: UInt = #line) -> Bool { 120 | 121 | var owner_int = owner_s != nil ? Int(owner_s!.to_s) : nil 122 | if owner_int == nil && owner_s != nil { 123 | owner_int = user_uid(owner_s!, file: file, line: line) 124 | if owner_int == nil { 125 | return false 126 | } 127 | } 128 | 129 | var group_int = group_s != nil ? Int(group_s!.to_s) : nil 130 | if group_int == nil && group_s != nil { 131 | group_int = group_gid(group_s!, file: file, line: line) 132 | if group_int == nil { 133 | return false 134 | } 135 | } 136 | 137 | var ok = true 138 | for file_name in file_names.to_a { 139 | ok = ok && unixOK("File.chown '\(file_name)'", Darwin.chown(file_name, uid_t(owner_int ?? -1), gid_t(group_int ?? -1)), file: file, line: line) 140 | } 141 | return ok 142 | } 143 | 144 | open class func user_uid(_ user_s: string_like, file: StaticString = #file, line: UInt = #line) -> Int? { 145 | var buff = [Int8](repeating: 0, count: Int(PATH_MAX)) 146 | var ret: UnsafeMutablePointer? 147 | var info = passwd() 148 | 149 | if !unixOK("File.getpwnam \(user_s.to_s)", getpwnam_r(user_s.to_s, &info, &buff, buff.count, &ret), file: file, line: line) { 150 | return nil 151 | } 152 | 153 | return Int(info.pw_uid) 154 | } 155 | 156 | open class func group_gid(_ group_s: string_like, file: StaticString = #file, line: UInt = #line) -> Int? { 157 | var buff = [Int8](repeating: 0, count: Int(PATH_MAX)) 158 | var ret: UnsafeMutablePointer? 159 | var info = group() 160 | 161 | if !unixOK("File.getgrnam \(group_s.to_s)", getgrnam_r(group_s.to_s, &info, &buff, buff.count, &ret), file: file, line: line) { 162 | return nil 163 | } 164 | 165 | return Int(info.gr_gid) 166 | } 167 | 168 | open class func ctime(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Time? { 169 | return birthtime(file_name) 170 | } 171 | 172 | open class func delete(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 173 | return unixOK("File.delete '\(file_name.to_s)'", Darwin.unlink(file_name.to_s), file: file, line: line) 174 | } 175 | 176 | open class func directory(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 177 | return Stat(file_name, file: file, line: line)?.directory 178 | } 179 | 180 | open class func dirname(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> String? { 181 | return NSURL(fileURLWithPath: file_name.to_s).deletingLastPathComponent?.path 182 | } 183 | 184 | open class func executable(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 185 | return Stat(file_name, file: file, line: line)?.executable 186 | } 187 | 188 | open class func executable_real(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 189 | return Stat(file_name, file: file, line: line)?.executable_real 190 | } 191 | 192 | open class func exist(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 193 | return Stat(file_name, file: nil) != nil 194 | } 195 | 196 | open class func exists(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 197 | return exist(file_name) 198 | } 199 | 200 | open class func expand_path(_ file_name: string_like, _ dir_string: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 201 | return NSURL(fileURLWithPath: file_name.to_s).standardized?.path 202 | } 203 | 204 | open class func extname(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> String? { 205 | return URL(fileURLWithPath: file_name.to_s).pathExtension 206 | } 207 | 208 | open class func file(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 209 | return Stat(file_name, file: file, line: line)?.file == true 210 | } 211 | 212 | // public class func fnmatch(pattern: string_like, _ path: string_like, _ flags: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 213 | // RKNotImplemented("File.fnmatch") 214 | // return false 215 | // } 216 | 217 | open class func ftype(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> String? { 218 | return Stat(file_name, file: file, line: line)?.ftype 219 | } 220 | 221 | open class func grpowned(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 222 | return Stat(file_name, file: file, line: line)?.grpowned 223 | } 224 | 225 | open class func identical(_ file_1: string_like, _ file_2: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 226 | if let stat1 = Stat(file_1, file: file, line: line), let stat2 = Stat(file_2, file: file, line: line) { 227 | return stat1 == stat2 228 | } 229 | return false 230 | } 231 | 232 | open class func join(_ strings: [String], file: StaticString = #file, line: UInt = #line) -> String { 233 | return strings.joined(separator: SEPARATOR) 234 | } 235 | 236 | open class func lchmod(_ mode_int: Int, _ file_names: array_like, file: StaticString = #file, line: UInt = #line) -> Bool { 237 | var ok = true 238 | for file_name in file_names.to_a { 239 | ok = ok && unixOK("File.lchmod '\(file_name)'", Darwin.lchmod(file_name, mode_t(mode_int)), file: file, line: line) 240 | } 241 | return ok 242 | } 243 | 244 | open class func lchown(_ owner_s: string_like?, _ group_s: string_like?, _ file_names: array_like, file: StaticString = #file, line: UInt = #line) -> Bool { 245 | 246 | var owner_int = owner_s != nil ? Int(owner_s!.to_s) : nil 247 | if owner_int == nil && owner_s != nil { 248 | owner_int = user_uid(owner_s!, file: file, line: line) 249 | if owner_int == nil { 250 | return false 251 | } 252 | } 253 | 254 | var group_int = group_s != nil ? Int(group_s!.to_s) : nil 255 | if group_int == nil && group_s != nil { 256 | group_int = group_gid(group_s!, file: file, line: line) 257 | if group_int == nil { 258 | return false 259 | } 260 | } 261 | 262 | var ok = true 263 | for file_name in file_names.to_a { 264 | ok = ok && unixOK("File.lchown '\(file_name.to_s)'", Darwin.lchown(file_name.to_s, uid_t(owner_int ?? -1), gid_t(group_int ?? -1)), file: file, line: line) 265 | } 266 | return ok 267 | } 268 | 269 | open class func link(_ old_name: string_like, _ new_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 270 | return unixOK("File.link '\(old_name.to_s)' '\(new_name.to_s)'", Darwin.link(old_name.to_s, new_name.to_s), file: file, line: line) 271 | } 272 | 273 | open class func lstat(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Stat? { 274 | return Stat(file_name, statLink: true, file: file, line: line) 275 | } 276 | 277 | open class func mtime(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Time? { 278 | return Stat(file_name, file: file, line: line)?.mtime 279 | } 280 | 281 | open class func owned(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 282 | return Stat(file_name, file: file, line: line)?.owned 283 | } 284 | 285 | open class func path(_ path: string_like, file: StaticString = #file, line: UInt = #line) -> String { 286 | return path.to_s //// 287 | } 288 | 289 | open class func pipe(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 290 | return Stat(file_name, file: file, line: line)?.pipe 291 | } 292 | 293 | open class func readable(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 294 | return Stat(file_name, file: file, line: line)?.readable 295 | } 296 | 297 | open class func readable_real(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 298 | return Stat(file_name, file: file, line: line)?.readable_real 299 | } 300 | 301 | open class func readlink(_ link_name: string_like, file: StaticString = #file, line: UInt = #line) -> String? { 302 | var path = [Int8](repeating: 0, count: Int(PATH_MAX+1)) 303 | let length = Darwin.readlink(link_name.to_s, &path, path.count) /// readlinkat for relatives? 304 | if unixOK("File.readlink '\(link_name.to_s)'", length == -1 ? 1 : 0, file: file, line: line) { 305 | path[length] = 0 306 | return String(validatingUTF8: path) 307 | } 308 | return nil 309 | } 310 | 311 | open class func realdirpath(_ file_name: string_like, _ dir_string: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 312 | if dir_string != nil { 313 | SRNotImplemented("File.realdirpath with dir_string argument", file: file, line: line) 314 | } 315 | return NSURL(fileURLWithPath: file_name.to_s).resolvingSymlinksInPath?.path //// 316 | } 317 | 318 | open class func realpath(_ file_name: string_like, _ dir_string: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 319 | if dir_string != nil { 320 | SRNotImplemented("File.realpath with dir_string argument", file: file, line: line) 321 | } 322 | return NSURL(fileURLWithPath: file_name.to_s).resolvingSymlinksInPath?.path //// 323 | } 324 | 325 | open class func extremoved(_ file_name: string_like, _ suffix: string_like? = nil, file: StaticString = #file, line: UInt = #line) -> String? { 326 | return NSURL(fileURLWithPath: file_name.to_s).deletingPathExtension?.path 327 | } 328 | 329 | open class func rename(_ old_name: string_like, _ new_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 330 | return unixOK("File.rename '\(old_name.to_s)' '\(new_name.to_s)'", Darwin.rename(old_name.to_s, new_name.to_s), file: file, line: line) 331 | } 332 | 333 | open class func setgid(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 334 | return Stat(file_name, file: file, line: line)?.setgid 335 | } 336 | 337 | open class func setuid(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 338 | return Stat(file_name, file: file, line: line)?.setuid 339 | } 340 | 341 | open class func size(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Int? { 342 | return Stat(file_name, file: file, line: line)?.size 343 | } 344 | 345 | open class func socket(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 346 | return Stat(file_name, file: file, line: line)?.socket 347 | } 348 | 349 | open class func split(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> [String?] { 350 | return [dirname(file_name), basename(file_name)] 351 | } 352 | 353 | open class func stat(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Stat? { 354 | return Stat(file_name, file: file, line: line) 355 | } 356 | 357 | open class func sticky(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 358 | return Stat(file_name, file: file, line: line)?.sticky 359 | } 360 | 361 | open class func symlink(_ old_name: string_like, _ new_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 362 | return unixOK("File.symlink '\(old_name.to_s)' '\(new_name.to_s)'", Darwin.symlink(old_name.to_s, new_name.to_s), file: file, line: line) 363 | } 364 | 365 | open class func truncate(_ file_name: string_like, _ integer: Int, file: StaticString = #file, line: UInt = #line) -> Bool { 366 | return unixOK("File.truncate '\(file_name.to_s)' \(integer)", Darwin.truncate(file_name.to_s, off_t(integer)), file: file, line: line) 367 | } 368 | 369 | open class func unlink(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 370 | return delete(file_name, file: file, line: line) 371 | } 372 | 373 | open class func unlink_f(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool { 374 | return File.exists(file_name) || File.lstat(file_name) != nil ? unlink(file_name, file: file, line: line) : true 375 | } 376 | 377 | open class func utime(_ file_name: string_like, _ actime: int_like, _ modtime: int_like, file: StaticString = #file, line: UInt = #line) -> Bool { 378 | var times = utimbuf() 379 | times.actime = time_t(actime.to_i) 380 | times.modtime = time_t(modtime.to_i) 381 | return unixOK("File.utime '\(file_name.to_s)'", Darwin.utime(file_name.to_s, ×), file: file, line: line) 382 | } 383 | 384 | open class func writable(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 385 | return Stat(file_name, file: file, line: line)?.writable 386 | } 387 | 388 | open class func writable_real(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 389 | return Stat(file_name, file: file, line: line)?.writable_real 390 | } 391 | 392 | open class func write(_ file_name: string_like, string: data_like, file: StaticString = #file, line: UInt = #line) -> fixnum? { 393 | return File(filepath: file_name, mode: "w", file: file, line: line)?.write(string) 394 | } 395 | 396 | open class func zero(_ file_name: string_like, file: StaticString = #file, line: UInt = #line) -> Bool? { 397 | return Stat(file_name, file: file, line: line)?.zero 398 | } 399 | 400 | // MARK: Instance Methods 401 | 402 | open var atime: Time? { 403 | return stat?.atime 404 | } 405 | 406 | open var birthtime: Time? { 407 | return stat?.birthtime 408 | } 409 | 410 | open func chmod(_ mode_int: Int, file: StaticString = #file, line: UInt = #line) -> Bool { 411 | return unixOK("File.chmod \(mode_int) '\(filepath)'", Darwin.chmod(filepath, mode_t(mode_int)), file: file, line: line) 412 | } 413 | 414 | open func chown(_ owner_s: string_like?, _ group_s: string_like?, file: StaticString = #file, line: UInt = #line) -> Bool { 415 | 416 | var owner_int = owner_s != nil ? Int(owner_s!.to_s) : nil 417 | if owner_int == nil && owner_s != nil { 418 | owner_int = File.user_uid(owner_s!, file: file, line: line) 419 | if owner_int == nil { 420 | return false 421 | } 422 | } 423 | 424 | var group_int = group_s != nil ? Int(group_s!.to_s) : nil 425 | if group_int == nil && group_s != nil { 426 | group_int = File.group_gid(group_s!, file: file, line: line) 427 | if group_int == nil { 428 | return false 429 | } 430 | } 431 | 432 | return unixOK("File.chown '\(filepath)' \(String(describing: owner_int)) \(String(describing: group_int))", Darwin.chown(filepath, 433 | uid_t(owner_int ?? -1), gid_t(group_int ?? -1)), file: file, line: line) 434 | } 435 | 436 | open var ctime: Time? { 437 | return birthtime 438 | } 439 | 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 | // } 443 | 444 | open var lstat: Stat? { 445 | return Stat(filepath, statLink: true, file: #file, line: #line) 446 | } 447 | 448 | open var mtime: Time? { 449 | return stat?.mtime 450 | } 451 | 452 | open var path: String { 453 | return filepath 454 | } 455 | 456 | open var to_path: String { 457 | return filepath 458 | } 459 | 460 | open func truncate(_ integer: Int, file: StaticString = #file, line: UInt = #line) -> Bool { 461 | return unixOK("File.truncate '\(filepath)' \(integer)", Darwin.truncate(filepath, off_t(integer)), file: file, line: line) 462 | } 463 | 464 | } 465 | -------------------------------------------------------------------------------- /FileUtils.swift: -------------------------------------------------------------------------------- 1 | // 2 | // public class func swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 30/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/FileUtils.swift#18 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/stdlib-2.2.3/libdoc/fileutils/rdoc/FileUtils.html 13 | // 14 | 15 | import Darwin 16 | // FIXME: comparison operators with optionals were removed from the Swift Standard Libary. 17 | // Consider refactoring the code to use the non-optional operators. 18 | fileprivate func < (lhs: T?, rhs: T?) -> Bool { 19 | switch (lhs, rhs) { 20 | case let (l?, r?): 21 | return l < r 22 | case (nil, _?): 23 | return true 24 | default: 25 | return false 26 | } 27 | } 28 | 29 | // FIXME: comparison operators with optionals were removed from the Swift Standard Libary. 30 | // Consider refactoring the code to use the non-optional operators. 31 | fileprivate func > (lhs: T?, rhs: T?) -> Bool { 32 | switch (lhs, rhs) { 33 | case let (l?, r?): 34 | return l > r 35 | default: 36 | return rhs < lhs 37 | } 38 | } 39 | 40 | 41 | public var STATUS = 0 42 | 43 | public func systemOK(_ command: string_like, file: StaticString? = #file, line: UInt = #line) -> Bool { 44 | #if os(iOS) 45 | SRNotImplemented("system() depricated since iOS 8", file: file!, line: line) 46 | #else 47 | STATUS = Int(_system(command.to_s)) 48 | if STATUS != 0 { 49 | if file != nil { 50 | SRLog("system call '\(command.to_s)' failed", file: file!, line: line) 51 | } 52 | return false 53 | } 54 | STATUS >>= 8 55 | #endif 56 | return true 57 | } 58 | 59 | open class FileUtils { 60 | 61 | open var pwd: String? { 62 | return Dir.getwd 63 | } 64 | 65 | fileprivate class func expand(_ list: array_like) -> String { 66 | return list.to_a.map { "\""+$0.replacingOccurrences(of: "\"", with: "\\\"")+"\"" } 67 | .joined(separator: " ") 68 | } 69 | 70 | open class func cd(_ dir: string_like, options: [String]?, file: StaticString = #file, line: UInt = #line) -> Bool { 71 | return Dir.chdir(dir, file: file, line: line) 72 | } 73 | 74 | // public class func cd(dir: string_like, options: [String]?, file: StaticString = #file, line: UInt = #line) -> Bool { {|dir| .... } 75 | 76 | open class func chmod(_ mode: string_like, _ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 77 | return systemOK("chmod \(mode.to_s) \(expand(list))", file: file, line: line) 78 | } 79 | 80 | open class func chmod_R(_ mode: string_like, _ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 81 | return systemOK("chmod -R \(mode.to_s) \(expand(list))", file: file, line: line) 82 | } 83 | 84 | open class func chown(_ user: string_like, _ group: string_like?, _ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 85 | let whoto = user.to_s + (group != nil ? ":"+group!.to_s : "") 86 | return systemOK("chown \(whoto) \(expand(list))", file: file, line: line) 87 | } 88 | 89 | open class func chown_R(_ user: string_like, _ group: string_like?, _ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 90 | let whoto = user.to_s + (group != nil ? ":"+group!.to_s : "") 91 | return systemOK("chown -R \(whoto) \(expand(list))", file: file, line: line) 92 | } 93 | 94 | open class func cmp(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 95 | return compare_file(src, dest, options, file: file, line: line) 96 | } 97 | 98 | open class func compare_file(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 99 | return systemOK("diff -q \"\(src.to_s)\" \"\(dest.to_s)\" >/dev/null", file: file, line: line) 100 | } 101 | 102 | open class func compare_stream(_ a: IO, _ b: IO, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 103 | if let a = a.read(), let b = b.read() { 104 | return a.length == b.length && memcmp(a.bytes, b.bytes, a.length) == 0 105 | } 106 | return false 107 | } 108 | 109 | open class func copy(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 110 | return cp(src, dest, options, file: file, line: line) 111 | } 112 | 113 | open class func copy_entry(_ src: string_like, _ dest: string_like, _ preserve: Bool = false, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 114 | return rsync([src.to_s], dest, preserve ? "-rlpogt" : "-rlp", options, file: file, line: line) 115 | } 116 | 117 | open class func copy_file(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 118 | return cp(src, dest, options, file: file, line: line) 119 | } 120 | 121 | open class func copy_stream(_ src: IO, _ dest: IO, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 122 | if let data = src.read() { 123 | dest.write(data) 124 | return true 125 | } 126 | return false 127 | } 128 | 129 | open class func cat(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 130 | return systemOK("cat \(expand(list)) >\"\(dir.to_s)\"", file: file, line: line) 131 | } 132 | 133 | open class func cp(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 134 | return systemOK("cp \(expand(list)) \"\(dir.to_s)\"", file: file, line: line) 135 | } 136 | 137 | open class func cp_r(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 138 | return systemOK("cp -r \(expand(list)) \"\(dir.to_s)\"", file: file, line: line) 139 | } 140 | 141 | open class func cp_rf(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 142 | return systemOK("cp -rf \(expand(list)) \"\(dir.to_s)\"", file: file, line: line) 143 | } 144 | 145 | open class func identical(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 146 | return compare_file(src, dest, options, file: file, line: line) 147 | } 148 | 149 | open class func install(_ src: string_like, _ dest: string_like, _ mode: string_like?, options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 150 | let mode = mode != nil ? " -m " + mode!.to_s : "" 151 | return systemOK("install\(mode) \"\(src.to_s)\" \"\(dest.to_s)\"", file: file, line: line) 152 | } 153 | 154 | open class func link(_ old: string_like, _ new: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 155 | return ln(old, new, options, file: file, line: line) 156 | } 157 | 158 | open class func ln(_ list: array_like, _ destdir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 159 | return systemOK("ln \(expand(list)) \"\(destdir.to_s)\"", file: file, line: line) 160 | } 161 | 162 | open class func ln_s(_ list: array_like, _ destdir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 163 | return systemOK("ln -s \(expand(list)) \"\(destdir.to_s)\"", file: file, line: line) 164 | } 165 | 166 | open class func ln_sf(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 167 | return systemOK("ln -sf \"\(src.to_s)\" \"\(dest.to_s)\"", file: file, line: line) 168 | } 169 | 170 | open class func makedirs(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 171 | return mkdir_p(list, options, file: file, line: line) 172 | } 173 | 174 | open class func mkdir(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 175 | return systemOK("mkdir \(expand(list))", file: file, line: line) 176 | } 177 | 178 | open class func mkdir_p(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 179 | return systemOK("mkdir -p \(expand(list))", file: file, line: line) 180 | } 181 | 182 | open class func mkpath(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 183 | return mkdir_p(list, options, file: file, line: line) 184 | } 185 | 186 | open class func mv(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 187 | return systemOK("mv \(expand(list)) \"\(dir.to_s)\"", file: file, line: line) 188 | } 189 | 190 | open class func mv_f(_ list: array_like, _ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 191 | return systemOK("mv -f \(expand(list)) \"\(dir.to_s)\"", file: file, line: line) 192 | } 193 | 194 | open class func remove_dir(_ dir: string_like, _ force: Bool = false, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 195 | return force ? rm_rf(dir, options, file: file, line: line) : rm_r([dir.to_s], options, file: file, line: line) 196 | } 197 | 198 | open class func remove_entry(_ dir: string_like, _ force: Bool = false, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 199 | return force ? rm_rf(dir, options, file: file, line: line) : rm_r([dir.to_s], options, file: file, line: line) 200 | } 201 | 202 | open class func remove_entry_secure(_ dir: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 203 | return systemOK("rm -rfP \"\(dir.to_s)\"", file: file, line: line) 204 | } 205 | 206 | open class func remove_file(_ path: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 207 | return rm([path.to_s], options, file: file, line: line) 208 | } 209 | 210 | open class func rm(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 211 | return systemOK("rm \(expand(list))", file: file, line: line) 212 | } 213 | 214 | open class func rm_f(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 215 | return systemOK("rm -f \(expand(list))", file: file, line: line) 216 | } 217 | 218 | open class func rm_r(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 219 | return systemOK("rm -r \(expand(list))", file: file, line: line) 220 | } 221 | 222 | open class func rm_rf(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 223 | return systemOK("rm -rf \(expand(list.to_a))", file: file, line: line) 224 | } 225 | 226 | open class func rmdir(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 227 | return systemOK("rmdir \(expand(list))", file: file, line: line) 228 | } 229 | 230 | open class func rmtree(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 231 | return rm_rf(list, options, file: file, line: line) 232 | } 233 | 234 | open class func safe_unlink(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 235 | return rm_f(list, options, file: file, line: line) 236 | } 237 | 238 | open class func symlink(_ src: string_like, _ dest: string_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 239 | return ln_s(src, dest, options, file: file, line: line) 240 | } 241 | 242 | open class func rsync(_ list: array_like, _ dest: string_like, _ args: string_like = "-a", _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 243 | return systemOK("rsync \(args) \(expand(list)) \"\(dest.to_s)\"", file: file, line: line) 244 | } 245 | 246 | open class func touch(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 247 | return systemOK("touch \(expand(list))", file: file, line: line) 248 | } 249 | 250 | open class func touch_f(_ list: array_like, _ options: [String]? = nil, file: StaticString = #file, line: UInt = #line) -> Bool { 251 | return systemOK("touch -f \(expand(list))", file: file, line: line) 252 | } 253 | 254 | open class func uptodate(_ new: string_like, old_list: array_like, file: StaticString = #file, line: UInt = #line) -> Bool { 255 | let new_time = Stat(new, file: nil)?.mtime.to_f ?? 0 256 | for old in old_list.to_a { 257 | if Stat(old, file: file, line: line)?.mtime.to_f > new_time { 258 | return false 259 | } 260 | } 261 | return true 262 | } 263 | 264 | } 265 | -------------------------------------------------------------------------------- /Float.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Float.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Float.swift#6 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Float.html 13 | // 14 | 15 | public protocol float_like { 16 | 17 | var to_f: Double { get } 18 | 19 | } 20 | 21 | extension Double: float_like { 22 | 23 | public var to_s: String { 24 | return String(self) 25 | } 26 | 27 | public var to_f: Double { 28 | return self 29 | } 30 | 31 | public var to_i: Int { 32 | return Int(self) 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /IO.swift: -------------------------------------------------------------------------------- 1 | // 2 | // IO.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/IO.swift#15 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/IO.html 13 | // 14 | 15 | import Darwin 16 | 17 | public let EWOULDBLOCKWaitReadable = EWOULDBLOCK 18 | public let EWOULDBLOCKWaitWritable = EWOULDBLOCK 19 | 20 | private let selectBitsPerFlag: Int32 = 32 21 | private let selectShift = 5 22 | private let selectBitMask = (1<) { 25 | memset(flags, 0, MemoryLayout.size) 26 | } 27 | 28 | public func FD_CLR(_ fd: Int, _ flags: UnsafeMutablePointer) { 29 | let set = flags + Int(fd>>selectShift) 30 | set.pointee &= ~Int32(1<<(fd&selectBitMask)) 31 | } 32 | 33 | public func FD_SET(_ fd: Int, _ flags: UnsafeMutablePointer) { 34 | let set = flags + Int(fd>>selectShift) 35 | set.pointee |= Int32(1<<(fd&selectBitMask)) 36 | } 37 | 38 | public func FD_ISSET(_ fd: Int, _ flags: UnsafeMutablePointer) -> Bool { 39 | let set = flags + Int(fd>>selectShift) 40 | return (set.pointee & Int32(1<<(fd&selectBitMask))) != 0 41 | } 42 | 43 | public func ==(lhs: IO, rhs: IO) -> Bool { 44 | if let lhData = lhs.read(), let rhData = rhs.read() { 45 | return lhData == rhData 46 | } 47 | return false 48 | } 49 | 50 | open class IO: RubyObject, string_like, data_like { 51 | 52 | fileprivate var _unixFILE: UnsafeMutablePointer? 53 | open var unixFILE: UnsafeMutablePointer? { 54 | get { 55 | if _unixFILE == nil { 56 | SRLog("Get of nil IO.unixFILE") 57 | } 58 | return _unixFILE 59 | } 60 | } 61 | 62 | open var autoclose = true 63 | public let binmode = true 64 | open var lineno = 0 65 | 66 | open var sync = true { 67 | didSet { 68 | flush() 69 | setvbuf(unixFILE, nil, sync ? _IONBF : _IOFBF, 0) 70 | } 71 | } 72 | 73 | open var nonblock = false { 74 | didSet { 75 | var flags = fcntl(Int(F_GETFL), 0) 76 | if nonblock { 77 | flags |= Int(O_NONBLOCK) 78 | } 79 | else { 80 | flags &= ~Int(O_NONBLOCK) 81 | } 82 | fcntl(Int(F_SETFL), flags) 83 | } 84 | } 85 | 86 | open var close_on_exec = false { 87 | didSet { 88 | if close_on_exec { 89 | fcntl(Int(F_SETFD), Int(FD_CLOEXEC)) 90 | } 91 | } 92 | } 93 | 94 | open var pos: Int { 95 | get { 96 | return Int(ftell(unixFILE)) 97 | } 98 | set { 99 | seek(newValue) 100 | } 101 | } 102 | 103 | public init(what: String?, unixFILE: UnsafeMutablePointer?, file: StaticString = #file, line: UInt = #line) { 104 | super.init() 105 | if unixFILE == nil && what != nil { 106 | SRError("\(what!) failed", file: file, line: line) 107 | } 108 | self._unixFILE = unixFILE 109 | } 110 | 111 | open func ifValid() -> IO? { 112 | return _unixFILE != nil ? self : nil 113 | } 114 | 115 | // MARK: Class methods 116 | 117 | open class func binread(_ name: string_like, _ length: Int? = nil, _ offset: Int? = nil) -> Data? { 118 | return self.read(name, length, offset) 119 | } 120 | 121 | open class func binwrite(_ name: string_like, _ string: data_like, _ offset: Int? = nil) -> fixnum? { 122 | return self.write(name, string, offset) 123 | } 124 | 125 | open class func copy_stream(_ src: IO, _ dst: IO, _ copy_length: Int? = nil, _ src_offset: Int? = nil) -> Int { 126 | if src_offset != nil { 127 | src.seek(src_offset!, Int(SEEK_SET)) 128 | } 129 | var copied = 0 130 | let data = Data(capacity: 1024*1024) 131 | while let data = src.read(data.capacity, data) { 132 | copied += data.length 133 | dst.write(data) 134 | data.length = 0 135 | } 136 | return copied 137 | } 138 | 139 | open class func for_fd(_ fd: Int, _ mode: string_like, opt: Array? = nil, file: StaticString = #file, line: UInt = #line) -> IO? { 140 | return IO(what: "fdopen \(fd)", unixFILE: fdopen(Int32(fd), mode.to_s), file: file, line: line).ifValid() 141 | } 142 | 143 | open class func foreach(_ name: string_like, _ sep: string_like = LINE_SEPARATOR, 144 | _ limit: Int? = nil, _ block: (_ line: String) -> ()) { 145 | if let nameInt = Int(name.to_s), 146 | let ioFile = File.open(nameInt, "r") { 147 | ioFile.each_line(sep, limit, block) 148 | } 149 | } 150 | 151 | open class func foreach(_ name: string_like, _ limit: Int? = nil, _ block: (_ line: String) -> ()) { 152 | foreach(name, LINE_SEPARATOR, limit, block) 153 | } 154 | 155 | open class func new(_ fd: Int, _ mode: string_like = "r", file: StaticString = #file, line: UInt = #line) -> IO? { 156 | return for_fd(fd, mode, file: file, line: line) 157 | } 158 | 159 | open class func open(_ fd: Int, _ mode: string_like = "r", file: StaticString = #file, line: UInt = #line) -> IO? { 160 | return for_fd(fd, mode, file: file, line: line) 161 | } 162 | 163 | open class func pipe(_ file: StaticString = #file, line: UInt = #line) -> (reader: IO?, writer: IO?) { 164 | var fds = [Int32](repeating: 0, count: 0) 165 | _ = Darwin.pipe(&fds) 166 | return (IO.new(Int(fds[0]), "r", file: file, line: line), IO.new(Int(fds[1]), "w", file: file, line: line)) 167 | } 168 | 169 | open class func popen(_ command: string_like, _ mode: string_like = "r", file: StaticString = #file, line: UInt = #line) -> IO? { 170 | return IO(what: "IO.popen '\(command)'", unixFILE: _popen(command.to_s, mode.to_s), file: file, line: line).ifValid() 171 | } 172 | 173 | open class func read(_ name: string_like, _ length: Int? = nil, _ offset: Int? = nil, file: StaticString = #file, line: UInt = #line) -> Data? { 174 | if let nameInt = Int(name.to_s), 175 | let ioFile = File.open(nameInt, "r") { 176 | if offset != nil { 177 | ioFile.seek(offset!, Int(SEEK_SET), file: file, line: line) 178 | } 179 | return ioFile.read(length) 180 | } 181 | return nil 182 | } 183 | 184 | open class func readlines(_ name: string_like, _ sep: string_like = LINE_SEPARATOR, _ limit: Int? = nil, file: StaticString = #file, line: UInt = #line) -> [String]? { 185 | if let nameInt = Int(name.to_s), 186 | let ioFile = File.open(nameInt, "r", file: file, line: line) { 187 | var out = [String]() 188 | ioFile.each_line(sep, limit, { 189 | (line) in 190 | out.append(line) 191 | }) 192 | return out 193 | } 194 | return nil 195 | } 196 | 197 | open class func readlines(_ name: string_like, _ limit: Int? = nil, file: StaticString = #file, line: UInt = #line) -> [String]? { 198 | return readlines(name, LINE_SEPARATOR, limit, file: file, line: line) 199 | } 200 | 201 | open class func select(_ read_array: [IO]?, _ write_array: [IO]? = nil, _ error_array: [IO]? = nil, 202 | timeout: Double? = nil, file: StaticString = #file, line: UInt = #line) 203 | -> (readable: [IO], writable: [IO], errored: [IO])? { 204 | 205 | let read_flags = malloc(MemoryLayout.size)!.assumingMemoryBound(to: Int32.self) 206 | let write_flags = malloc(MemoryLayout.size)!.assumingMemoryBound(to: Int32.self) 207 | let error_flags = malloc(MemoryLayout.size)!.assumingMemoryBound(to: Int32.self) 208 | var max_fd = -1 209 | 210 | for (array, flags) in [(read_array, read_flags), (write_array, write_flags), (error_array, error_flags)] { 211 | FD_ZERO(flags) 212 | if array != nil { 213 | for io in array! { 214 | FD_SET(io.fileno, flags) 215 | if max_fd < io.fileno { 216 | max_fd = io.fileno 217 | } 218 | } 219 | } 220 | } 221 | 222 | var time: Time? 223 | if timeout != nil { 224 | time = Time(time_f: timeout!) 225 | } 226 | 227 | func mutablePointer(_ val: inout T) -> UnsafeMutablePointer { 228 | return withUnsafeMutablePointer (to: &val) { 229 | UnsafeMutablePointer($0) 230 | } 231 | } 232 | 233 | 234 | let selected = read_flags.withMemoryRebound(to: fd_set.self, capacity: 1) { read_set in 235 | return write_flags.withMemoryRebound(to: fd_set.self, capacity: 1) { write_set in 236 | return error_flags.withMemoryRebound(to: fd_set.self, capacity: 1) { error_set in 237 | Darwin.select(Int32(max_fd), read_set, write_set, error_set, 238 | time != nil ? mutablePointer(&time!.value) : nil) 239 | } 240 | } 241 | } 242 | 243 | var out: (readable: [IO], writable: [IO], errored: [IO])? 244 | 245 | if selected < 0 { 246 | unixOK("IO.select", selected, file: file, line: line) 247 | } 248 | else if selected > 0 { 249 | var readable = [IO](), writable = [IO](), errored = [IO]() 250 | 251 | for (array, flags, out) in [ 252 | (read_array, read_flags, mutablePointer(&readable)), 253 | (write_array, write_flags, mutablePointer(&writable)), 254 | (error_array, error_flags, mutablePointer(&errored))] { 255 | if array != nil { 256 | for io in array! { 257 | if FD_ISSET(io.fileno, flags) { 258 | out.pointee.append(io) 259 | } 260 | } 261 | } 262 | } 263 | 264 | out = (readable, writable, errored) 265 | } 266 | 267 | free(read_flags) 268 | free(write_flags) 269 | free(error_flags) 270 | return out 271 | } 272 | 273 | open class func sysopen(_ path: string_like, _ mode: Int = Int(O_RDONLY), _ perm: Int = 0o644) -> fixnum { 274 | return Int(Darwin.open(path.to_s, CInt(mode), mode_t(perm))) 275 | } 276 | 277 | @discardableResult 278 | open class func write(_ name: string_like, _ string: data_like, _ offset: Int? = 0, _ open_args: String? = nil, file: StaticString = #file, line: UInt = #line) -> fixnum? { 279 | if let ioFile = File.open(name, open_args ?? "w", file: file, line: line) { 280 | if offset != nil { 281 | ioFile.seek(offset!, Int(SEEK_SET), file: file, line: line) 282 | } 283 | return ioFile.write(string) 284 | } 285 | return nil 286 | } 287 | 288 | // MARK: Instance methods 289 | 290 | // public func advise(advice: Int, _ offset: Int = 0, _ len: Int = 0) { 291 | // RKNotImplemented("IO.advise") 292 | // } 293 | 294 | open func bytes(_ block: (CChar) -> ()) -> IO { 295 | return each_byte(block) 296 | } 297 | 298 | open func chars(_ block: (CChar16) -> ()) -> IO { 299 | return each_char(block) 300 | } 301 | 302 | @discardableResult 303 | open func close(_ file: StaticString = #file, line: UInt = #line) -> Int { 304 | if _unixFILE != nil { 305 | var status = _pclose(unixFILE!) 306 | if status == -1 { 307 | status = fclose(unixFILE) 308 | } 309 | _unixFILE = nil 310 | return Int(status) 311 | } 312 | return -1 313 | } 314 | 315 | // public func close_read() { 316 | // RKNotImplemented("IO.close_read") 317 | // } 318 | // 319 | // public func close_write() { 320 | // RKNotImplemented("IO.close_write") 321 | // } 322 | 323 | open var closed: Bool { 324 | return unixFILE == nil 325 | } 326 | 327 | open func each_byte(_ block: (CChar) -> ()) -> IO { 328 | while true { 329 | let byte = CChar(fgetc(unixFILE)) 330 | if eof { 331 | break 332 | } 333 | block(byte) 334 | } 335 | return self 336 | } 337 | 338 | @discardableResult 339 | open func each_char(_ block: (CChar16) -> ()) -> IO { 340 | read()?.to_s.utf16.each(block) 341 | return self 342 | } 343 | 344 | open func each(_ limit: Int? = nil, _ block: (_ line: String) -> ()) -> IO { 345 | return each_line(LINE_SEPARATOR, limit, block) 346 | } 347 | 348 | open func each(_ sep: string_like = LINE_SEPARATOR, _ limit: Int? = nil, _ block: (_ line: String) -> ()) -> IO { 349 | return each_line(sep, limit, block) 350 | } 351 | 352 | // public func each_line(limit: Int? = nil, _ block: (line: String) -> ()) -> IO { 353 | // return each_line(dollarSlash, limit, block) 354 | // } 355 | // 356 | @discardableResult 357 | open func each_line(_ sep: string_like = LINE_SEPARATOR, _ limit: Int? = nil, _ block: (_ line: String) -> ()) -> IO { 358 | var count = 0 359 | while let line = readline(sep) { 360 | block(line) 361 | if limit != nil { 362 | count += 1 363 | if count >= limit! { 364 | break 365 | } 366 | } 367 | } 368 | return self 369 | } 370 | 371 | open var eof: Bool { 372 | return feof(unixFILE) != 0 373 | } 374 | 375 | @discardableResult 376 | open func fcntl(_ arg: Int, _ arg2: Int = 0) -> Int { 377 | return Int(fcntl3(Int32(fileno), Int32(arg), Int32(arg2))) 378 | } 379 | 380 | // public func fdatasync() { 381 | // RKNotImplemented("IO.fdatasync") 382 | // } 383 | 384 | open var fileno: Int { 385 | return Int(Darwin.fileno(unixFILE)) 386 | } 387 | 388 | @discardableResult 389 | open func flush(_ file: StaticString = #file, line: UInt = #line) -> Bool { 390 | return unixOK("IO.fflush", fflush(unixFILE), file: file, line: line) 391 | } 392 | 393 | open func fsync(_ file: StaticString = #file, line: UInt = #line) -> Bool { 394 | return flush() && unixOK("IO.fsync", Darwin.fsync(Int32(fileno)), file: file, line: line) 395 | } 396 | 397 | open var getbyte: fixnum? { 398 | let byte = CChar(fgetc(unixFILE)) 399 | if feof(unixFILE) != 0 { 400 | return nil 401 | } 402 | return Int(byte) 403 | } 404 | 405 | open var getc: String? { 406 | let byte = CChar(fgetc(unixFILE)) 407 | if feof(unixFILE) != 0 { 408 | return nil 409 | } 410 | return String(byte) 411 | } 412 | 413 | static public let newline = Int8("\n".ord) 414 | static public let retchar = Int8("\r".ord) 415 | 416 | func gets(_ sep: string_like = LINE_SEPARATOR) -> String? { 417 | let data = Data(capacity: 1_000_000) //// TODO: should loop 418 | if fgets(data.bytes, Int32(data.capacity), unixFILE) == nil { 419 | return nil 420 | } 421 | lineno += 1 422 | data.length = Int(strlen(data.bytes)) 423 | if data.length > 0 && data.bytes[data.length-1] == IO.newline { 424 | data.length -= 1 425 | data.bytes[data.length] = 0 426 | } 427 | if data.length > 0 && data.bytes[data.length-1] == IO.retchar { 428 | data.length -= 1 429 | data.bytes[data.length] = 0 430 | } 431 | return data.to_s 432 | } 433 | 434 | open var inspect: String { 435 | return self.to_s 436 | } 437 | 438 | open var internal_encoding: Int { 439 | return Int(STRING_ENCODING.rawValue) 440 | } 441 | 442 | // public func ioctl(integer_cmd: Int, arg: Int) -> Int { 443 | // RKNotImplemented("IO.ioctl") 444 | // return 1 445 | // } 446 | 447 | open var isatty: Bool { 448 | return Darwin.isatty(Int32(fileno)) != 0 449 | } 450 | 451 | open func lines(_ block: (_ line: String) -> ()) -> IO { 452 | return each_line(LINE_SEPARATOR, nil, block) 453 | } 454 | 455 | // public var pid: Int { 456 | // /// not possible without re-implementing popen() 457 | // RKNotImplemented("IO.pid") 458 | // return -1 459 | // } 460 | 461 | @discardableResult 462 | open func print(_ string: string_like) -> Int { 463 | return Int(fputs(string.to_s, unixFILE)) 464 | } 465 | 466 | open func print(_ strings: array_like) { 467 | for string in strings.to_a { 468 | print(string) 469 | } 470 | } 471 | 472 | func printf(_ string: string_like) { 473 | print(string) 474 | } 475 | 476 | func putc(_ obj: Int) -> Int { 477 | return Int(fputc(Int32(obj), unixFILE)) 478 | } 479 | 480 | open func puts(_ string: string_like) -> Int { 481 | return print(string) 482 | } 483 | 484 | open func puts(_ strings: array_like) { 485 | return print(strings) 486 | } 487 | 488 | open func read(_ length: Int? = nil, _ outbuf: Data? = nil) -> Data? { 489 | let data = outbuf ?? Data(capacity: (length ?? stat?.size ?? 1_000_000)+1) //// 490 | while true { 491 | let toread = data.capacity-data.length 492 | let wasread = fread(data.bytes+data.length, 1, toread, unixFILE) 493 | data.length += wasread 494 | if wasread != toread { 495 | break 496 | } 497 | data.capacity *= 2 498 | } 499 | return ferror(unixFILE) == 0 || feof(unixFILE) != 0 ? data : nil 500 | } 501 | 502 | open func read_nonblock(_ length: Int? = nil, _ outbuf: Data? = nil) -> Data? { 503 | nonblock = true 504 | return read(length, outbuf) 505 | } 506 | 507 | open var readbyte: fixnum? { 508 | return getbyte 509 | } 510 | 511 | open var readchar: String? { 512 | return getc 513 | } 514 | 515 | open func readline(_ sep: string_like = LINE_SEPARATOR) -> String? { 516 | return gets(sep) 517 | } 518 | 519 | open func readlines(/*sep: string_like = dollarSlash, _*/ _ limit: Int? = nil) -> [String] { 520 | var out = [String]() 521 | each_line(LINE_SEPARATOR, limit, { 522 | (line) in 523 | out.append(line) 524 | }) 525 | return out 526 | } 527 | 528 | // public func readlines(limit: Int? = nil) -> [String] { 529 | // return readlines(dollarSlash, limit) 530 | // } 531 | 532 | // public func readpartial(maxlen: Int, _ outbuf: Data? = nil) -> Data? { 533 | // RKNotImplemented("IO.readpartial") 534 | // return nil 535 | // } 536 | 537 | open func reopen(_ other_IO: IO) -> IO { 538 | _unixFILE = other_IO.unixFILE //// 539 | return self 540 | } 541 | 542 | open func reopen(_ path: string_like, _ mode_str: string_like = "r", file: StaticString = #file, line: UInt = #line) -> IO? { 543 | return unixOK("IO.reopen \(path.to_s)", freopen(path.to_s, mode_str.to_s, self.unixFILE) == nil ? 1 : 0, 544 | file: file, line: line) ? self : nil 545 | } 546 | 547 | open func rewind(_ file: StaticString = #file, line: UInt = #line) -> IO { 548 | Darwin.rewind(unixFILE) 549 | return self 550 | } 551 | 552 | @discardableResult 553 | open func seek(_ amount: Int, _ whence: Int = Int(SEEK_SET), file: StaticString = #file, line: UInt = #line) -> Bool { 554 | return unixOK("IO.seek", fseek(unixFILE, amount, Int32(whence)), file: file, line: line) 555 | } 556 | 557 | // public func set_encoding(ext_enc: Int) -> IO? { 558 | // RKNotImplemented("IO.set_encoding") 559 | // return nil 560 | // } 561 | 562 | open var stat: Stat? { 563 | return Stat(fd: fileno, file: #file, line: #line) 564 | } 565 | 566 | open func sysread(_ maxlen: Int? = nil, _ outbuf: Data? = nil) -> Data? { 567 | let data = outbuf ?? Data(capacity: maxlen ?? stat?.size ?? 1_000_000) //// 568 | data.length = Darwin.read(Int32(fileno), data.bytes, data.capacity) 569 | return data.length > 0 ? data : nil 570 | } 571 | 572 | open func sysseek(_ offset: Int, _ whence: Int = Int(SEEK_SET)) -> Int { 573 | return Int(lseek(Int32(fileno), off_t(offset), Int32(whence))) 574 | } 575 | 576 | open func syswrite(_ string: data_like) -> Int { 577 | return Int(Darwin.write(Int32(fileno), string.to_d.bytes, string.to_d.length)) 578 | } 579 | 580 | open var tell: Int { 581 | return pos 582 | } 583 | 584 | open var to_a: [String] { 585 | return readlines() 586 | } 587 | 588 | open var to_i: Int { 589 | return fileno 590 | } 591 | 592 | open var to_d: Data { 593 | if let data = read() { 594 | return data 595 | } 596 | SRLog("IO.to_d, no data") 597 | return "IO.to_d, no data".to_d 598 | } 599 | 600 | open var to_s: String { 601 | if let data = read() { 602 | return data.to_s 603 | } 604 | SRLog("IO.to_s, no data") 605 | return "IO.to_s, no data" 606 | } 607 | 608 | open var tty: Bool { 609 | return isatty 610 | } 611 | 612 | open func ungetbyte(_ string: string_like) { 613 | ungetc(Int32(string.to_s.characterAtIndex(0)), unixFILE) //// 614 | } 615 | 616 | open func ungetbyte(_ byte: Int) { 617 | ungetc(Int32(byte), unixFILE) 618 | } 619 | 620 | @discardableResult 621 | open func write(_ string: data_like) -> fixnum { 622 | let data = string.to_d 623 | return fwrite(data.bytes, 1, data.length, unixFILE); 624 | } 625 | 626 | open func write_nonblock(_ string: data_like, options: Array? = nil) -> Int { 627 | nonblock = true 628 | return write(string) 629 | } 630 | 631 | deinit { 632 | if autoclose { 633 | close() 634 | } 635 | } 636 | 637 | } 638 | -------------------------------------------------------------------------------- /Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSHumanReadableCopyright 22 | Copyright © 2020 John Holdsworth. All rights reserved. 23 | 24 | 25 | -------------------------------------------------------------------------------- /Integer.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Integer.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Integer.swift#8 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Integer.html 13 | // 14 | 15 | public typealias fixnum = Int 16 | 17 | public protocol int_like { 18 | 19 | var to_i: Int { get } 20 | 21 | } 22 | 23 | extension Int: int_like { 24 | 25 | public var to_i: Int { 26 | return self 27 | } 28 | 29 | public var to_f: Double { 30 | return Double(self) 31 | } 32 | 33 | public var to_s: String { 34 | return String(self) 35 | } 36 | 37 | public var to_b: String { 38 | return String(self, radix: 2) 39 | } 40 | 41 | public var to_o: String { 42 | return String(self, radix: 8 ) 43 | } 44 | 45 | public var to_h: String { 46 | return String(self, radix: 16, uppercase: false) 47 | } 48 | 49 | public var chr: String { 50 | var chars = UInt16(self) 51 | return String(utf16CodeUnits: &chars, count: 1) 52 | } 53 | 54 | public var denominator: Int { 55 | return 1 56 | } 57 | 58 | public func downto(_ limit: Int, block: (_ i: Int) -> ()) -> Int { 59 | var i = self 60 | while i >= limit { 61 | block(i) 62 | i -= 1 63 | } 64 | return self 65 | } 66 | 67 | public var even: Bool { 68 | return self & 0x1 == 0 69 | } 70 | 71 | // public func gcd(int2: Int) -> Int { 72 | // RKNotImplemented("Integer.gcd") 73 | // return -1 74 | // } 75 | // 76 | // public func gcdlcm(int2: Int) -> Int { 77 | // RKNotImplemented("Integer.gcd") 78 | // return -1 79 | // } 80 | // 81 | // public func lcm(int2: Int) -> Int { 82 | // RKNotImplemented("Integer.gcd") 83 | // return -1 84 | // } 85 | 86 | public var next: Int { 87 | return self+1 88 | } 89 | 90 | public var numerator: Int { 91 | return self+1 92 | } 93 | 94 | public var odd: Bool { 95 | return !even 96 | } 97 | 98 | public var ord: Int { 99 | return self 100 | } 101 | 102 | public var pred: Int { 103 | return self-1 104 | } 105 | 106 | public func times(_ block: (_ i: Int) -> ()) -> Int { 107 | var i = 1 108 | while i <= self { 109 | block(i) 110 | i += 1 111 | } 112 | return self 113 | } 114 | 115 | public func upto(_ limit: Int, block: (_ i: Int) -> ()) -> Int { 116 | var i = self 117 | while i <= limit { 118 | block(i) 119 | i += 1 120 | } 121 | return self 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /Kernel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Kernel.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 27/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Kernel.swift#17 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Kernel.html 13 | // 14 | 15 | import Foundation 16 | 17 | public func U(_ toUnwrap: T?, name: String? = nil, file: StaticString = #file, line: UInt = #line) -> T { 18 | if toUnwrap == nil { 19 | let msg = name != nil ? "Forced unwrap of \(name ?? "No name") fail" : "Forced unwrap fail" 20 | _throw(NSException(name: NSExceptionName(rawValue: msg), reason: "\(file), \(line)", userInfo: ["msg": msg, "file": String(describing: file), "line": "\(line)"] )) 21 | } 22 | return toUnwrap! 23 | } 24 | 25 | public enum WarningDisposition { 26 | case ignore, warn, `throw`, fatal 27 | } 28 | 29 | public var WARNING_DISPOSITION: WarningDisposition = .warn 30 | public var LAST_WARNING: String? 31 | 32 | public func SRLog(_ msg: String, file: StaticString = #file, line: UInt = #line) { 33 | LAST_WARNING = msg+" at \(file)#\(line)" 34 | if WARNING_DISPOSITION == .throw { 35 | _throw(NSException(name: NSExceptionName(rawValue: msg), reason: LAST_WARNING, userInfo: ["msg": msg, "file": String(describing: file), "line": "\(line)"] )) 36 | } 37 | if WARNING_DISPOSITION != .ignore { 38 | STDERR.print("SwiftRuby: \(U(LAST_WARNING))\n") 39 | } 40 | if WARNING_DISPOSITION == .fatal { 41 | fatalError() 42 | } 43 | } 44 | 45 | public func SRError(_ msg: String, file: StaticString, line: UInt) { 46 | let error = String(validatingUTF8: strerror(errno )) ?? "Undecodable strerror" 47 | SRLog(msg+": \(error)", file: file, line: line) 48 | } 49 | 50 | public func SRFatal(_ msg: String, file: StaticString, line: UInt) { 51 | SRLog(msg, file: file, line: line) 52 | fatalError() 53 | } 54 | 55 | public func SRNotImplemented(_ what: String, file: StaticString, line: UInt) { 56 | SRFatal("\(what) not implemented", file: file, line: line) 57 | } 58 | 59 | @_silgen_name("execArgv") 60 | func execArgv(_ executable: NSString, _ argv: NSArray) 61 | 62 | @_silgen_name("spawnArgv") 63 | func spawnArgv(_ executable: NSString, _ argv: NSArray) -> pid_t 64 | 65 | open class Kernel: RubyObject { 66 | 67 | open class func open(_ path: string_like, _ mode: string_like = "r", _ perm: Int = 0o644, file: StaticString = #file, line: UInt = #line) -> IO? { 68 | let path = path.to_s 69 | let index1 = path.index(path.startIndex, offsetBy: 1) 70 | 71 | if path[path.startIndex ..< index1] == "|" { 72 | return IO.popen(String(path[index1 ..< path.endIndex]), 73 | mode, file: file, line: line) 74 | } 75 | 76 | return File.open(path, mode, perm, file: file, line: line) 77 | } 78 | 79 | open class func exec(_ command: string_like) { 80 | exec("/bin/bash", ["-c", command.to_s]) 81 | } 82 | 83 | open class func exec(_ executable: string_like, _ arguments: array_like) { 84 | exec([executable.to_s, executable.to_s], arguments.to_a) 85 | } 86 | 87 | open class func exec(_ executable: array_like, _ arguments: array_like) { 88 | execArgv(executable.to_a[0].to_s as NSString, [executable.to_a[1]]+arguments.to_a as NSArray) 89 | } 90 | 91 | open class func spawn(_ command: string_like) -> Int { 92 | return Int(spawnArgv("/bin/bash", ["/bin/bash", "-c", command.to_s])) 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License: 2 | 3 | Copyright (c) 2015 John Holdsworth 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /MISSING.md: -------------------------------------------------------------------------------- 1 | I'd like to see the following modules/classes in RubyNative: 2 | 3 | Kernel 4 | Thread 5 | *Socket 6 | -------------------------------------------------------------------------------- /Object.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Object.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Object.swift#19 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Object.html 13 | // 14 | 15 | import Foundation 16 | 17 | public let ARGV = CommandLine.arguments 18 | 19 | public let STDIN = IO(what: "stdin", unixFILE: stdin) 20 | public let STDOUT = IO(what: "stdout", unixFILE: stdout) 21 | public let STDERR = IO(what: "stderr", unixFILE: stderr) 22 | 23 | public let ENV = ENVProxy() 24 | 25 | open class ENVProxy { 26 | 27 | open subscript(key: string_like) -> String? { 28 | get { 29 | let val = getenv(key.to_s) 30 | return val != nil ? String(validatingUTF8: val!) ?? "Value not UTF8" : nil 31 | } 32 | set { 33 | if newValue != nil { 34 | setenv(key.to_s, newValue!, 1) 35 | } 36 | else { 37 | unsetenv(key.to_s) 38 | } 39 | } 40 | } 41 | 42 | } 43 | 44 | open class RubyObject { 45 | 46 | open var hash: fixnum { 47 | return unsafeBitCast(self, to: Int.self) 48 | } 49 | 50 | open var instance_variables: [String] { 51 | return instanceVariablesForClass(type(of: self), NSMutableArray()).map {$0} 52 | } 53 | 54 | open var methods: [String] { 55 | return methodSymbolsForClass(type(of: self), NSMutableArray()).map { _stdlib_demangleName($0) } 56 | } 57 | 58 | } 59 | 60 | // not public in Swift3 61 | #if swift(>=3.0) 62 | @_silgen_name("swift_demangle") 63 | public 64 | func _stdlib_demangleImpl( 65 | _ mangledName: UnsafePointer?, 66 | mangledNameLength: UInt, 67 | outputBuffer: UnsafeMutablePointer?, 68 | outputBufferSize: UnsafeMutablePointer?, 69 | flags: UInt32 70 | ) -> UnsafeMutablePointer? 71 | 72 | 73 | public func _stdlib_demangleName(_ mangledName: String) -> String { 74 | return mangledName.utf8CString.withUnsafeBufferPointer { 75 | (mangledNameUTF8) in 76 | 77 | let demangledNamePtr = _stdlib_demangleImpl( 78 | mangledNameUTF8.baseAddress, 79 | mangledNameLength: UInt(mangledNameUTF8.count - 1), 80 | outputBuffer: nil, 81 | outputBufferSize: nil, 82 | flags: 0) 83 | 84 | if let demangledNamePtr = demangledNamePtr { 85 | let demangledName = String(cString: demangledNamePtr) 86 | free(demangledNamePtr) 87 | return demangledName 88 | } 89 | return mangledName 90 | } 91 | } 92 | #endif 93 | 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### SwiftRuby 2 | 3 | RubyNative is a port of the Ruby core api to Swift intended to be used with [Diamond](https://github.com/RubyNative/Diamond) 4 | script interpreter or any Swift Program that would like simpler access to file i/o than is offered 5 | by Foundation kit. It also extends Swift's String and Array classes and will provide a 6 | well integrated Regexp class to take the burden off the programmer dealing with 7 | uncompromising Swift Strings. 8 | 9 | Why port Ruby to Swift? Using a strictly typed and scoped language for scripting 10 | avoids may of the pitfalls of a dynamic language and allows for auto-completion 11 | when working in the editor. This makes your scripts run faster in terms of 12 | developer time and very much so at run time. The languages are also a good 13 | fit syntactically. 14 | 15 | Seems like a lot of low level UNIX code? We chose to stay close to the metal and the 16 | Ruby apis seem based on the POSIX interface. It is also not clear Foundation will 17 | be available when Swift is released for Linux. For now the project has minimal 18 | dependencies on Foundation classes NSURL and NSRegularExpression for things that it 19 | would not be worth replicating until the smoke clears for Swift on Linux. 20 | 21 | At this stage, Ruby's [Dir](http://ruby-doc.org/core-2.2.3/Dir.html), [IO](http://ruby-doc.org/core-2.2.3/IO.html), 22 | [File](http://ruby-doc.org/core-2.2.3/File.html), [StringIO](http://ruby-doc.org/stdlib-2.2.3/libdoc/stringio/rdoc/StringIO.html), 23 | [Time](http://ruby-doc.org/core-2.2.3/Time.html) and [Stat](http://ruby-doc.org/core-2.2.3/File/Stat.html) classes have been implemented. 24 | These classes follow the original documented functionality as closely as possible. 25 | Input arguments are generally protocols such as `string_like` or `data_like` 26 | to allow for automatic conversion between types. Return values are concrete 27 | types String Int, Float or Data or an instance. 28 | 29 | A flavour of the common Ruby idioms implemented in Swift thus far is given below: 30 | 31 | ![Icon](http://injectionforxcode.johnholdsworth.com/ruby.png) 32 | 33 | SwiftRuby has avoided using operator overloading as much as Ruby with one exception: 34 | subscripting when working with Strings. There are two general forms. Subscripting 35 | with integers to extract ranges from Strings and subscripting with Strings to 36 | provide a succinct syntax for Regular Expressions. 37 | 38 | ### Subscripting Strings 39 | 40 | At danger of unpicking the sterling work Swift has done to protect us all from 41 | String internals simple extensions have been defined to make scripting easier: 42 | 43 | ![Icon](http://injectionforxcode.johnholdsworth.com/strings.png) 44 | 45 | ### Subscripting Strings with a String as syntax for Regexp 46 | 47 | If you think about it, the logical index into a string is a Regexp. Ruby toys with this idea 48 | but SwiftRuby takes this much further. This is best explained by the following examples: 49 | 50 | ![Icon](http://injectionforxcode.johnholdsworth.com/regexps.png) 51 | 52 | ### License is MIT 53 | 54 | SwiftRuby is also available as a CocoaPod for use without using the [Diamond](https://github.com/RubyNative/Diamond) 55 | script interpreter and is available under an MIT License. If you have any comments or suggestions, the 56 | authors can be contacted on Twitter [@Injection4Xcode](https://twitter.com/#!/@Injection4Xcode). 57 | -------------------------------------------------------------------------------- /Rational.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Rational.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 27/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Rational.swift#4 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Rational.html 13 | // 14 | 15 | open class Rational: RubyObject { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Regexp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Regex.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Regexp.swift#12 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Regexp.html 13 | // 14 | 15 | import Foundation 16 | 17 | infix operator =~ : ComparisonPrecedence 18 | 19 | public func =~ (lhs: String, rhs: String) -> Regexp { 20 | return Regexp(target: lhs as NSString, pattern: rhs) 21 | } 22 | 23 | infix operator !~ : ComparisonPrecedence 24 | 25 | public func !~ (lhs: String, rhs: String) -> NotRegexp { 26 | return NotRegexp(target: lhs as NSString, pattern: rhs) 27 | } 28 | 29 | extension String { 30 | 31 | public subscript (pattern: String) -> Regexp { 32 | return Regexp(target: self as NSString, pattern: pattern) 33 | } 34 | 35 | public subscript (pattern: String, options: NSRegularExpression.Options) -> Regexp { 36 | return Regexp(target: self as NSString, pattern: pattern, options: options) 37 | } 38 | 39 | public subscript (pattern: String, optionString: String) -> Regexp { 40 | return Regexp(target: self as NSString, pattern: pattern, optionString: optionString) 41 | } 42 | 43 | public subscript (pattern: String, capture: Int) -> String? { 44 | return slice(pattern, capture) 45 | } 46 | 47 | public var mutableString: NSMutableString { 48 | return NSMutableString(string: self) 49 | } 50 | 51 | public func gsub(_ pattern: String, _ template: String) -> String { 52 | let out = self.mutableString 53 | _ = out[pattern] =~ template 54 | return out as String 55 | } 56 | 57 | public func index(_ pattern: String) -> Int? { 58 | let range = Regexp(target: self as NSString, pattern: pattern).range() 59 | return range.location != NSNotFound ? range.location : nil 60 | } 61 | 62 | public var lstrip: String { 63 | return self["^\\s+"][""] 64 | } 65 | 66 | public func match(_ pattern: String) -> [String?]? { 67 | return Regexp(target: self as NSString, pattern: pattern).groups() 68 | } 69 | 70 | public var rstrip: String { 71 | return self["\\s*+"][""] 72 | } 73 | 74 | public func slice(_ pattern: String, _ capture: Int = 0) -> String? { 75 | return Regexp(target: self as NSString, pattern: pattern)[capture] 76 | } 77 | 78 | public func scan(_ pattern: String) -> [[String?]] { 79 | return Regexp(target: self as NSString, pattern: pattern).allGroups() 80 | } 81 | 82 | public var strip: String { 83 | return self["^\\s+|\\s+$"][""] 84 | } 85 | 86 | public func sub(_ pattern: String, _ template: String) -> String { 87 | let out = self.mutableString 88 | _ = out[pattern] =~ [template] 89 | return out as String 90 | } 91 | 92 | } 93 | 94 | extension NSMutableString { 95 | 96 | public subscript (pattern: String) -> MutableRegexp { 97 | return MutableRegexp(target: self, pattern: pattern) 98 | } 99 | 100 | public subscript (pattern: String, options: NSRegularExpression.Options) -> MutableRegexp { 101 | return MutableRegexp(target: self, pattern: pattern, options: options) 102 | } 103 | 104 | public subscript (pattern: String, optionString: String) -> MutableRegexp { 105 | return MutableRegexp(target: self, pattern: pattern, optionString: optionString) 106 | } 107 | 108 | } 109 | 110 | open class Regexp: RubyObject { 111 | 112 | let target: NSString 113 | let regexp: NSRegularExpression 114 | var file: RegexpFile? 115 | 116 | public convenience init(target: NSString, pattern: String, optionString: String) { 117 | var options: UInt = 0 118 | for char in optionString { 119 | switch char { 120 | case "i": 121 | options |= NSRegularExpression.Options.caseInsensitive.rawValue 122 | case "x": 123 | options |= NSRegularExpression.Options.allowCommentsAndWhitespace.rawValue 124 | case "q": 125 | options |= NSRegularExpression.Options.ignoreMetacharacters.rawValue 126 | case "m": 127 | options |= NSRegularExpression.Options.anchorsMatchLines.rawValue 128 | case "s": 129 | options |= NSRegularExpression.Options.dotMatchesLineSeparators.rawValue 130 | case "l": 131 | options |= NSRegularExpression.Options.useUnixLineSeparators.rawValue 132 | case "u": 133 | options |= NSRegularExpression.Options.useUnicodeWordBoundaries.rawValue 134 | default: 135 | SRLog("Invalid Regexp option \(char)") 136 | break 137 | } 138 | } 139 | self.init(target: target, pattern: pattern, options: NSRegularExpression.Options(rawValue: options )) 140 | } 141 | 142 | public init(target: NSString, pattern: String, 143 | options: NSRegularExpression.Options = NSRegularExpression.Options(rawValue: 0 )) { 144 | self.target = target 145 | do { 146 | self.regexp = try NSRegularExpression(pattern: pattern, options: options) 147 | } 148 | catch let error as NSError { 149 | SRLog("Regexp pattern: '\(pattern)' compile error: \(error)") 150 | self.regexp = NSRegularExpression() 151 | } 152 | } 153 | 154 | final var targetRange: NSRange { 155 | return NSMakeRange(0, target.length) 156 | } 157 | 158 | final func substring(_ range: NSRange) -> String? { 159 | if (range.location != NSNotFound) { 160 | return target.substring(with: range) 161 | } else { 162 | return nil 163 | } 164 | } 165 | 166 | open func doesMatch(_ options: NSRegularExpression.MatchingOptions? = nil) -> Bool { 167 | return range(options).location != NSNotFound 168 | } 169 | 170 | open func range(_ options: NSRegularExpression.MatchingOptions? = nil) -> NSRange { 171 | return regexp.rangeOfFirstMatch(in: target as String, options: options ?? NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) 172 | } 173 | 174 | func matchResults(_ options: NSRegularExpression.MatchingOptions? = nil) -> [NSTextCheckingResult] { 175 | return regexp.matches(in: target as String, options: options ?? NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) 176 | } 177 | 178 | func replaceWith(_ template: String, options: NSRegularExpression.MatchingOptions? = nil) -> NSMutableString { 179 | let mutable = /*target as? NSMutableString ??*/ NSMutableString(string: target) 180 | regexp.replaceMatches(in: mutable, options: options ?? NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange, withTemplate: template) 181 | return mutable 182 | } 183 | 184 | func groupsForMatch(_ match: NSTextCheckingResult) -> [String?] { 185 | var groups = [String?]() 186 | for groupno in 0...regexp.numberOfCaptureGroups { 187 | groups.append(substring(match.range(at: groupno) )) 188 | } 189 | return groups 190 | } 191 | 192 | open func dictionary(_ options: NSRegularExpression.MatchingOptions? = nil) -> [String:String] { 193 | var out = [String:String]() 194 | for match in matchResults(options) { 195 | out[substring(match.range(at: 1))!] = 196 | substring(match.range(at: 2))! 197 | } 198 | return out 199 | } 200 | 201 | open func match(_ options: NSRegularExpression.MatchingOptions? = nil) -> String? { 202 | return substring(range(options )) 203 | } 204 | 205 | open func groups(_ options: NSRegularExpression.MatchingOptions? = nil) -> [String?]? { 206 | if let match = regexp.firstMatch(in: target as String, options: options ?? NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) { 207 | return groupsForMatch(match) 208 | } 209 | return nil 210 | } 211 | 212 | open func allGroups(_ options: NSRegularExpression.MatchingOptions? = nil) -> [[String?]] { 213 | return matchResults(options).map { self.groupsForMatch($0) } 214 | } 215 | 216 | open subscript (groupno: Int) -> String? { 217 | if let match = regexp.firstMatch(in: target as String, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) { 218 | return substring(match.range(at: groupno )) 219 | } 220 | return nil 221 | } 222 | 223 | open subscript (groupno: Int, options: NSRegularExpression.MatchingOptions) -> String? { 224 | if let match = regexp.firstMatch(in: target as String, options: options, range: targetRange) { 225 | return substring(match.range(at: groupno )) 226 | } 227 | return nil 228 | } 229 | 230 | open subscript(template: String) -> String { 231 | return replaceWith(template) as String 232 | } 233 | 234 | open subscript(template: String, options: NSRegularExpression.MatchingOptions) -> String { 235 | return replaceWith(template, options: options) as String 236 | } 237 | 238 | open var boolValue: Bool { 239 | return doesMatch() 240 | } 241 | 242 | } 243 | 244 | open class NotRegexp : Regexp { 245 | 246 | open override var boolValue: Bool { 247 | return !doesMatch() 248 | } 249 | 250 | } 251 | 252 | open class MutableRegexp: Regexp { 253 | 254 | override open subscript (groupno: Int) -> String? { 255 | get { 256 | if let match = regexp.firstMatch(in: target as String, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) { 257 | return substring(match.range(at: groupno )) 258 | } 259 | return nil 260 | } 261 | set(newValue) { 262 | if let newValue = newValue { 263 | if let mutableTarget = target as? NSMutableString { 264 | for match in Array(matchResults().reversed()) { 265 | let replacement = regexp.replacementString(for: match, 266 | in: target as String, offset: 0, template: newValue) 267 | mutableTarget.replaceCharacters(in: match.range(at: groupno), with: replacement) 268 | } 269 | } else { 270 | SRLog("Group modify on non-mutable") 271 | } 272 | } 273 | else { 274 | SRLog("nil replacement in group modify") 275 | } 276 | 277 | } 278 | } 279 | 280 | func substituteMatches(_ substitution: (NSTextCheckingResult, UnsafeMutablePointer) -> String) -> Bool { 281 | let out = NSMutableString() 282 | var pos = 0 283 | 284 | regexp.enumerateMatches(in: target as String, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: targetRange) { 285 | (match: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer) in 286 | 287 | let matchRange = match!.range 288 | out.append(self.substring(NSMakeRange(pos, matchRange.location-pos ))!) //// 289 | out.append(substitution(match!, stop )) 290 | pos = matchRange.location + matchRange.length 291 | } 292 | 293 | out.append(substring(NSMakeRange(pos, targetRange.length-pos ))!) //// 294 | 295 | if let mutableTarget = target as? NSMutableString { 296 | if out != target { 297 | mutableTarget.setString(out as String) 298 | return true 299 | } 300 | } else { 301 | SRLog("Modify substitute on non-mutable") 302 | } 303 | return false 304 | } 305 | 306 | } 307 | 308 | public func =~ (left: MutableRegexp, right: String) -> Bool { 309 | return left.substituteMatches({ 310 | (match: NSTextCheckingResult, stop: UnsafeMutablePointer) in 311 | return left.regexp.replacementString(for: match, 312 | in: left.target as String, offset: 0, template: right) 313 | }) 314 | } 315 | 316 | public func =~ (left: MutableRegexp, right: [String]) -> Bool { 317 | var matchNumber = 0 318 | return left.substituteMatches({ 319 | (match: NSTextCheckingResult, stop: UnsafeMutablePointer) in 320 | 321 | matchNumber += 1 322 | if matchNumber == right.count { 323 | stop.pointee = true 324 | } 325 | 326 | return left.regexp.replacementString(for: match, 327 | in: left.target as String, offset: 0, template: right[matchNumber-1]) 328 | }) 329 | } 330 | 331 | public func =~ (left: MutableRegexp, right: (String?) -> String) -> Bool { 332 | return left.substituteMatches({ 333 | (match: NSTextCheckingResult, stop: UnsafeMutablePointer) in 334 | return right(left.substring(match.range )) 335 | }) 336 | } 337 | 338 | public func =~ (left: MutableRegexp, right: ([String?]) -> String) -> Bool { 339 | return left.substituteMatches({ 340 | (match: NSTextCheckingResult, stop: UnsafeMutablePointer) in 341 | return right(left.groupsForMatch(match )) 342 | }) 343 | } 344 | 345 | open class RegexpFile { 346 | 347 | let filepath: String 348 | let contents: NSMutableString! //// 349 | let original: String! 350 | 351 | public init?(_ path: String, file: StaticString = #file, line: UInt = #line) { 352 | filepath = path 353 | contents = File.read(path)?.to_s.mutableString 354 | original = contents as String 355 | if contents == nil { 356 | SRError("RegexpFile could not read '\(path)'", file: file, line: line) 357 | return nil 358 | } 359 | } 360 | 361 | open subscript(pattern: String) -> MutableRegexp { 362 | let regexp = MutableRegexp(target: contents, pattern: pattern) 363 | regexp.file = self // retains until after substitution 364 | return regexp 365 | } 366 | 367 | open subscript (pattern: String, options: NSRegularExpression.Options) -> MutableRegexp { 368 | let regexp = MutableRegexp(target: contents, pattern: pattern, options: options) 369 | regexp.file = self // retains until after substitution 370 | return regexp 371 | } 372 | 373 | open subscript (pattern: String, options: String) -> MutableRegexp { 374 | let regexp = MutableRegexp(target: contents, pattern: pattern, optionString: options) 375 | regexp.file = self // retains until after substitution 376 | return regexp 377 | } 378 | 379 | deinit { 380 | if contents as String != original { 381 | File.write(filepath, contents as String) 382 | } 383 | } 384 | 385 | } 386 | -------------------------------------------------------------------------------- /Stat.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Stat.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Stat.swift#10 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/File/Stat.html 13 | // 14 | 15 | import Darwin 16 | 17 | // from (not in Foundation) 18 | 19 | /* File mode */ 20 | /* Read, write, execute/search by owner */ 21 | public let S_IRWXU: Int = 0o000700 /* [XSI] RWX mask for owner */ 22 | public let S_IRUSR: Int = 0o000400 /* [XSI] R for owner */ 23 | public let S_IWUSR: Int = 0o000200 /* [XSI] W for owner */ 24 | public let S_IXUSR: Int = 0o000100 /* [XSI] X for owner */ 25 | /* Read, write, execute/search by group */ 26 | public let S_IRWXG: Int = 0o000070 /* [XSI] RWX mask for group */ 27 | public let S_IRGRP: Int = 0o000040 /* [XSI] R for group */ 28 | public let S_IWGRP: Int = 0o000020 /* [XSI] W for group */ 29 | public let S_IXGRP: Int = 0o000010 /* [XSI] X for group */ 30 | /* Read, write, execute/search by others */ 31 | public let S_IRWXO: Int = 0o000007 /* [XSI] RWX mask for other */ 32 | public let S_IROTH: Int = 0o000004 /* [XSI] R for other */ 33 | public let S_IWOTH: Int = 0o000002 /* [XSI] W for other */ 34 | public let S_IXOTH: Int = 0o000001 /* [XSI] X for other */ 35 | 36 | public let S_ISUID: Int = 0o004000 /* [XSI] set user id on execution */ 37 | public let S_ISGID: Int = 0o002000 /* [XSI] set group id on execution */ 38 | public let S_ISVTX: Int = 0o001000 /* [XSI] directory restrcted delete */ 39 | 40 | public let S_IFMT : Int = 0o170000 /* [XSI] type of file mask */ 41 | public let S_IFIFO: Int = 0o010000 /* [XSI] named pipe (fifo) */ 42 | public let S_IFCHR: Int = 0o020000 /* [XSI] character special */ 43 | public let S_IFDIR: Int = 0o040000 /* [XSI] directory */ 44 | public let S_IFBLK: Int = 0o060000 /* [XSI] block special */ 45 | public let S_IFREG: Int = 0o100000 /* [XSI] regular */ 46 | public let S_IFLNK: Int = 0o120000 /* [XSI] symbolic link */ 47 | public let S_IFSOCK:Int = 0o140000 /* [XSI] socket */ 48 | 49 | /* Linux only? */ 50 | public let S_IRUGO = (S_IRUSR|S_IRGRP|S_IROTH) 51 | public let S_IWUGO = (S_IWUSR|S_IWGRP|S_IWOTH) 52 | public let S_IXUGO = (S_IXUSR|S_IXGRP|S_IXOTH) 53 | 54 | public func S_ISBLK(_ m: Int) -> Bool { 55 | return (((m) & S_IFMT) == S_IFBLK) /* block special */ 56 | } 57 | 58 | public func S_ISCHR(_ m: Int) -> Bool { 59 | return (((m) & S_IFMT) == S_IFCHR) /* char special */ 60 | } 61 | 62 | public func S_ISDIR(_ m: Int) -> Bool { 63 | return (((m) & S_IFMT) == S_IFDIR) /* directory */ 64 | } 65 | 66 | public func S_ISFIFO(_ m: Int) -> Bool { 67 | return (((m) & S_IFMT) == S_IFIFO) /* fifo or socket */ 68 | } 69 | 70 | public func S_ISREG(_ m: Int) -> Bool { 71 | return (((m) & S_IFMT) == S_IFREG) /* regular file */ 72 | } 73 | 74 | public func S_ISLNK(_ m: Int) -> Bool { 75 | return (((m) & S_IFMT) == S_IFLNK) /* symbolic link */ 76 | } 77 | 78 | public func S_ISSOCK(_ m: Int) -> Bool { 79 | return (((m) & S_IFMT) == S_IFSOCK) /* socket */ 80 | } 81 | 82 | public func ==(lhs: Stat, rhs: Stat) -> Bool { 83 | return lhs.dev == rhs.dev && lhs.ino == rhs.ino 84 | } 85 | 86 | open class Stat : RubyObject { 87 | 88 | open var info = stat() 89 | 90 | open class func new(_ filename: string_like, statLink: Bool = false, file: StaticString = #file, line: UInt = #line) -> Stat? { 91 | return Stat(filename, statLink: statLink, file: file, line: line) 92 | } 93 | 94 | public convenience init?(fd: Int, file: StaticString, line: UInt) { 95 | self.init() 96 | if !unixOK("Stat.fstat #\(fd)", fstat(Int32(fd), &info), file: file, line: line) || info.st_dev == 0 { 97 | return nil 98 | } 99 | } 100 | 101 | public convenience init?(_ filepath: string_like, statLink: Bool = false, file: StaticString?, line: UInt = 0) { 102 | self.init() 103 | if statLink { 104 | if !unixOK("Stat.lstat \(filepath.to_s)", lstat(filepath.to_s, &info), file: file, line: line) { 105 | return nil 106 | } 107 | } 108 | else { 109 | if !unixOK("Stat.stat \(filepath.to_s)", stat(filepath.to_s, &info), file: file, line: line) { 110 | return nil 111 | } 112 | } 113 | } 114 | 115 | // MARK: Instance methods 116 | 117 | open var atime: Time { 118 | return Time(spec: info.st_atimespec) 119 | } 120 | 121 | open var birthtime: Time { 122 | return Time(spec: info.st_ctimespec) 123 | } 124 | 125 | open var blksize: Int { 126 | return Int(info.st_blksize) 127 | } 128 | 129 | open var blockdev: Bool { 130 | return S_ISBLK(mode) 131 | } 132 | 133 | open var blocks: Int { 134 | return Int(info.st_blocks) 135 | } 136 | 137 | open var chardev: Bool { 138 | return S_ISCHR(mode) 139 | } 140 | 141 | open var ctime: Time { 142 | return Time(spec: info.st_ctimespec) 143 | } 144 | 145 | open var dev: Int { 146 | return Int(info.st_dev) 147 | } 148 | 149 | open var directory: Bool { 150 | return S_ISDIR(mode) 151 | } 152 | 153 | open var executable: Bool { 154 | if geteuid() == 0 { 155 | return true 156 | } 157 | if owned { 158 | return mode & S_IXUSR != 0 159 | } 160 | if grpowned { 161 | return mode & S_IXGRP != 0 162 | } 163 | if mode & S_IXOTH == 0 { 164 | return false 165 | } 166 | return true 167 | } 168 | 169 | open var executable_real: Bool { 170 | if getuid() == 0 { 171 | return true 172 | } 173 | if rowned { 174 | return mode & S_IXUSR != 0 175 | } 176 | if rgrpowned { 177 | return mode & S_IXGRP != 0 178 | } 179 | if mode & S_IXOTH == 0 { 180 | return false 181 | } 182 | return true 183 | } 184 | 185 | open var file: Bool { 186 | return S_ISREG(mode) 187 | } 188 | 189 | open var ftype: String { 190 | return 191 | S_ISREG(mode) ? "file" : 192 | S_ISDIR(mode) ? "directory" : 193 | S_ISCHR(mode) ? "characterSpecial" : 194 | S_ISBLK(mode) ? "blockSpecial" : 195 | S_ISFIFO(mode) ? "fifo" : 196 | S_ISLNK(mode) ? "link" : 197 | S_ISSOCK(mode) ? "socket" : 198 | "unknown" 199 | } 200 | 201 | open var gid: Int { 202 | return Int(info.st_gid) 203 | } 204 | 205 | open var grpowned: Bool { 206 | let egid = gid_t(getegid()) 207 | var groups = [gid_t](repeating: 0, count: 1000) 208 | let gcount = getgroups(Int32(groups.count), &groups) 209 | 210 | for g in 0.. String { 43 | return slice(i) 44 | } 45 | 46 | public subscript (start: Int, len: Int) -> String { 47 | return slice(start, len: len) 48 | } 49 | 50 | public subscript (r: Range) -> String { 51 | return String(self[index(startIndex, offsetBy: r.lowerBound) ..< 52 | index(startIndex, offsetBy: r.upperBound)]) 53 | } 54 | 55 | public var to_s: String { 56 | return self 57 | } 58 | 59 | public var to_a: [String] { 60 | // ??? self.characters.count 61 | return [self] 62 | } 63 | 64 | public var to_c: [CChar] { 65 | if let chars = cString(using: STRING_ENCODING) { 66 | return chars 67 | } 68 | 69 | SRLog("String.to_c, unable to encode string for output") 70 | return U(cString(using: FALLBACK_OUTPUT_ENCODING)) 71 | } 72 | 73 | public var to_d: Data { 74 | return Data(array: self.to_c) 75 | } 76 | 77 | public var to_i: Int { 78 | if let val = Int(self) { 79 | return val 80 | } 81 | let dummy = -99999999 82 | SRLog("Unable to convert \(self) to Int. Returning \(dummy)") 83 | return dummy 84 | } 85 | 86 | public var to_f: Double { 87 | if let val = Double(self) { 88 | return val 89 | } 90 | let dummy = -99999999.0 91 | SRLog("Unable to convert \(self) to Doubleb. Returning \(dummy)") 92 | return dummy 93 | } 94 | 95 | public func characterAtIndex(_ i: Int) -> Int { 96 | if let char = self[i].unicodeScalars.first { 97 | return Int(char.value) 98 | } 99 | SRLog("No character available in string '\(self)' returning nul char") 100 | return 0 101 | } 102 | 103 | public var downcase: String { 104 | return self.lowercased() 105 | } 106 | 107 | public func each_byte(_ block: (UInt8) -> ()) { 108 | for char in utf8 { 109 | block(char) 110 | } 111 | } 112 | 113 | public func each_char(_ block: (UInt16) -> ()) { 114 | for char in utf16 { 115 | block(char) 116 | } 117 | } 118 | 119 | public func each_codepoint(_ block: (String) -> ()) { 120 | for char in self { 121 | block(String(char )) 122 | } 123 | } 124 | 125 | public func each_line(_ block: (String) -> ()) { 126 | StringIO(self).each_line(LINE_SEPARATOR, nil, block) 127 | } 128 | 129 | public var length: Int { 130 | return count 131 | } 132 | 133 | public var ord: Int { 134 | return characterAtIndex(0) 135 | } 136 | 137 | public func slice(_ start: Int, len: Int = 1) -> String { 138 | var vstart = start, vlen = len 139 | let length = self.length 140 | 141 | if start < 0 { 142 | vstart = length + start 143 | } 144 | if vstart < 0 { 145 | SRLog("String.str(\(start), \(len)) start before front of string '\(self)', length \(length)") 146 | if STRING_INDEX_DISPOSITION == .truncate { 147 | vstart = 0 148 | } 149 | } 150 | else if vstart > length { 151 | SRLog("String.str(\(start), \(len)) start after end of string '\(self)', length \(length)") 152 | if STRING_INDEX_DISPOSITION == .truncate { 153 | vstart = length 154 | } 155 | } 156 | 157 | if len < 0 { 158 | vlen = length + len - vstart 159 | } 160 | else if len == NSNotFound { 161 | vlen = length - vstart 162 | } 163 | if vlen < 0 { 164 | SRLog("String.str(\(start), \(len)) start + len before start of substring '\(self)', length \(length)") 165 | if STRING_INDEX_DISPOSITION == .truncate { 166 | vlen = 0 167 | } 168 | } 169 | else if vstart + vlen > length { 170 | SRLog("String.str(\(start), \(len)) start + len after end of string '\(self)', length \(length)") 171 | if STRING_INDEX_DISPOSITION == .truncate { 172 | vlen = length - vstart 173 | } 174 | } 175 | 176 | return self[vstart.. [String] { 180 | return components(separatedBy: delimiter) 181 | } 182 | 183 | public var upcase: String { 184 | return self.uppercased() 185 | } 186 | 187 | } 188 | -------------------------------------------------------------------------------- /StringIO.swift: -------------------------------------------------------------------------------- 1 | // 2 | // StringIO.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 28/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/StringIO.swift#10 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/stdlib-2.2.3/libdoc/stringio/rdoc/StringIO.html 13 | // 14 | 15 | import Darwin 16 | 17 | public var LINE_SEPARATOR = "\n" 18 | 19 | open class StringIO: IO { 20 | 21 | public let data: Data 22 | open var offset = 0 23 | 24 | open override var pos: Int { 25 | get { 26 | return offset 27 | } 28 | set { 29 | offset = newValue 30 | } 31 | } 32 | 33 | public init(_ string: data_like = "", file: StaticString = #file, line: UInt = #line) { 34 | data = string.to_d 35 | super.init(what: nil, unixFILE: nil) 36 | } 37 | 38 | open class func new(_ string: data_like, _ mode: string_like = "r", _ perm: Int? = nil, file: StaticString = #file, line: UInt = #line) -> StringIO { 39 | return StringIO(string, file: file, line: line) 40 | } 41 | 42 | open class func open(_ string: data_like, _ mode: string_like = "r", _ perm: Int? = nil, file: StaticString = #file, line: UInt = #line) -> StringIO { 43 | return new(string, mode, perm, file: file, line: line) 44 | } 45 | 46 | open override func each_byte(_ block: (CChar) -> ()) -> IO { 47 | while !eof { 48 | block(data.bytes[offset]) 49 | offset += 1 50 | } 51 | return self 52 | } 53 | 54 | open override var eof: Bool { 55 | return offset >= data.length 56 | } 57 | 58 | open override var getc: String? { 59 | let ret: String? = !eof ? String(data.bytes[offset]) : nil 60 | offset += 1 61 | return ret 62 | } 63 | 64 | override func gets(_ sep: string_like = LINE_SEPARATOR) -> String? { 65 | if eof { 66 | return nil 67 | } 68 | 69 | let sepchar = sep.to_s.ord 70 | let endOfLine = memchr(data.bytes+offset, Int32(sepchar), Int(data.length-offset))?.assumingMemoryBound(to: Int8.self) 71 | 72 | if endOfLine != nil { 73 | endOfLine!.pointee = 0 74 | } 75 | 76 | let out = String(validatingUTF8: data.bytes+offset) 77 | 78 | if endOfLine != nil { 79 | endOfLine!.pointee = Int8(sepchar) 80 | offset = endOfLine! - data.bytes + 1 81 | } 82 | else { 83 | offset = data.length 84 | } 85 | 86 | return out 87 | } 88 | 89 | open override func print(_ string: string_like) -> Int { 90 | return write(string.to_s) 91 | } 92 | 93 | override func putc(_ obj: Int) -> Int { 94 | if data.capacity < data.length + 1 { 95 | data.capacity += 10_000 //// 96 | } 97 | data.bytes[data.length] = Int8(obj) 98 | data.length += 1 99 | return 1 100 | } 101 | 102 | open override func read(_ length: Int?, _ outbuf: Data?) -> Data? { 103 | return data 104 | } 105 | 106 | @discardableResult 107 | open override func rewind(_ file: StaticString = #file, line: UInt = #line) -> IO { 108 | _ = seek(0, Int(SEEK_SET)) 109 | return self 110 | } 111 | 112 | @discardableResult 113 | open override func seek(_ amount: Int, _ whence: Int, file: StaticString = #file, line: UInt = #line) -> Bool { 114 | switch Int32(whence) { 115 | case SEEK_SET: 116 | offset = amount 117 | case SEEK_CUR: 118 | offset += amount 119 | case SEEK_END: 120 | offset = data.length + amount 121 | default: 122 | return false 123 | } 124 | if offset < 0 || offset > data.length { 125 | SRLog("Invalid StringIO.seek \(amount), \(whence) -> \(offset) outside 0-\(data.length)", file: file, line: line) 126 | return false 127 | } 128 | return true 129 | } 130 | 131 | @discardableResult 132 | open override func write(_ string: data_like) -> fixnum { 133 | return data.append(string) 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /SwiftRuby.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "SwiftRuby" 3 | s.version = "1.0" 4 | s.summary = "Port of Ruby api to Swift for scripting" 5 | s.homepage = "https://github.com/RubyNative/SwiftRuby" 6 | s.social_media_url = "https://twitter.com/Injection4Xcode" 7 | s.documentation_url = "https://github.com/RubyNative/SwiftRuby" 8 | s.license = { :type => "MIT" } 9 | s.authors = { "johnno1962" => "ruby@johnholdsworth.com" } 10 | 11 | s.osx.deployment_target = "10.9" 12 | s.ios.deployment_target = "8.0" 13 | s.source = { :git => "https://github.com/RubyNative/SwiftRuby.git", :tag => s.version } 14 | s.source_files = "*.{swift,h,m}" 15 | end 16 | -------------------------------------------------------------------------------- /SwiftRuby.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | BB98DC7F2559897E008E8627 /* SwiftRuby.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB98DC752559897E008E8627 /* SwiftRuby.framework */; }; 11 | BB98DC842559897E008E8627 /* SwiftRubyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DC832559897E008E8627 /* SwiftRubyTests.swift */; }; 12 | BB98DC862559897E008E8627 /* SwiftRuby.h in Headers */ = {isa = PBXBuildFile; fileRef = BB98DC782559897E008E8627 /* SwiftRuby.h */; settings = {ATTRIBUTES = (Public, ); }; }; 13 | BB98DC94255989D7008E8627 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = BB98DC90255989D6008E8627 /* LICENSE */; }; 14 | BB98DC95255989D7008E8627 /* SwiftRuby.podspec in Resources */ = {isa = PBXBuildFile; fileRef = BB98DC91255989D7008E8627 /* SwiftRuby.podspec */; }; 15 | BB98DC96255989D7008E8627 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = BB98DC92255989D7008E8627 /* README.md */; }; 16 | BB98DC97255989D7008E8627 /* MISSING.md in Resources */ = {isa = PBXBuildFile; fileRef = BB98DC93255989D7008E8627 /* MISSING.md */; }; 17 | BB98DC9D25598A05008E8627 /* Kernel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DC9825598A04008E8627 /* Kernel.swift */; }; 18 | BB98DC9E25598A05008E8627 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DC9925598A05008E8627 /* main.swift */; }; 19 | BB98DC9F25598A05008E8627 /* Utilities.h in Headers */ = {isa = PBXBuildFile; fileRef = BB98DC9A25598A05008E8627 /* Utilities.h */; settings = {ATTRIBUTES = (Public, ); }; }; 20 | BB98DCA025598A05008E8627 /* Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DC9B25598A05008E8627 /* Object.swift */; }; 21 | BB98DCA125598A05008E8627 /* Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = BB98DC9C25598A05008E8627 /* Utilities.m */; }; 22 | BB98DCAA25598A3B008E8627 /* Rational.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA325598A3A008E8627 /* Rational.swift */; }; 23 | BB98DCAB25598A3B008E8627 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA425598A3A008E8627 /* Array.swift */; }; 24 | BB98DCAC25598A3B008E8627 /* Regexp.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA525598A3A008E8627 /* Regexp.swift */; }; 25 | BB98DCAD25598A3B008E8627 /* Float.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA625598A3A008E8627 /* Float.swift */; }; 26 | BB98DCAE25598A3B008E8627 /* Integer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA725598A3A008E8627 /* Integer.swift */; }; 27 | BB98DCAF25598A3B008E8627 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA825598A3A008E8627 /* Data.swift */; }; 28 | BB98DCB025598A3B008E8627 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCA925598A3B008E8627 /* String.swift */; }; 29 | BB98DCB925598A7F008E8627 /* StringIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB225598A7F008E8627 /* StringIO.swift */; }; 30 | BB98DCBA25598A7F008E8627 /* Dir.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB325598A7F008E8627 /* Dir.swift */; }; 31 | BB98DCBB25598A7F008E8627 /* Stat.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB425598A7F008E8627 /* Stat.swift */; }; 32 | BB98DCBC25598A7F008E8627 /* Time.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB525598A7F008E8627 /* Time.swift */; }; 33 | BB98DCBD25598A7F008E8627 /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB625598A7F008E8627 /* File.swift */; }; 34 | BB98DCBE25598A7F008E8627 /* IO.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB725598A7F008E8627 /* IO.swift */; }; 35 | BB98DCBF25598A7F008E8627 /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB98DCB825598A7F008E8627 /* FileUtils.swift */; }; 36 | /* End PBXBuildFile section */ 37 | 38 | /* Begin PBXContainerItemProxy section */ 39 | BB98DC802559897E008E8627 /* PBXContainerItemProxy */ = { 40 | isa = PBXContainerItemProxy; 41 | containerPortal = BB98DC6C2559897E008E8627 /* Project object */; 42 | proxyType = 1; 43 | remoteGlobalIDString = BB98DC742559897E008E8627; 44 | remoteInfo = SwiftRuby; 45 | }; 46 | /* End PBXContainerItemProxy section */ 47 | 48 | /* Begin PBXFileReference section */ 49 | BB98DC752559897E008E8627 /* SwiftRuby.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftRuby.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | BB98DC782559897E008E8627 /* SwiftRuby.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftRuby.h; sourceTree = ""; }; 51 | BB98DC7E2559897E008E8627 /* SwiftRubyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftRubyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 52 | BB98DC832559897E008E8627 /* SwiftRubyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftRubyTests.swift; sourceTree = ""; }; 53 | BB98DC852559897E008E8627 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 54 | BB98DC90255989D6008E8627 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; 55 | BB98DC91255989D7008E8627 /* SwiftRuby.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SwiftRuby.podspec; sourceTree = ""; }; 56 | BB98DC92255989D7008E8627 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 57 | BB98DC93255989D7008E8627 /* MISSING.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = MISSING.md; sourceTree = ""; }; 58 | BB98DC9825598A04008E8627 /* Kernel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Kernel.swift; sourceTree = SOURCE_ROOT; }; 59 | BB98DC9925598A05008E8627 /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = SOURCE_ROOT; }; 60 | BB98DC9A25598A05008E8627 /* Utilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Utilities.h; sourceTree = SOURCE_ROOT; }; 61 | BB98DC9B25598A05008E8627 /* Object.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Object.swift; sourceTree = SOURCE_ROOT; }; 62 | BB98DC9C25598A05008E8627 /* Utilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Utilities.m; sourceTree = SOURCE_ROOT; }; 63 | BB98DCA325598A3A008E8627 /* Rational.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Rational.swift; sourceTree = SOURCE_ROOT; }; 64 | BB98DCA425598A3A008E8627 /* Array.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = SOURCE_ROOT; }; 65 | BB98DCA525598A3A008E8627 /* Regexp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Regexp.swift; sourceTree = SOURCE_ROOT; }; 66 | BB98DCA625598A3A008E8627 /* Float.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Float.swift; sourceTree = SOURCE_ROOT; }; 67 | BB98DCA725598A3A008E8627 /* Integer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Integer.swift; sourceTree = SOURCE_ROOT; }; 68 | BB98DCA825598A3A008E8627 /* Data.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = SOURCE_ROOT; }; 69 | BB98DCA925598A3B008E8627 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = SOURCE_ROOT; }; 70 | BB98DCB225598A7F008E8627 /* StringIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringIO.swift; sourceTree = SOURCE_ROOT; }; 71 | BB98DCB325598A7F008E8627 /* Dir.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Dir.swift; sourceTree = SOURCE_ROOT; }; 72 | BB98DCB425598A7F008E8627 /* Stat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stat.swift; sourceTree = SOURCE_ROOT; }; 73 | BB98DCB525598A7F008E8627 /* Time.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Time.swift; sourceTree = SOURCE_ROOT; }; 74 | BB98DCB625598A7F008E8627 /* File.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = SOURCE_ROOT; }; 75 | BB98DCB725598A7F008E8627 /* IO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IO.swift; sourceTree = SOURCE_ROOT; }; 76 | BB98DCB825598A7F008E8627 /* FileUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileUtils.swift; sourceTree = SOURCE_ROOT; }; 77 | BB98DCC025598CAD008E8627 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = SOURCE_ROOT; }; 78 | /* End PBXFileReference section */ 79 | 80 | /* Begin PBXFrameworksBuildPhase section */ 81 | BB98DC722559897E008E8627 /* Frameworks */ = { 82 | isa = PBXFrameworksBuildPhase; 83 | buildActionMask = 2147483647; 84 | files = ( 85 | ); 86 | runOnlyForDeploymentPostprocessing = 0; 87 | }; 88 | BB98DC7B2559897E008E8627 /* Frameworks */ = { 89 | isa = PBXFrameworksBuildPhase; 90 | buildActionMask = 2147483647; 91 | files = ( 92 | BB98DC7F2559897E008E8627 /* SwiftRuby.framework in Frameworks */, 93 | ); 94 | runOnlyForDeploymentPostprocessing = 0; 95 | }; 96 | /* End PBXFrameworksBuildPhase section */ 97 | 98 | /* Begin PBXGroup section */ 99 | BB98DC6B2559897E008E8627 = { 100 | isa = PBXGroup; 101 | children = ( 102 | BB98DC90255989D6008E8627 /* LICENSE */, 103 | BB98DC92255989D7008E8627 /* README.md */, 104 | BB98DC93255989D7008E8627 /* MISSING.md */, 105 | BB98DC91255989D7008E8627 /* SwiftRuby.podspec */, 106 | BB98DC8F255989B2008E8627 /* Core */, 107 | BB98DCA225598A13008E8627 /* Types */, 108 | BB98DCB125598A53008E8627 /* IO */, 109 | BB98DC772559897E008E8627 /* SwiftRuby */, 110 | BB98DC822559897E008E8627 /* SwiftRubyTests */, 111 | BB98DC762559897E008E8627 /* Products */, 112 | ); 113 | sourceTree = ""; 114 | }; 115 | BB98DC762559897E008E8627 /* Products */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | BB98DC752559897E008E8627 /* SwiftRuby.framework */, 119 | BB98DC7E2559897E008E8627 /* SwiftRubyTests.xctest */, 120 | ); 121 | name = Products; 122 | sourceTree = ""; 123 | }; 124 | BB98DC772559897E008E8627 /* SwiftRuby */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | BB98DC782559897E008E8627 /* SwiftRuby.h */, 128 | ); 129 | path = SwiftRuby; 130 | sourceTree = ""; 131 | }; 132 | BB98DC822559897E008E8627 /* SwiftRubyTests */ = { 133 | isa = PBXGroup; 134 | children = ( 135 | BB98DC832559897E008E8627 /* SwiftRubyTests.swift */, 136 | BB98DC852559897E008E8627 /* Info.plist */, 137 | ); 138 | path = SwiftRubyTests; 139 | sourceTree = ""; 140 | }; 141 | BB98DC8F255989B2008E8627 /* Core */ = { 142 | isa = PBXGroup; 143 | children = ( 144 | BB98DCC025598CAD008E8627 /* Info.plist */, 145 | BB98DC9925598A05008E8627 /* main.swift */, 146 | BB98DC9B25598A05008E8627 /* Object.swift */, 147 | BB98DC9825598A04008E8627 /* Kernel.swift */, 148 | BB98DC9A25598A05008E8627 /* Utilities.h */, 149 | BB98DC9C25598A05008E8627 /* Utilities.m */, 150 | ); 151 | name = Core; 152 | sourceTree = ""; 153 | }; 154 | BB98DCA225598A13008E8627 /* Types */ = { 155 | isa = PBXGroup; 156 | children = ( 157 | BB98DCA825598A3A008E8627 /* Data.swift */, 158 | BB98DCA425598A3A008E8627 /* Array.swift */, 159 | BB98DCA925598A3B008E8627 /* String.swift */, 160 | BB98DCA525598A3A008E8627 /* Regexp.swift */, 161 | BB98DCA325598A3A008E8627 /* Rational.swift */, 162 | BB98DCA725598A3A008E8627 /* Integer.swift */, 163 | BB98DCA625598A3A008E8627 /* Float.swift */, 164 | ); 165 | name = Types; 166 | sourceTree = ""; 167 | }; 168 | BB98DCB125598A53008E8627 /* IO */ = { 169 | isa = PBXGroup; 170 | children = ( 171 | BB98DCB725598A7F008E8627 /* IO.swift */, 172 | BB98DCB325598A7F008E8627 /* Dir.swift */, 173 | BB98DCB625598A7F008E8627 /* File.swift */, 174 | BB98DCB825598A7F008E8627 /* FileUtils.swift */, 175 | BB98DCB225598A7F008E8627 /* StringIO.swift */, 176 | BB98DCB525598A7F008E8627 /* Time.swift */, 177 | BB98DCB425598A7F008E8627 /* Stat.swift */, 178 | ); 179 | name = IO; 180 | sourceTree = ""; 181 | }; 182 | /* End PBXGroup section */ 183 | 184 | /* Begin PBXHeadersBuildPhase section */ 185 | BB98DC702559897E008E8627 /* Headers */ = { 186 | isa = PBXHeadersBuildPhase; 187 | buildActionMask = 2147483647; 188 | files = ( 189 | BB98DC862559897E008E8627 /* SwiftRuby.h in Headers */, 190 | BB98DC9F25598A05008E8627 /* Utilities.h in Headers */, 191 | ); 192 | runOnlyForDeploymentPostprocessing = 0; 193 | }; 194 | /* End PBXHeadersBuildPhase section */ 195 | 196 | /* Begin PBXNativeTarget section */ 197 | BB98DC742559897E008E8627 /* SwiftRuby */ = { 198 | isa = PBXNativeTarget; 199 | buildConfigurationList = BB98DC892559897E008E8627 /* Build configuration list for PBXNativeTarget "SwiftRuby" */; 200 | buildPhases = ( 201 | BB98DC702559897E008E8627 /* Headers */, 202 | BB98DC712559897E008E8627 /* Sources */, 203 | BB98DC722559897E008E8627 /* Frameworks */, 204 | BB98DC732559897E008E8627 /* Resources */, 205 | ); 206 | buildRules = ( 207 | ); 208 | dependencies = ( 209 | ); 210 | name = SwiftRuby; 211 | productName = SwiftRuby; 212 | productReference = BB98DC752559897E008E8627 /* SwiftRuby.framework */; 213 | productType = "com.apple.product-type.framework"; 214 | }; 215 | BB98DC7D2559897E008E8627 /* SwiftRubyTests */ = { 216 | isa = PBXNativeTarget; 217 | buildConfigurationList = BB98DC8C2559897E008E8627 /* Build configuration list for PBXNativeTarget "SwiftRubyTests" */; 218 | buildPhases = ( 219 | BB98DC7A2559897E008E8627 /* Sources */, 220 | BB98DC7B2559897E008E8627 /* Frameworks */, 221 | BB98DC7C2559897E008E8627 /* Resources */, 222 | ); 223 | buildRules = ( 224 | ); 225 | dependencies = ( 226 | BB98DC812559897E008E8627 /* PBXTargetDependency */, 227 | ); 228 | name = SwiftRubyTests; 229 | productName = SwiftRubyTests; 230 | productReference = BB98DC7E2559897E008E8627 /* SwiftRubyTests.xctest */; 231 | productType = "com.apple.product-type.bundle.unit-test"; 232 | }; 233 | /* End PBXNativeTarget section */ 234 | 235 | /* Begin PBXProject section */ 236 | BB98DC6C2559897E008E8627 /* Project object */ = { 237 | isa = PBXProject; 238 | attributes = { 239 | LastSwiftUpdateCheck = 1150; 240 | LastUpgradeCheck = 1150; 241 | ORGANIZATIONNAME = "John Holdsworth"; 242 | TargetAttributes = { 243 | BB98DC742559897E008E8627 = { 244 | CreatedOnToolsVersion = 11.5; 245 | LastSwiftMigration = 1150; 246 | }; 247 | BB98DC7D2559897E008E8627 = { 248 | CreatedOnToolsVersion = 11.5; 249 | }; 250 | }; 251 | }; 252 | buildConfigurationList = BB98DC6F2559897E008E8627 /* Build configuration list for PBXProject "SwiftRuby" */; 253 | compatibilityVersion = "Xcode 9.3"; 254 | developmentRegion = en; 255 | hasScannedForEncodings = 0; 256 | knownRegions = ( 257 | en, 258 | Base, 259 | ); 260 | mainGroup = BB98DC6B2559897E008E8627; 261 | productRefGroup = BB98DC762559897E008E8627 /* Products */; 262 | projectDirPath = ""; 263 | projectRoot = ""; 264 | targets = ( 265 | BB98DC742559897E008E8627 /* SwiftRuby */, 266 | BB98DC7D2559897E008E8627 /* SwiftRubyTests */, 267 | ); 268 | }; 269 | /* End PBXProject section */ 270 | 271 | /* Begin PBXResourcesBuildPhase section */ 272 | BB98DC732559897E008E8627 /* Resources */ = { 273 | isa = PBXResourcesBuildPhase; 274 | buildActionMask = 2147483647; 275 | files = ( 276 | BB98DC96255989D7008E8627 /* README.md in Resources */, 277 | BB98DC95255989D7008E8627 /* SwiftRuby.podspec in Resources */, 278 | BB98DC94255989D7008E8627 /* LICENSE in Resources */, 279 | BB98DC97255989D7008E8627 /* MISSING.md in Resources */, 280 | ); 281 | runOnlyForDeploymentPostprocessing = 0; 282 | }; 283 | BB98DC7C2559897E008E8627 /* Resources */ = { 284 | isa = PBXResourcesBuildPhase; 285 | buildActionMask = 2147483647; 286 | files = ( 287 | ); 288 | runOnlyForDeploymentPostprocessing = 0; 289 | }; 290 | /* End PBXResourcesBuildPhase section */ 291 | 292 | /* Begin PBXSourcesBuildPhase section */ 293 | BB98DC712559897E008E8627 /* Sources */ = { 294 | isa = PBXSourcesBuildPhase; 295 | buildActionMask = 2147483647; 296 | files = ( 297 | BB98DC9E25598A05008E8627 /* main.swift in Sources */, 298 | BB98DCBA25598A7F008E8627 /* Dir.swift in Sources */, 299 | BB98DCB025598A3B008E8627 /* String.swift in Sources */, 300 | BB98DCAF25598A3B008E8627 /* Data.swift in Sources */, 301 | BB98DCAC25598A3B008E8627 /* Regexp.swift in Sources */, 302 | BB98DCBE25598A7F008E8627 /* IO.swift in Sources */, 303 | BB98DCBD25598A7F008E8627 /* File.swift in Sources */, 304 | BB98DCA025598A05008E8627 /* Object.swift in Sources */, 305 | BB98DCAE25598A3B008E8627 /* Integer.swift in Sources */, 306 | BB98DCBF25598A7F008E8627 /* FileUtils.swift in Sources */, 307 | BB98DCAA25598A3B008E8627 /* Rational.swift in Sources */, 308 | BB98DCAD25598A3B008E8627 /* Float.swift in Sources */, 309 | BB98DCB925598A7F008E8627 /* StringIO.swift in Sources */, 310 | BB98DCAB25598A3B008E8627 /* Array.swift in Sources */, 311 | BB98DCBB25598A7F008E8627 /* Stat.swift in Sources */, 312 | BB98DCBC25598A7F008E8627 /* Time.swift in Sources */, 313 | BB98DCA125598A05008E8627 /* Utilities.m in Sources */, 314 | BB98DC9D25598A05008E8627 /* Kernel.swift in Sources */, 315 | ); 316 | runOnlyForDeploymentPostprocessing = 0; 317 | }; 318 | BB98DC7A2559897E008E8627 /* Sources */ = { 319 | isa = PBXSourcesBuildPhase; 320 | buildActionMask = 2147483647; 321 | files = ( 322 | BB98DC842559897E008E8627 /* SwiftRubyTests.swift in Sources */, 323 | ); 324 | runOnlyForDeploymentPostprocessing = 0; 325 | }; 326 | /* End PBXSourcesBuildPhase section */ 327 | 328 | /* Begin PBXTargetDependency section */ 329 | BB98DC812559897E008E8627 /* PBXTargetDependency */ = { 330 | isa = PBXTargetDependency; 331 | target = BB98DC742559897E008E8627 /* SwiftRuby */; 332 | targetProxy = BB98DC802559897E008E8627 /* PBXContainerItemProxy */; 333 | }; 334 | /* End PBXTargetDependency section */ 335 | 336 | /* Begin XCBuildConfiguration section */ 337 | BB98DC872559897E008E8627 /* Debug */ = { 338 | isa = XCBuildConfiguration; 339 | buildSettings = { 340 | ALWAYS_SEARCH_USER_PATHS = NO; 341 | BUILD_LIBRARY_FOR_DISTRIBUTION = YES; 342 | CLANG_ANALYZER_NONNULL = YES; 343 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 344 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 345 | CLANG_CXX_LIBRARY = "libc++"; 346 | CLANG_ENABLE_MODULES = YES; 347 | CLANG_ENABLE_OBJC_ARC = YES; 348 | CLANG_ENABLE_OBJC_WEAK = YES; 349 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 350 | CLANG_WARN_BOOL_CONVERSION = YES; 351 | CLANG_WARN_COMMA = YES; 352 | CLANG_WARN_CONSTANT_CONVERSION = YES; 353 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 354 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 355 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 356 | CLANG_WARN_EMPTY_BODY = YES; 357 | CLANG_WARN_ENUM_CONVERSION = YES; 358 | CLANG_WARN_INFINITE_RECURSION = YES; 359 | CLANG_WARN_INT_CONVERSION = YES; 360 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 361 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 362 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 363 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 364 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 365 | CLANG_WARN_STRICT_PROTOTYPES = YES; 366 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 367 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 368 | CLANG_WARN_UNREACHABLE_CODE = YES; 369 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 370 | COPY_PHASE_STRIP = NO; 371 | CURRENT_PROJECT_VERSION = 1; 372 | DEBUG_INFORMATION_FORMAT = dwarf; 373 | DIAMOND_HOME = $HOME/Library/Diamond; 374 | ENABLE_STRICT_OBJC_MSGSEND = YES; 375 | ENABLE_TESTABILITY = YES; 376 | FRAMEWORK_SEARCH_PATHS = ( 377 | "$(inherited)", 378 | "$(DIAMOND_HOME)/Frameworks", 379 | ); 380 | GCC_C_LANGUAGE_STANDARD = gnu11; 381 | GCC_DYNAMIC_NO_PIC = NO; 382 | GCC_NO_COMMON_BLOCKS = YES; 383 | GCC_OPTIMIZATION_LEVEL = 0; 384 | GCC_PREPROCESSOR_DEFINITIONS = ( 385 | "DEBUG=1", 386 | "$(inherited)", 387 | ); 388 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 389 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 390 | GCC_WARN_UNDECLARED_SELECTOR = YES; 391 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 392 | GCC_WARN_UNUSED_FUNCTION = YES; 393 | GCC_WARN_UNUSED_VARIABLE = YES; 394 | LD_RUNPATH_SEARCH_PATHS = ( 395 | "$(inherited)", 396 | "$(DIAMOND_HOME)/Frameworks", 397 | "@executable_path/../Library/Diamond/Frameworks", 398 | "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx", 399 | "@executable_path/../Frameworks", 400 | "@loader_path/../Frameworks", 401 | ); 402 | MACOSX_DEPLOYMENT_TARGET = 10.9; 403 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 404 | MTL_FAST_MATH = YES; 405 | ONLY_ACTIVE_ARCH = YES; 406 | SDKROOT = macosx; 407 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 408 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 409 | SWIFT_VERSION = 5.0; 410 | SYMROOT = "$(DIAMOND_HOME)/Build/$(PLATFORM_NAME)"; 411 | VERSIONING_SYSTEM = "apple-generic"; 412 | VERSION_INFO_PREFIX = ""; 413 | }; 414 | name = Debug; 415 | }; 416 | BB98DC882559897E008E8627 /* Release */ = { 417 | isa = XCBuildConfiguration; 418 | buildSettings = { 419 | ALWAYS_SEARCH_USER_PATHS = NO; 420 | BUILD_LIBRARY_FOR_DISTRIBUTION = YES; 421 | CLANG_ANALYZER_NONNULL = YES; 422 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 423 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 424 | CLANG_CXX_LIBRARY = "libc++"; 425 | CLANG_ENABLE_MODULES = YES; 426 | CLANG_ENABLE_OBJC_ARC = YES; 427 | CLANG_ENABLE_OBJC_WEAK = YES; 428 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 429 | CLANG_WARN_BOOL_CONVERSION = YES; 430 | CLANG_WARN_COMMA = YES; 431 | CLANG_WARN_CONSTANT_CONVERSION = YES; 432 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 433 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 434 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 435 | CLANG_WARN_EMPTY_BODY = YES; 436 | CLANG_WARN_ENUM_CONVERSION = YES; 437 | CLANG_WARN_INFINITE_RECURSION = YES; 438 | CLANG_WARN_INT_CONVERSION = YES; 439 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 440 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 441 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 442 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 443 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 444 | CLANG_WARN_STRICT_PROTOTYPES = YES; 445 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 446 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 447 | CLANG_WARN_UNREACHABLE_CODE = YES; 448 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 449 | COPY_PHASE_STRIP = NO; 450 | CURRENT_PROJECT_VERSION = 1; 451 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 452 | DIAMOND_HOME = $HOME/Library/Diamond; 453 | ENABLE_NS_ASSERTIONS = NO; 454 | ENABLE_STRICT_OBJC_MSGSEND = YES; 455 | FRAMEWORK_SEARCH_PATHS = ( 456 | "$(inherited)", 457 | "$(DIAMOND_HOME)/Frameworks", 458 | ); 459 | GCC_C_LANGUAGE_STANDARD = gnu11; 460 | GCC_NO_COMMON_BLOCKS = YES; 461 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 462 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 463 | GCC_WARN_UNDECLARED_SELECTOR = YES; 464 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 465 | GCC_WARN_UNUSED_FUNCTION = YES; 466 | GCC_WARN_UNUSED_VARIABLE = YES; 467 | LD_RUNPATH_SEARCH_PATHS = ( 468 | "$(inherited)", 469 | "$(DIAMOND_HOME)/Frameworks", 470 | "@executable_path/../Library/Diamond/Frameworks", 471 | "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx", 472 | "@executable_path/../Frameworks", 473 | "@loader_path/../Frameworks", 474 | ); 475 | MACOSX_DEPLOYMENT_TARGET = 10.9; 476 | MTL_ENABLE_DEBUG_INFO = NO; 477 | MTL_FAST_MATH = YES; 478 | SDKROOT = macosx; 479 | SWIFT_COMPILATION_MODE = wholemodule; 480 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 481 | SWIFT_VERSION = 5.0; 482 | SYMROOT = "$(DIAMOND_HOME)/Build/$(PLATFORM_NAME)"; 483 | VERSIONING_SYSTEM = "apple-generic"; 484 | VERSION_INFO_PREFIX = ""; 485 | }; 486 | name = Release; 487 | }; 488 | BB98DC8A2559897E008E8627 /* Debug */ = { 489 | isa = XCBuildConfiguration; 490 | buildSettings = { 491 | CLANG_ENABLE_MODULES = YES; 492 | CODE_SIGN_STYLE = Automatic; 493 | COMBINE_HIDPI_IMAGES = YES; 494 | DEFINES_MODULE = YES; 495 | DEVELOPMENT_TEAM = 9V5A8WE85E; 496 | DYLIB_COMPATIBILITY_VERSION = 1; 497 | DYLIB_CURRENT_VERSION = 1; 498 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 499 | INFOPLIST_FILE = Info.plist; 500 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 501 | PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRuby; 502 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 503 | SKIP_INSTALL = YES; 504 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 505 | }; 506 | name = Debug; 507 | }; 508 | BB98DC8B2559897E008E8627 /* Release */ = { 509 | isa = XCBuildConfiguration; 510 | buildSettings = { 511 | CLANG_ENABLE_MODULES = YES; 512 | CODE_SIGN_STYLE = Automatic; 513 | COMBINE_HIDPI_IMAGES = YES; 514 | DEFINES_MODULE = YES; 515 | DEVELOPMENT_TEAM = 9V5A8WE85E; 516 | DYLIB_COMPATIBILITY_VERSION = 1; 517 | DYLIB_CURRENT_VERSION = 1; 518 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 519 | INFOPLIST_FILE = Info.plist; 520 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 521 | PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRuby; 522 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 523 | SKIP_INSTALL = YES; 524 | }; 525 | name = Release; 526 | }; 527 | BB98DC8D2559897E008E8627 /* Debug */ = { 528 | isa = XCBuildConfiguration; 529 | buildSettings = { 530 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 531 | CODE_SIGN_STYLE = Automatic; 532 | COMBINE_HIDPI_IMAGES = YES; 533 | DEVELOPMENT_TEAM = 9V5A8WE85E; 534 | INFOPLIST_FILE = SwiftRubyTests/Info.plist; 535 | LD_RUNPATH_SEARCH_PATHS = ( 536 | "$(inherited)", 537 | "@executable_path/../Frameworks", 538 | "@loader_path/../Frameworks", 539 | ); 540 | PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRubyTests; 541 | PRODUCT_NAME = "$(TARGET_NAME)"; 542 | }; 543 | name = Debug; 544 | }; 545 | BB98DC8E2559897E008E8627 /* Release */ = { 546 | isa = XCBuildConfiguration; 547 | buildSettings = { 548 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 549 | CODE_SIGN_STYLE = Automatic; 550 | COMBINE_HIDPI_IMAGES = YES; 551 | DEVELOPMENT_TEAM = 9V5A8WE85E; 552 | INFOPLIST_FILE = SwiftRubyTests/Info.plist; 553 | LD_RUNPATH_SEARCH_PATHS = ( 554 | "$(inherited)", 555 | "@executable_path/../Frameworks", 556 | "@loader_path/../Frameworks", 557 | ); 558 | PRODUCT_BUNDLE_IDENTIFIER = com.johnholdsworth.SwiftRubyTests; 559 | PRODUCT_NAME = "$(TARGET_NAME)"; 560 | }; 561 | name = Release; 562 | }; 563 | /* End XCBuildConfiguration section */ 564 | 565 | /* Begin XCConfigurationList section */ 566 | BB98DC6F2559897E008E8627 /* Build configuration list for PBXProject "SwiftRuby" */ = { 567 | isa = XCConfigurationList; 568 | buildConfigurations = ( 569 | BB98DC872559897E008E8627 /* Debug */, 570 | BB98DC882559897E008E8627 /* Release */, 571 | ); 572 | defaultConfigurationIsVisible = 0; 573 | defaultConfigurationName = Release; 574 | }; 575 | BB98DC892559897E008E8627 /* Build configuration list for PBXNativeTarget "SwiftRuby" */ = { 576 | isa = XCConfigurationList; 577 | buildConfigurations = ( 578 | BB98DC8A2559897E008E8627 /* Debug */, 579 | BB98DC8B2559897E008E8627 /* Release */, 580 | ); 581 | defaultConfigurationIsVisible = 0; 582 | defaultConfigurationName = Release; 583 | }; 584 | BB98DC8C2559897E008E8627 /* Build configuration list for PBXNativeTarget "SwiftRubyTests" */ = { 585 | isa = XCConfigurationList; 586 | buildConfigurations = ( 587 | BB98DC8D2559897E008E8627 /* Debug */, 588 | BB98DC8E2559897E008E8627 /* Release */, 589 | ); 590 | defaultConfigurationIsVisible = 0; 591 | defaultConfigurationName = Release; 592 | }; 593 | /* End XCConfigurationList section */ 594 | }; 595 | rootObject = BB98DC6C2559897E008E8627 /* Project object */; 596 | } 597 | -------------------------------------------------------------------------------- /SwiftRuby.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftRuby/SwiftRuby.h: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftRuby.h 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 09/11/2020. 6 | // Copyright © 2020 John Holdsworth. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for SwiftRuby. 12 | FOUNDATION_EXPORT double SwiftRubyVersionNumber; 13 | 14 | //! Project version string for SwiftRuby. 15 | FOUNDATION_EXPORT const unsigned char SwiftRubyVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | #import "Utilities.h" 20 | -------------------------------------------------------------------------------- /SwiftRubyTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /SwiftRubyTests/SwiftRubyTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftRubyTests.swift 3 | // SwiftRubyTests 4 | // 5 | // Created by John Holdsworth on 30/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/SwiftRubyTests/SwiftRubyTests.swift#17 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | 13 | import XCTest 14 | @testable import SwiftRuby 15 | 16 | class SwiftRubyTests: XCTestCase { 17 | 18 | override func setUp() { 19 | super.setUp() 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | override func tearDown() { 24 | // Put teardown code here. This method is called after the invocation of each test method in the class. 25 | super.tearDown() 26 | } 27 | 28 | func testKit() { 29 | // This is an example of a functional test case. 30 | // Use XCTAssert and related functions to verify your tests produce the correct results. 31 | 32 | XCTAssertEqual("€".to_d.to_s.ord.chr, "€", "basic unicode") 33 | 34 | let invalid = "/tmp/tmp/tmp" 35 | XCTAssertFalse(Dir.mkdir(invalid), "failed create directory ") 36 | XCTAssertFalse(FileUtils.mkdir(invalid), "failed create directory ") 37 | 38 | let testdir = "/tmp/rktest" 39 | XCTAssert(FileUtils.rm_rf(testdir), "reset test directory") 40 | XCTAssert(Dir.mkdir(testdir), "recreate test driectory") 41 | XCTAssert(Dir.chdir(testdir), "chdir test directory") 42 | 43 | let string1 = "🇩🇪🇺🇸🇫🇷🇮🇹🇬🇧\n🇪🇸🇯🇵🇷🇺🇨🇳\n" 44 | XCTAssertEqual(File.write("same1.txt", string1), string1.utf8.count, "write same1") 45 | 46 | let string2 = StringIO("🇩🇪🇺🇸🇫🇷🇮🇹🇬🇧\n") 47 | string2.write("🇪🇸🇯🇵🇷🇺🇨🇳\n") 48 | 49 | XCTAssertEqual(File.write("same2.txt", string2), string2.data.length, "write same2") 50 | 51 | XCTAssertEqual(File.open("same1.txt")!.to_a, ["🇩🇪🇺🇸🇫🇷🇮🇹🇬🇧", "🇪🇸🇯🇵🇷🇺🇨🇳"], "readlines file") 52 | 53 | string2.rewind() 54 | XCTAssertEqual(string2.to_a, ["🇩🇪🇺🇸🇫🇷🇮🇹🇬🇧", "🇪🇸🇯🇵🇷🇺🇨🇳"], "readlines stringIO") 55 | 56 | if let file = File.open("diff1.txt", "w") { 57 | file.write(string2) 58 | file.write(string2) 59 | } 60 | 61 | let refernce = "€ Unicode String €" 62 | var string3 = "" 63 | StringIO(refernce).each_char { 64 | (char) in 65 | string3 += Int(char).chr 66 | } 67 | 68 | XCTAssertEqual(string3, refernce, "char block") 69 | 70 | XCTAssertTrue(FileUtils.compare_file("same1.txt", "same2.txt"), "basic same") 71 | XCTAssertFalse(FileUtils.compare_file("same1.txt", "diff1.txt"), "basic diff") 72 | 73 | XCTAssertTrue(FileUtils.compare_stream(File.open("same1.txt")!, File.open("same2.txt")!), "stream compare") 74 | XCTAssertFalse(FileUtils.compare_stream(File.open("same1.txt")!, File.open("diff1.txt")!), "stream diff") 75 | 76 | XCTAssert(fabs(Time().to_f - File.mtime("diff1.txt")!.to_f) <= 5.0, "modification time") 77 | 78 | let largeFile = "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/IDEKit" 79 | XCTAssert(File.open(largeFile)!.read()! == IO.popen("cat \(largeFile)")!.read()!, "large file") 80 | 81 | WARNING_DISPOSITION = .ignore 82 | for mode in [0o700, 0o070, 0o007, 0o000] { 83 | File.chmod(mode, "diff1.txt") 84 | XCTAssertEqual(File.open("diff1.txt", "r") != nil, File.readable("diff1.txt"), "permission \(mode)") 85 | } 86 | 87 | let files = ["diff1.txt", "same1.txt", "same2.txt"] 88 | XCTAssertEqual(Dir.glob(testdir+"/*.txt")!.sorted(), files.map { testdir+"/"+$0 }, "glob directory") 89 | XCTAssertEqual(Dir.open(".")!.to_a.sorted(), [".", ".."]+files, "read directory") 90 | XCTAssertEqual(Kernel.open("| ls \(testdir)")!.to_a, files, "read popen") 91 | 92 | XCTAssertEqual("🇩🇪🇺🇸\n🇩🇪🇺🇸\n"["^(..)🇺🇸", .anchorsMatchLines]["$1🇪🇸"], "🇩🇪🇪🇸\n🇩🇪🇪🇸\n", "unicode replace") 93 | XCTAssertEqual("🇩🇪🇺🇸\n🇩🇪🇺🇸\n"["^(.*)🇺🇸", "m"]["$1🇪🇸"], "🇩🇪🇪🇸\n🇩🇪🇪🇸\n", "unicode replace") 94 | 95 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[2], "🇺🇸", "basic subscript") 96 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[2, 3], "🇺🇸a🇫🇷", "simple subscript") 97 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[2..<7], "🇺🇸a🇫🇷a🇮🇹", "range subscript") 98 | 99 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧".sub("a", "b"), "🇩🇪b🇺🇸a🇫🇷a🇮🇹a🇬🇧", "single replace") 100 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"["🇺🇸(.)"][1], "a", "regexp group") 101 | XCTAssertEqual(" abc ".index("abc"), 3, "index") 102 | XCTAssertEqual(" abc ".strip, "abc", "strip") 103 | 104 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-1], "🇬🇧", "-ve subscript") 105 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-3, -1], "🇮🇹a", "two -ve subscript") 106 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-5, NSNotFound], "🇫🇷a🇮🇹a🇬🇧", "-ve to end") 107 | 108 | WARNING_DISPOSITION = .warn 109 | STRING_INDEX_DISPOSITION = .truncate 110 | 111 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[0, 20], "🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧", "start + len") 112 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-20, -1], "🇩🇪a🇺🇸a🇫🇷a🇮🇹a", "start < front") 113 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-2, 20], "a🇬🇧", "start + end > back") 114 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[-2, -20], "", "end < start") 115 | XCTAssertEqual("🇩🇪a🇺🇸a🇫🇷a🇮🇹a🇬🇧"[20, 0], "", "start > back") 116 | 117 | let testPath = "/a/b/c.d" 118 | XCTAssertEqual(File.dirname(testPath), "/a/b", "dirname") 119 | XCTAssertEqual(File.basename(testPath), "c.d", "basename") 120 | XCTAssertEqual(File.extname(testPath), "d", "extname") 121 | XCTAssertEqual(File.extremoved(testPath), "/a/b/c", "removeext") 122 | 123 | XCTAssertEqual(Dir.home(), ENV["HOME"], "home directory") 124 | } 125 | 126 | func testPerformanceExample() { 127 | // This is an example of a performance test case. 128 | self.measure { 129 | // Put the code you want to measure the time of here. 130 | } 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /Time.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Time.swift 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Time.swift#11 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | // See: http://ruby-doc.org/core-2.2.3/Time.html 13 | // 14 | 15 | import Darwin 16 | 17 | open class Time : RubyObject, string_like { 18 | 19 | open var value = timeval() 20 | open var tzone = timezone() 21 | open var tmout: tm? 22 | 23 | open var isUTC = false { 24 | didSet { 25 | tmout = nil 26 | } 27 | } 28 | 29 | open class func now() -> Time { 30 | return Time() 31 | } 32 | 33 | public convenience override init() { 34 | self.init(seconds: nil) 35 | gettimeofday(&value, &tzone) 36 | } 37 | 38 | public init(seconds: Int?, usec: Int = 0) { /// 39 | super.init() 40 | if seconds != nil { 41 | value.tv_sec = seconds!.to_i 42 | value.tv_usec = suseconds_t(usec.to_i) 43 | } 44 | } 45 | 46 | public convenience init(spec: timespec) { 47 | self.init(seconds: spec.tv_sec, usec: spec.tv_nsec/1_000) 48 | } 49 | 50 | public convenience init(time_f: Double) { 51 | let time_i = Int(time_f.to_f) 52 | self.init(seconds: time_i, usec: Int((time_f.to_f-Double(time_i))*1_000_000)) 53 | } 54 | 55 | // MARK: Class methods 56 | 57 | open class func at(_ time: Time) -> Time { 58 | return Time(seconds: Int(time.value.tv_sec), usec: Int(time.value.tv_usec)) 59 | } 60 | 61 | open class func at(_ time_f: float_like) -> Time { 62 | return Time(time_f: time_f.to_f) 63 | } 64 | 65 | open class func at(_ time: Int, usec: Double = 0) -> Time { 66 | return Time(seconds: time, usec: Int(usec.to_f)) 67 | } 68 | 69 | open class func at(_ time: string_like, format: string_like = "%Y-%m-%d %H:%M:%S %z") -> Time { 70 | return strptime(time, format: format) 71 | } 72 | 73 | open class func strptime(_ time: string_like, format: string_like) -> Time { 74 | var mktm = tm() 75 | _ = Darwin.strptime(time.to_s, format.to_s, &mktm) 76 | return Time(seconds: mktime(&mktm )) 77 | } 78 | 79 | open class func local(_ year: Int = 0, month: Int = 0, day: Int = 0, 80 | hour: Int = 0, min: Int = 0, sec: Int = 0, usec_with_frac: Double = 0.0) -> Time { 81 | var mktm = tm(tm_sec: Int32(sec), tm_min: Int32(min), tm_hour: Int32(hour), 82 | tm_mday: 0, tm_mon: Int32(month), tm_year: Int32(year), 83 | tm_wday: 0, tm_yday: 0, tm_isdst: 0, tm_gmtoff: 0, tm_zone: getenv("TZ")) 84 | return Time(seconds: mktime(&mktm )) 85 | } 86 | 87 | // MARK: Instance methods 88 | 89 | func settm() -> tm { 90 | if tmout == nil { 91 | tmout = tm() 92 | if isUTC { 93 | gmtime_r(&value.tv_sec, &tmout!) 94 | } 95 | else { 96 | localtime_r(&value.tv_sec, &tmout!); 97 | } 98 | } 99 | return tmout! 100 | } 101 | 102 | open var asctime: String { 103 | var out = [CChar](repeating: 0, count: 30) 104 | var tmp = settm() 105 | asctime_r(&tmp, &out) 106 | return String(validatingUTF8: out)! 107 | } 108 | 109 | open var ctime: String { 110 | return asctime 111 | } 112 | 113 | open var day: Int { 114 | return Int(settm().tm_mday) 115 | } 116 | 117 | open var dst: Bool { 118 | return settm().tm_isdst != 0 119 | } 120 | 121 | open func eql(_ other_time: Time) -> Bool { 122 | return value.tv_sec == other_time.value.tv_sec && value.tv_usec == other_time.value.tv_usec 123 | } 124 | 125 | open var friday: Bool { 126 | return settm().tm_wday == 5 127 | } 128 | 129 | open var getgm: Time { 130 | let gm = Time.at(self) 131 | gm.isUTC = true 132 | return gm 133 | } 134 | 135 | open var getlocal: Time { 136 | let gm = Time.at(self) 137 | gm.isUTC = false 138 | return gm 139 | } 140 | 141 | open func getlocal(_ utc_offset: Int) -> Time? { 142 | return nil//// 143 | } 144 | 145 | open var getutc: Time { 146 | return getgm 147 | } 148 | 149 | open var gmt: Bool { 150 | return isUTC 151 | } 152 | 153 | open var gmt_offset: Int { 154 | return isUTC ? 0 : settm().tm_gmtoff 155 | } 156 | 157 | open var gmtime: Time { 158 | isUTC = true 159 | return self 160 | } 161 | 162 | open var gmtoff: Int { 163 | return gmt_offset 164 | } 165 | 166 | open var hour: Int { 167 | return Int(settm().tm_hour) 168 | } 169 | 170 | open var inspect: String { 171 | return self.strftime(isUTC ? "%Y-%m-%d %H:%M:%S UTC" : "%Y-%m-%d %H:%M:%S %z") 172 | } 173 | 174 | open var isdst: Bool { 175 | return settm().tm_isdst != 0 176 | } 177 | 178 | open var localtime: Time { 179 | return getlocal 180 | } 181 | 182 | open func localtime(_ utc_offset: Int) -> Time? { 183 | return nil//// 184 | } 185 | 186 | open var mday: fixnum { 187 | return Int(settm().tm_mday) 188 | } 189 | 190 | open var min: fixnum { 191 | return Int(settm().tm_min) 192 | } 193 | 194 | open var mon: fixnum { 195 | return Int(settm().tm_mon)+1 196 | } 197 | 198 | open var month: fixnum { 199 | return Int(settm().tm_mon) 200 | } 201 | 202 | open var monday: Bool { 203 | return settm().tm_wday == 1 204 | } 205 | 206 | open var nsec: Int { 207 | return Int(value.tv_usec) * 1_000 208 | } 209 | 210 | open func round(_ ndigits: Int) -> Time { 211 | let divisor = pow(10.0, Double(ndigits.to_i)) 212 | return Time(time_f: Darwin.round(self.to_f * divisor) / divisor) 213 | } 214 | 215 | open var saturday: Bool { 216 | return settm().tm_wday == 6 217 | } 218 | 219 | open var sec: fixnum { 220 | return Int(settm().tm_sec) 221 | } 222 | 223 | open func strftime(_ format: string_like) -> String { 224 | var out = [Int8](repeating: 0, count: 1000) 225 | var tmp = settm() 226 | _ = Darwin.strftime(&out, out.count, format.to_s, &tmp) 227 | return String(validatingUTF8: out)! 228 | } 229 | 230 | open var succ: Time { 231 | return Time(seconds: value.tv_sec+1, usec: Int(value.tv_usec)) 232 | } 233 | 234 | open var sunday: Bool { 235 | return settm().tm_wday == 0 236 | } 237 | 238 | open var thursday: Bool { 239 | return settm().tm_wday == 4 240 | } 241 | 242 | open var to_a: [String] { 243 | let tmp = settm() 244 | return [String(tmp.tm_sec), String(tmp.tm_min), String(tmp.tm_hour), String(tmp.tm_mday), String(tmp.tm_mon), 245 | String(tmp.tm_year), String(tmp.tm_wday), String(tmp.tm_yday), String(tmp.tm_isdst), String(describing: tmp.tm_zone)] 246 | } 247 | 248 | open var to_f: Double { 249 | return Double(value.tv_sec) + Double(value.tv_usec)/1_000_000 250 | } 251 | 252 | open var to_i: Int { 253 | return Int(value.tv_sec) 254 | } 255 | 256 | open var to_r: Rational? { 257 | return nil//// 258 | } 259 | 260 | open var to_s: String { 261 | return inspect 262 | } 263 | 264 | open var tuesday: Bool { 265 | return settm().tm_wday == 2 266 | } 267 | 268 | open var tv_nsec: Int { 269 | return Int(value.tv_usec) * 1_000 270 | } 271 | 272 | open var tv_sec: Int { 273 | return Int(value.tv_sec) 274 | } 275 | 276 | open var tv_usec: Int { 277 | return Int(value.tv_usec) 278 | } 279 | 280 | open var usec: Int { 281 | return tv_usec 282 | } 283 | 284 | open var utc: Time { 285 | return gmtime 286 | } 287 | 288 | open func utc(_ utc_offset: Int) -> Time? { 289 | return nil/// 290 | } 291 | 292 | open var utc_offset: fixnum { 293 | return gmt_offset 294 | } 295 | 296 | open var wday: fixnum { 297 | return Int(settm().tm_wday) 298 | } 299 | 300 | open var wednesday: Bool { 301 | return settm().tm_wday == 3 302 | } 303 | 304 | open var yday: fixnum { 305 | return Int(settm().tm_yday) 306 | } 307 | 308 | open var year: Int { 309 | return Int(settm().tm_yday) 310 | } 311 | 312 | open var zone: String { 313 | return String(validatingUTF8: settm().tm_zone)! 314 | } 315 | 316 | } 317 | -------------------------------------------------------------------------------- /Utilities.h: -------------------------------------------------------------------------------- 1 | // 2 | // Utilities.h 3 | // SwiftRuby 4 | // 5 | // Created by John Holdsworth on 12/10/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Utilities.h#11 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | 13 | #ifndef Utilities_h 14 | #define Utilities_h 15 | 16 | #import 17 | 18 | extern NSArray * _Nonnull instanceVariablesForClass(Class _Nonnull cls, NSMutableArray * _Nonnull ivarNames); 19 | extern NSArray * _Nonnull methodSymbolsForClass(Class _Nonnull cls, NSMutableArray * _Nonnull syms); 20 | 21 | extern NSString * _Nonnull kCatchLevels; 22 | 23 | extern void _try(void (^ _Nonnull tryBlock)(void)); 24 | extern void _catch(void (^ _Nonnull catchBlock)(NSException * _Nonnull e )); 25 | extern void _throw(NSException * _Nonnull e); 26 | 27 | extern int _system(const char * _Nonnull command); 28 | extern FILE * _Nullable _popen(const char * _Nonnull command, const char * _Nonnull perm); 29 | extern int _pclose(FILE * _Nonnull fp); 30 | 31 | extern void execArgv(NSString * _Nonnull executable, NSArray * _Nonnull arguments); 32 | extern pid_t spawnArgv(NSString * _Nonnull executable, NSArray * _Nonnull arguments); 33 | extern int fcntl3(int fildes, int cmd, int flags); 34 | 35 | #endif /* Utilities_h */ 36 | -------------------------------------------------------------------------------- /Utilities.m: -------------------------------------------------------------------------------- 1 | // 2 | // Utilities.m 3 | // RubyNative 4 | // 5 | // Created by John Holdsworth on 26/09/2015. 6 | // Copyright © 2015 John Holdsworth. All rights reserved. 7 | // 8 | // $Id: //depot/SwiftRuby/Utilities.m#21 $ 9 | // 10 | // Repo: https://github.com/RubyNative/SwiftRuby 11 | // 12 | 13 | #import "Utilities.h" 14 | 15 | #import 16 | #import 17 | #import 18 | 19 | // Thanks to Jay Freeman's https://www.youtube.com/watch?v=Ii-02vhsdVk 20 | 21 | struct _in_objc_class { 22 | 23 | Class meta, supr; 24 | void *cache, *vtable; 25 | struct _in_objc_ronly *internal; 26 | 27 | // data new to swift 28 | int f1, f2; // added for 1.0 Beta5 29 | int size, tos, mdsize, eight; 30 | 31 | struct _swift_data3 { 32 | // unsigned long flags; 33 | // const char *className; 34 | int className; 35 | int fieldcount, flags2; 36 | int ivarNames; 37 | int get_field_data; 38 | // const char *ivarNames; 39 | // struct _swift_field **(*get_field_data)(); 40 | } *swiftData; 41 | 42 | IMP dispatch[1]; 43 | }; 44 | 45 | // Swift3 pointers to some metadata are relative 46 | static const char *swift3Relative(void *ptrPtr) { 47 | intptr_t offset = *(int *)ptrPtr; 48 | return offset < 0 ? (const char *)((intptr_t)ptrPtr + offset) : (const char *)offset; 49 | } 50 | 51 | NSArray *instanceVariablesForClass(Class cls, NSMutableArray *ivarNames) { 52 | assert(0 && "This code is obsolete"); 53 | Class superCls = class_getSuperclass(cls); 54 | if (superCls) 55 | instanceVariablesForClass(superCls, ivarNames); 56 | 57 | struct _in_objc_class *swiftClass = (__bridge struct _in_objc_class *)cls; 58 | 59 | if ((uintptr_t)swiftClass->internal & 0x1) { 60 | struct _swift_data3 *swiftData = (struct _swift_data3 *)swift3Relative(&swiftClass->swiftData); 61 | const char *nameptr = swift3Relative(&swiftData->ivarNames); 62 | 63 | for (int f = 0 ; f < swiftData->fieldcount ; f++) { 64 | [ivarNames addObject:[NSString stringWithFormat:@"%@.%@", NSStringFromClass(cls), 65 | [NSString stringWithUTF8String:nameptr]]]; 66 | nameptr += strlen(nameptr) + 1; 67 | } 68 | } 69 | else { 70 | unsigned ic; 71 | Ivar *ivars = class_copyIvarList(cls, &ic); 72 | for (unsigned i=0 ; i *methodSymbolsForClass(Class cls, NSMutableArray *syms) { 81 | assert(0 && "This code is obsolete"); 82 | 83 | struct _in_objc_class *swiftClass = (__bridge struct _in_objc_class *)cls; 84 | 85 | IMP *sym_start = swiftClass->dispatch, 86 | *sym_end = (IMP *)((char *)swiftClass + swiftClass->mdsize - 2*sizeof(IMP)); 87 | 88 | Dl_info info; 89 | for (IMP *sym_ptr = sym_start ; sym_ptr < sym_end ; sym_ptr++) 90 | if (dladdr(*sym_ptr, &info) && info.dli_sname) 91 | [syms addObject:[NSString stringWithUTF8String:info.dli_sname]]; 92 | 93 | return syms; 94 | } 95 | 96 | static NSString *kLastExceptionKey = @"SwiftRubyException"; 97 | NSString *kCatchLevels = @"SwiftRubyCatchLevels"; 98 | 99 | void _try(void (^tryBlock)(void)) { 100 | NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary; 101 | int catchLevels = [threadDictionary[kCatchLevels] intValue]; 102 | threadDictionary[kCatchLevels] = @(catchLevels+1); 103 | threadDictionary[kLastExceptionKey] = nil; 104 | @try { 105 | tryBlock(); 106 | } 107 | @catch (NSException *e) { 108 | threadDictionary[kLastExceptionKey] = e; 109 | } 110 | threadDictionary[kCatchLevels] = @(catchLevels); 111 | } 112 | 113 | void _catch(void (^catchBlock)(NSException *e )) { 114 | NSMutableDictionary *threadDictionary = [NSThread currentThread].threadDictionary; 115 | NSException *e = threadDictionary[kLastExceptionKey]; 116 | if (e) { 117 | threadDictionary[kLastExceptionKey] = nil; 118 | catchBlock(e); 119 | } 120 | } 121 | 122 | void _throw(NSException *e) { 123 | if ([[NSThread currentThread].threadDictionary[kCatchLevels] intValue] > 0) 124 | @throw e; 125 | else { 126 | fputs([[NSString stringWithFormat:@"SwiftRuby: Uncaught Exception: name: %@, reason: %@, userInfo: %@\n", 127 | e.name, e.reason, e.userInfo] UTF8String], stderr); 128 | abort(); 129 | } 130 | } 131 | 132 | void execArgv(NSString *executable, NSArray *arguments) { 133 | const char **argv = calloc(arguments.count+1, sizeof *argv); 134 | for (NSUInteger i=0 ; i *arguments) { 141 | const char **argv = calloc(arguments.count+1, sizeof *argv); 142 | for (NSUInteger i=0 ; i