Skip to content
otto-schnurr edited this page Oct 2, 2014 · 19 revisions

This interface is borrowed from python.


Types

LineOfText

Declaration

typealias LineOfText = String

Discussion

A collection of characters typically ending with a newline. The newline delimiter is included in the collection (but is easily removed). The last line of a file might not contain a newline.

A FileInput is a sequence of LineOfText objects.

Creating an Input Sequence

input()

Parses input from the command line.

Declaration

func input() -> FileInput

Discussion

Returns a FileInput sequence that iterates over lines of all file paths listed in command line arguments. If that list is empty then standard input is used.

let lineCount = countElements(input())
println("\(lineCount) lines")

A file path of "-" is replaced with standard input.

FileInput()

Constructs a sequence that iterates lines of standard input.

Declaration

init()

Discussion

Creating FileInput using this constructor:

let standardInput = FileInput()

is equivalent to using a "-" file path:

let equivalentInput = FileInput(filePath: "-")

FileInput(filePath:)

Constructs a sequence that iterates lines of a file.

Declaration

init(filePath: String)

Discussion

The resulting FileInput sequence retrieves one line of data at a time:

for line in FileInput(filePath: "data.txt") {
    processNextLineOfData(line)
}

A file path of "-" is replaced with standard input.

FileInput(filePaths:)

Constructs a sequence that iterates lines over a collection of files.

Declaration

init(filePaths: [String])

Discussion

The resulting FileInput sequence retrieves lines from "a.txt then "b.txt":

let files = ["a.txt", "b.txt"]
for line in FileInput(filePaths: files) {
    processNextLineOfData(line)
}

Each file path of "-" is replaced with standard input.

Querying an Input Sequence

var filePath { get }

An optional string indicating where the previous line of text came from.

Declaration

var filePath: String? { get }

Discussion

Use this property to query what file path was used in the last call to nextLine(). The value will be nil when the sequence has been exhausted.

let lines = input()
for line in lines {
    // Newline delimiters are not removed from line.
    print("\(lines.filePath!): \(line)")
}

nextLine() -> LineOfText?

Returns the next line of text in the sequence.

Declaration

func nextLine() -> LineOfText?

Discussion

Use this method to retrieve the next line of input. The value will be nil when the sequence has been exhausted.

if let firstLine = input().nextLine() {
    processFirstLine(firstLine)
}

This is the method used by for-in loops.

Formatting Text

Declaration

extension String {
    func removeLeadingSpace() -> String
    func removeTrailingSpace() -> String
    func findFirstSpace() -> String.Index?
}

removeLeadingSpace()

Returns a copy of a string with no white space at the beginning.

Discussion

Use this method to find where content starts in a string.

let unindentedText = text.removeLeadingSpace()

removeTrailingSpace()

Returns a copy of a string with no white space at the end.

Discussion

Use this method to strip newlines from a FileInput sequence.

for line in input() {
    process(line.removeTrailingSpace())
}

findFirstSpace()

Returns an index of the first white space character in a string.

Discussion

Use this method to find word boundaries.

var firstWord = text.removeLeadingSpace()

if let wordEnd = firstWord.findFirstSpace() {
    firstWord = firstWord[firstWord.start..<wordEnd]
}