Skip to content

Commit 92e7f39

Browse files
committed
StringTable: Added columnCharCounts property
1 parent f0c5d55 commit 92e7f39

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Sources/TextFileKit/StringTable.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,34 @@ extension StringTable {
5252
}
5353
}
5454
}
55+
56+
extension StringTable {
57+
/// Returns the min and max character count for each column.
58+
/// The `upperBound` can be useful for resizing columns to fit the column's data.
59+
///
60+
/// - Returns: `[Column Index: Min Char Count ... Max Char Count]`
61+
public var columnCharCounts: [Int: ClosedRange<Int>] {
62+
reduce(into: [:]) { dict, rowValues in
63+
// iterate columns for current row
64+
for (columnOffset, rowValue) in rowValues.enumerated() {
65+
let cellCharCount = rowValue.count
66+
let defaultRange = cellCharCount ... cellCharCount
67+
68+
// ensure all columns have a value, even if it's default of 0
69+
if dict[columnOffset] == nil {
70+
dict[columnOffset] = defaultRange
71+
}
72+
73+
// update stored range if cell character count is outside stored range
74+
var range = dict[columnOffset] ?? defaultRange
75+
if range.upperBound < cellCharCount {
76+
range = range.lowerBound ... cellCharCount
77+
}
78+
if range.lowerBound > cellCharCount {
79+
range = cellCharCount ... range.upperBound
80+
}
81+
if dict[columnOffset] != range { dict[columnOffset] = range }
82+
}
83+
}
84+
}
85+
}

Tests/TextFileKitTests/StringTable Tests.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,67 @@ class StringTable_Tests: XCTestCase {
144144
st[safe: 3, 0] = "4A"
145145
XCTAssertEqual(st[safe: 3, 0], nil)
146146
}
147+
148+
func testColumnCharCountsA() {
149+
let table: StringTable = []
150+
let ranges = table.columnCharCounts
151+
XCTAssertEqual(ranges.count, 0)
152+
}
153+
154+
func testColumnCharCountsB() {
155+
let table: StringTable = [[]]
156+
let ranges = table.columnCharCounts
157+
XCTAssertEqual(ranges.count, 0)
158+
}
159+
160+
func testColumnCharCountsC() {
161+
let table: StringTable = [["H1"]]
162+
let ranges = table.columnCharCounts
163+
XCTAssertEqual(ranges.count, 1)
164+
165+
XCTAssertEqual(ranges[0], 2 ... 2)
166+
}
167+
168+
func testColumnCharCountsD() {
169+
let table: StringTable = [["H1", "H2", "H3"]]
170+
let ranges = table.columnCharCounts
171+
XCTAssertEqual(ranges.count, 3)
172+
173+
XCTAssertEqual(ranges[0], 2 ... 2)
174+
XCTAssertEqual(ranges[1], 2 ... 2)
175+
XCTAssertEqual(ranges[2], 2 ... 2)
176+
}
177+
178+
func testColumnCharCountsE() {
179+
let table: StringTable = [["H1", "", "H3_"]]
180+
let ranges = table.columnCharCounts
181+
XCTAssertEqual(ranges.count, 3)
182+
183+
XCTAssertEqual(ranges[0], 2 ... 2)
184+
XCTAssertEqual(ranges[1], 0 ... 0)
185+
XCTAssertEqual(ranges[2], 3 ... 3)
186+
}
187+
188+
func testColumnCharCountsF() {
189+
let table: StringTable = [["H1"], [""], ["R2"]]
190+
let ranges = table.columnCharCounts
191+
XCTAssertEqual(ranges.count, 1)
192+
193+
XCTAssertEqual(ranges[0], 0 ... 2)
194+
}
195+
196+
func testColumnCharCountsG() {
197+
let table: StringTable = [
198+
["H1", "H2", "H3"],
199+
["", "abc", "ab"]
200+
]
201+
let ranges = table.columnCharCounts
202+
XCTAssertEqual(ranges.count, 3)
203+
204+
XCTAssertEqual(ranges[0], 0 ... 2)
205+
XCTAssertEqual(ranges[1], 2 ... 3)
206+
XCTAssertEqual(ranges[2], 2 ... 2)
207+
}
147208
}
148209

149210
#endif

0 commit comments

Comments
 (0)