Commit 052290cf authored by Drew's avatar Drew Committed by GitHub

Merge pull request #15 from AnarchyTools/swift-preview-6

Swift preview 6
parents f1771b5b 0f49f75f
Pipeline #2483 failed with stage
in 2 minutes and 21 seconds
FROM drewcrawford/buildbase:latest
RUN apt-get update
RUN apt-get install atbuild package-deb -y
ADD . /atpkg
WORKDIR /atpkg
RUN atbuild check
\ No newline at end of file
......@@ -14,7 +14,7 @@
/// Time interval
public struct TimeInterval {
private let interval: Int
fileprivate let interval: Int
/// Initialize on second scale
///
......
......@@ -132,10 +132,17 @@ public class File {
/// - Parameter binary: optional, open in binary mode, defaults to text mode
public convenience init(tempFileAtPath path: Path, prefix: String, binary: Bool = false) throws {
let p = path.appending(prefix + ".XXXXXXX")
var buf = Array(p.description.utf8)
buf.append(0)
let fd = mkstemp(UnsafeMutablePointer(buf))
if let filename = String(validatingUTF8: UnsafeMutablePointer(buf)) {
var buf = p.description.utf8CString
let fd = buf.withUnsafeMutableBufferPointer {
mkstemp($0.baseAddress!)
}
let filename_ = buf.withUnsafeBufferPointer { (ptr) -> String? in
if let o = ptr.baseAddress {
return String(cString: o)
}
return nil
}
if let filename = filename_ {
try self.init(fd: fd, mode: .ReadAndWrite, binary: binary, takeOwnership: true)
self.path = Path(filename)
} else {
......@@ -166,7 +173,9 @@ public class File {
public func read(size: Int) throws -> String? {
var buffer:[UInt8] = try self.read(size: size)
buffer.append(0)
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
String(cString: $0.baseAddress!)
}
}
/// Read bytes from file
......@@ -175,7 +184,7 @@ public class File {
/// - Returns: UInt8 array with bytes
public func read(size: Int) throws -> [UInt8] {
var buffer = [UInt8](repeating: 0, count: size)
let read = fread(UnsafeMutablePointer(buffer), 1, size, self.fp)
let read = fread(UnsafeMutablePointer(mutating: buffer), 1, size, self.fp)
if read == 0 {
if feof(self.fp) == 0 {
throw SysError(errno: errno)
......@@ -195,7 +204,9 @@ public class File {
public func readAll() throws -> String? {
var buffer:[UInt8] = try self.readAll()
buffer.append(0)
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
String(cString: $0.baseAddress!)
}
}
/// Read complete file
......@@ -217,8 +228,11 @@ public class File {
///
/// - Returns: String read from file (newline included) if valid UTF-8 or nil
public func readLine() throws -> String? {
let buffer = [UInt8](repeating: 0, count: 64 * 1024 + 1)
let read = fgets(UnsafeMutablePointer(buffer), 64 * 1024, self.fp)
var buffer = [UInt8](repeating: 0, count: 64 * 1024 + 1)
let read = buffer.withUnsafeMutableBufferPointer() { (ptr_) -> UnsafeMutablePointer<Int8>? in
let ptr = UnsafeMutableRawPointer(ptr_.baseAddress)!
return fgets(ptr.assumingMemoryBound(to: Int8.self), 64 * 1024, self.fp)
}
if read == nil {
if feof(self.fp) == 0 {
throw SysError(errno: errno)
......@@ -226,7 +240,9 @@ public class File {
throw SysError.EndOfFile
}
}
return String(validatingUTF8: UnsafePointer<CChar>(buffer))
return buffer.withUnsafeBufferPointer {
return String(cString: $0.baseAddress!)
}
}
/// Write a string to the file
......
......@@ -101,7 +101,7 @@ public enum FileType: Mode_t {
case Socket = 12
case Whiteout = 14
private init?(statMode: Mode_t) {
fileprivate init?(statMode: Mode_t) {
self.init(rawValue: (statMode & 0xf000) >> 12)
}
}
......
......@@ -409,7 +409,7 @@ public class FS {
let result = rename(from.description, to.description)
if result < 0 {
let error = SysError(errno: errno, to, from)
if case .CrossDeviceLink = error where !atomic {
if case .CrossDeviceLink = error, !atomic {
try FS.copyItem(from: from, to: to, recursive: true)
try FS.removeItem(path: from, recursive: true)
} else {
......@@ -445,12 +445,20 @@ public class FS {
/// - Returns: path to the already created directory
public class func temporaryDirectory(prefix: String = "tempdir") throws -> Path {
let p = Path.tempDirectory().appending(prefix + ".XXXXXXX")
var buf = Array(p.description.utf8)
buf.append(0)
if mkdtemp(UnsafeMutablePointer(buf)) == nil {
var buf = p.description.utf8CString
let result = buf.withUnsafeMutableBufferPointer {
mkdtemp($0.baseAddress!)
}
if result == nil {
throw SysError(errno: errno, p)
}
if let dirname = String(validatingUTF8: UnsafeMutablePointer(buf)) {
let dirname_ = buf.withUnsafeBufferPointer { (ptr) -> String? in
if let o = ptr.baseAddress {
return String(cString: o)
}
return nil
}
if let dirname = dirname_ {
return Path(dirname)
} else {
throw SysError.UnknownError
......
......@@ -105,15 +105,15 @@ public class StdErrLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
self.colorize(stderr, level)
if let date = date where self.logDate {
if let date = date, self.logDate {
self._fwrite(stderr, "\(date.isoDateString!) ")
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
self._fwrite(stderr, "\(file):\(line): ")
}
for item in msg {
self._fwrite(stderr, String(item) + " ")
self._fwrite(stderr, String(describing: item) + " ")
}
self.colorize(stderr, nil)
self._fwrite(stderr, "\n")
......@@ -132,15 +132,15 @@ public class StdOutLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
self.colorize(stdout, level)
if let date = date where self.logDate {
if let date = date, self.logDate {
self._fwrite(stdout, "\(date.isoDateString!) ")
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
self._fwrite(stdout, "\(file):\(line): ")
}
for item in msg {
self._fwrite(stdout, String(item) + " ")
self._fwrite(stdout, String(describing: item) + " ")
}
self.colorize(stdout, nil)
self._fwrite(stdout, "\n")
......@@ -197,16 +197,16 @@ public class FileLogger: LoggerProcotol {
public func log(_ level: Log.LogLevel, _ msg: [Any], date: Date?, file: String?, line: Int?, function: String?) {
if let logFile = self.logFile {
do {
if let date = date where self.logDate {
if let date = date, self.logDate {
try date.isoDateString!.write(to: logFile)
}
if let file = file, line = line where self.logFileAndLine || level == .Fatal {
if let file = file, let line = line, self.logFileAndLine || level == .Fatal {
try " [\(level)] \(file):\(line): ".write(to: logFile)
} else {
try " [\(level)]: ".write(to: logFile)
}
for item in msg {
try (String(item) + " ").write(to: logFile)
try (String(describing: item) + " ").write(to: logFile)
}
try "\n".write(to: logFile)
logFile.flush()
......
......@@ -18,7 +18,7 @@ public extension String {
///
/// - Parameter path: a path
public init(path: Path) {
self.init(path.description)
self.init(path.description)!
}
/// Load a string from a file
......
......@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
public enum SysError: ErrorProtocol {
public enum SysError: Error {
// Generic
case OperationNotPermitted
case NoSuchEntity(Path?)
......
......@@ -21,10 +21,10 @@
// So apparently this is how you access a fixed length C array from
// an imported struct -.-
public func fixedCArrayToArray<T, E>(data: inout T) -> [E] {
return withUnsafePointer(&data) { ptr -> [E] in
return withUnsafePointer(to: &data) { ptr -> [E] in
let buffer = UnsafeBufferPointer(
start: unsafeBitCast(ptr, to: UnsafePointer<E>.self),
count: sizeofValue(data)
count: MemoryLayout<T>.size
)
return [E](buffer)
}
......
......@@ -83,7 +83,7 @@ public struct URL {
case .Schema(let data):
var schema: String? = nil
(newState, schema) = self._parseSchema(character, data)
if let schema = schema where schema.characters.count > 0 {
if let schema = schema, schema.characters.count > 0 {
self.schema = schema
}
case .AfterSchema:
......
......@@ -69,9 +69,9 @@ class DateTests: XCTestCase {
}
func testAddingDates() {
let date1 = Date(timestamp: 86400)
let date2 = Date(timestamp: 86400)
XCTAssert(date1 + date2 == Date(timestamp: 86400 * 2))
let date1 = atfoundation.Date(timestamp: 86400)
let date2 = atfoundation.Date(timestamp: 86400)
XCTAssert(date1 + date2 == atfoundation.Date(timestamp: 86400 * 2))
XCTAssert(date1 - date2 == Date(timestamp: 0))
}
......@@ -84,7 +84,7 @@ class DateTests: XCTestCase {
}
extension DateTests {
static var allTests : [(String, DateTests -> () throws -> Void)] {
static var allTests : [(String, (DateTests) -> () throws -> Void)] {
return [
("testISODateParsing", testISODateParsing),
("testTimeStampToISODate", testTimeStampToISODate),
......
......@@ -217,7 +217,7 @@ class FSTests: XCTestCase {
}
extension FSTests {
static var allTests : [(String, FSTests -> () throws -> Void)] {
static var allTests : [(String, (FSTests) -> () throws -> Void)] {
return [
("testFileExists", testFileExists),
("testFileDoesNotExist", testFileDoesNotExist),
......
......@@ -210,7 +210,7 @@ class PathTests: XCTestCase {
}
extension PathTests {
static var allTests : [(String, PathTests -> () throws -> Void)] {
static var allTests : [(String, (PathTests) -> () throws -> Void)] {
return [
("testInitNoDelimiter", testInitNoDelimiter),
("testInitWithDelimiter", testInitWithDelimiter),
......
......@@ -68,7 +68,7 @@ class LoggerTests: XCTestCase {
}
extension LoggerTests {
static var allTests : [(String, LoggerTests -> () throws -> Void)] {
static var allTests : [(String, (LoggerTests) -> () throws -> Void)] {
return [
("testLogStdErr", testLogStdErr),
("testLogStdOut", testLogStdOut),
......
......@@ -91,7 +91,7 @@ class ReplaceTests: XCTestCase {
}
extension ReplaceTests {
static var allTests : [(String, ReplaceTests -> () throws -> Void)] {
static var allTests : [(String, (ReplaceTests) -> () throws -> Void)] {
return [
("testNewStringSubstring", testNewStringSubstring),
("testNewStringSubstringWithEmpty", testNewStringSubstringWithEmpty),
......
......@@ -240,7 +240,7 @@ class SearchTests: XCTestCase {
}
extension SearchTests {
static var allTests : [(String, SearchTests -> () throws -> Void)] {
static var allTests : [(String, (SearchTests) -> () throws -> Void)] {
return [
("testPositionOfChar", testPositionOfChar),
("testPositionOfCharNotFound", testPositionOfCharNotFound),
......
......@@ -128,7 +128,7 @@ class SplitTests: XCTestCase {
}
extension SplitTests {
static var allTests : [(String, SplitTests -> () throws -> Void)] {
static var allTests : [(String, (SplitTests) -> () throws -> Void)] {
return [
("testJoinWithString", testJoinWithString),
("testJoinWithStringAndEmptyArray", testJoinWithStringAndEmptyArray),
......
......@@ -44,7 +44,7 @@ class SubstringTests: XCTestCase {
}
extension SubstringTests {
static var allTests : [(String, SubstringTests -> () throws -> Void)] {
static var allTests : [(String, (SubstringTests) -> () throws -> Void)] {
return [
("testSubstring", testSubstring),
("testSubstringFullRange", testSubstringFullRange),
......
......@@ -40,7 +40,7 @@ class WhitespaceTests: XCTestCase {
}
extension WhitespaceTests {
static var allTests : [(String, WhitespaceTests -> () throws -> Void)] {
static var allTests : [(String, (WhitespaceTests) -> () throws -> Void)] {
return [
("testWhitespaceAtBeginning", testWhitespaceAtBeginning),
("testWhitespaceAtEnd", testWhitespaceAtEnd),
......
......@@ -54,7 +54,7 @@ class URLTests: XCTestCase {
}
extension URLTests {
static var allTests : [(String, URLTests -> () throws -> Void)] {
static var allTests : [(String, (URLTests) -> () throws -> Void)] {
return [
("testURLEncode", testURLEncode),
("testURLDecode", testURLDecode),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment