diff --git a/SwiftShell/Stream.swift b/SwiftShell/Stream.swift index 3c6a034..395eed5 100644 --- a/SwiftShell/Stream.swift +++ b/SwiftShell/Stream.swift @@ -8,23 +8,40 @@ import Foundation +// TODO: get encoding from environmental variable LC_TYPE public var streamencoding = NSUTF8StringEncoding +/** A stream of text. Does as much as possible lazily. */ public protocol ReadableStreamType : Streamable { + /** + Whatever amount of text the stream feels like providing. + If the source is a file this will read everything at once. + + :returns: more text from the stream, or nil if we have reached the end. + */ func readSome() -> String? + + /// Reads everything at once. func read() -> String - func lines() -> SequenceOf + + /// Lazily splits the stream into lines + func lines() -> SequenceOf + + /// Allows stream to be used by "println" func writeTo(inout target: Target) } +/** An output stream, like standard output and standard error. */ public protocol WriteableStreamType : OutputStreamType { func write(string: String) + + /// Must be called on local streams when done writing. func closeStream() } - +/** Creates a stream from a String. */ public func stream(text: String) -> ReadableStreamType { let pipe = NSPipe() let input = pipe.fileHandleForWriting @@ -79,7 +96,7 @@ struct StringStreamGenerator : GeneratorType { return nextline } else { if let newcache = stream.readSome() { - cache = nextline + newcache // TODO: crashes on long streams + cache = nextline + newcache return next() } else { return nextline == "" ? nil : nextline diff --git a/SwiftShell_Speed_Tests/Stream_Iteration_SpeedTests.swift b/SwiftShell_Speed_Tests/Stream_Iteration_SpeedTests.swift index 035342f..d6caf08 100644 --- a/SwiftShell_Speed_Tests/Stream_Iteration_SpeedTests.swift +++ b/SwiftShell_Speed_Tests/Stream_Iteration_SpeedTests.swift @@ -109,7 +109,7 @@ class Stream_Iteration_SpeedTests: XCTestCase { func allSpeedIterateOverFile() -> Array{ let longtextpath = pathForTestResource("long text", type: "txt") - var times = Array(count: numberoflines, repeatedValue: 0) + var times = Array(count: numberoflines, repeatedValue: 0) let f = open(longtextpath) let start = mach_absolute_time() diff --git a/swiftshell.bash b/swiftshell.bash index 4483270..67f74ff 100755 --- a/swiftshell.bash +++ b/swiftshell.bash @@ -1,10 +1,10 @@ #!/bin/bash -# swiftshell.bash -# SwiftShell +# swiftshell.bash +# SwiftShell # -# Created by Kåre Morstøl on 24/08/14. -# Copyright (c) 2014 NotTooBad Software. All rights reserved. +# Created by Kåre Morstøl on 24/08/14. +# Copyright (c) 2014 NotTooBad Software. All rights reserved. # The folders where Swift can find the frameworks it imports as modules. # SwiftShell.framework must be in one of these. @@ -13,7 +13,7 @@ FRAMEWORKS_PATH=$DYLD_FRAMEWORK_PATH:~/Library/Frameworks:/Library/Frameworks # Add a : to the beginning if not already there if [[ $FRAMEWORKS_PATH != :* ]] then - FRAMEWORKS_PATH=:$FRAMEWORKS_PATH + FRAMEWORKS_PATH=:$FRAMEWORKS_PATH fi # Swift needs folders one at a time, so we replace : with " -F "