Skip to content

Commit c386e64

Browse files
committed
Added convenience methods and subscripts to StringTable
1 parent 0e33716 commit c386e64

9 files changed

+302
-76
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# Custom
2+
[Dd]ev/
3+
14
# Xcode
2-
/dev/
35

46
# macOS
57
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// StringArrayTableRepresentable.swift
3+
// TextFileKit
4+
//
5+
// Created by Steffan Andrews on 2021-03-15.
6+
// Copyright © 2021 Steffan Andrews. All rights reserved.
7+
//
8+
9+
/// Protocol describing file formats which can be encoded to/from a `StringTable` (String Array table of rows and columns).
10+
public protocol StringArrayTableRepresentable {
11+
12+
/// Raw data store as an Array table addressed as `self[row][column]`, `self[row, column]` or `self[safe: row, column]`.
13+
var table: StringTable { get set }
14+
15+
/// (Computed property) Raw text content of the file.
16+
var rawText: String { get }
17+
18+
/// Initialize from an Array table addressed as `self[row][column]`, `self[row, column]` or `self[safe: row, column]`.
19+
init(table: StringTable)
20+
21+
/// Initialize from raw text content.
22+
init(rawText: String)
23+
24+
}

Sources/TextFileKit/StringTable.swift

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// StringTable.swift
3+
// TextFileKit
4+
//
5+
// Created by Steffan Andrews on 2021-03-15.
6+
// Copyright © 2021 Steffan Andrews. All rights reserved.
7+
//
8+
9+
/// String table addressed as `self[row][column]`, `self[row, column]` or `self[safe: row, column]`.
10+
public typealias StringTable = [[String]]
11+
12+
extension StringTable {
13+
14+
/// Number of rows in the string table.
15+
public var rowCount: Int {
16+
17+
count
18+
19+
}
20+
21+
/// Number of rows in the columns table.
22+
/// The first row determines number of columns for the entire table.
23+
public var columnCount: Int {
24+
25+
first?.count ?? 0
26+
27+
}
28+
29+
/// Access a cell of the table.
30+
/// Ensure the cell exists before accessing it or an exception will be thrown, the same as standard array subscript behavior.
31+
public subscript(row: Int, col: Int) -> Element.Element {
32+
33+
get {
34+
self[row][col]
35+
}
36+
set {
37+
self[row][col] = newValue
38+
}
39+
40+
}
41+
42+
/// Access a cell of the table.
43+
/// Get: Cells which do not exist will return `nil`.
44+
/// Set: Cells which do not exist will not be set.
45+
public subscript(safe row: Int, col: Int) -> Element.Element? {
46+
47+
get {
48+
self[safe: row]?[safe: col]
49+
}
50+
set {
51+
guard row < self.rowCount,
52+
col < self[row].count
53+
else { return }
54+
55+
if let newValue = newValue {
56+
self[row][col] = newValue
57+
}
58+
}
59+
60+
}
61+
62+
}

Sources/TextFileKit/TextFile CSV.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,37 @@ extension TextFile {
1414

1515
// tested with Google Sheets, Microsoft Excel, and Apple Numbers
1616

17-
/// CUSTOM SHARED:
1817
/// CSV (Comma-Separated Values) text file format.
1918
public struct CSV: StringArrayTableRepresentable {
2019

21-
// consts
20+
// MARK: - Constants
2221

2322
internal static let sepChar: Character = ","
2423
internal static let newLineChar: Character = Character.newLine
2524

2625
public static let fileExtension = "csv"
2726

28-
// vars
27+
// MARK: - Variables
2928

3029
public var table: StringTable = []
3130

32-
// init
31+
// MARK: - Init
3332

3433
public init(table: StringTable = []) {
3534
self.table = table
3635
}
3736

38-
public init(rawData: String) {
39-
table = Self.parseCSV(text: rawData)
37+
public init(rawText: String) {
38+
table = Self.parseCSV(text: rawText)
4039
}
4140

42-
// rawData
41+
// MARK: - rawText
4342

44-
public var rawData: String {
43+
public var rawText: String {
4544

46-
table.map ({ row in
45+
table.map { row in
4746

48-
row.map ({ textString in
47+
row.map { textString in
4948

5049
var outString = textString
5150

@@ -60,9 +59,10 @@ extension TextFile {
6059

6160
return outString
6261

63-
})
62+
}
6463
.joined(separator: Self.sepChar.string)
65-
})
64+
65+
}
6666
.joined(separator: Self.newLineChar.string)
6767

6868
}

Sources/TextFileKit/TextFile TSV.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,37 @@ extension TextFile {
1212

1313
// tested with Google Sheets, Microsoft Excel, and Apple Numbers
1414

15-
/// CUSTOM SHARED:
1615
/// TSV (Tab-Separated Values) text file format.
1716
public struct TSV: StringArrayTableRepresentable {
1817

19-
// consts
18+
// MARK: - Constants
2019

2120
internal static let sepChar: Character = "\t"
2221
internal static let newLineChar: Character = Character.newLine
2322

2423
public static let fileExtension = "tsv"
2524

26-
// vars
25+
// MARK: - Variables
2726

2827
public var table: StringTable = []
2928

30-
// init
29+
// MARK: - Init
3130

3231
public init(table: StringTable = []) {
3332
self.table = table
3433
}
3534

36-
public init(rawData: String) {
37-
table = Self.parseTSV(text: rawData)
35+
public init(rawText: String) {
36+
table = Self.parseTSV(text: rawText)
3837
}
3938

40-
// rawData
39+
// MARK: - RawText
4140

42-
public var rawData: String {
41+
public var rawText: String {
4342

44-
table.map ({ row in
43+
table.map { row in
4544

46-
row.map ({ textString in
45+
row.map { textString in
4746

4847
var outString = textString
4948
var needsQuoteWrapping = false
@@ -67,9 +66,10 @@ extension TextFile {
6766

6867
return outString
6968

70-
})
69+
}
7170
.joined(separator: Self.sepChar.string)
72-
})
71+
72+
}
7373
.joined(separator: Self.newLineChar.string)
7474

7575
}

Sources/TextFileKit/TextFile.swift

-23
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,7 @@
66
// Copyright © 2020 Steffan Andrews. All rights reserved.
77
//
88

9-
/// CUSTOM SHARED:
109
/// Family of Text File parsing and constructing entities.
1110
public enum TextFile {
1211

1312
}
14-
15-
/// CUSTOM SHARED:
16-
/// String table addressed as `self[row][column]`.
17-
public typealias StringTable = [[String]]
18-
19-
/// CUSTOM SHARED:
20-
/// Protocol describing file formats which can be serialized/deserialized to/from a String Array table of rows and columns.
21-
public protocol StringArrayTableRepresentable {
22-
23-
/// Raw data store as an Array table addressed as `self[row][column]`.
24-
var table: StringTable { get set }
25-
26-
/// (Computed property) Raw string content of the file.
27-
var rawData: String { get }
28-
29-
/// Initialize from an Array table addressed as `self[row][column]`.
30-
init(table: StringTable)
31-
32-
/// Initialize from raw file data.
33-
init(rawData: String)
34-
35-
}

0 commit comments

Comments
 (0)