Skip to content

Commit 96acd70

Browse files
Indentation string calculations
1 parent fe34b7f commit 96acd70

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

Sources/TextFormation/Indentation.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,27 @@ public enum IndentationError: Error {
3030

3131
extension IndentationError: Hashable {}
3232
extension IndentationError: Sendable {}
33+
34+
extension Indentation {
35+
/// Apply an indentation operation to a whitespace string.
36+
public func apply(to string: String, indentationUnit: String, width: Int) -> String {
37+
// here, we have to determine how many units of indentation currently exist
38+
let spaceOnlyReference = string.replacingOccurrences(of: "\t", with: String(repeating: " ", count: width))
39+
let spaceCount = spaceOnlyReference.utf8.count
40+
let referenceCount = spaceCount / width
41+
let remainder = spaceCount % width
42+
43+
switch self {
44+
case .equal:
45+
return String(repeating: indentationUnit, count: referenceCount) + String(repeating: " ", count: remainder)
46+
case .relativeIncrease:
47+
return String(repeating: indentationUnit, count: referenceCount + 1) + String(repeating: " ", count: remainder)
48+
case .relativeDecrease:
49+
if referenceCount == 0 {
50+
return string
51+
}
52+
53+
return String(repeating: indentationUnit, count: referenceCount - 1) + String(repeating: " ", count: remainder)
54+
}
55+
}
56+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import Foundation
2+
import Testing
3+
4+
import TextFormation
5+
6+
struct IndentationTests {
7+
@Test func equalEmpty() throws {
8+
let equal = Indentation.equal(NSRange.zero)
9+
10+
#expect(equal.apply(to: "", indentationUnit: "\t", width: 4) == "")
11+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == "")
12+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == "")
13+
}
14+
15+
@Test func increaseEmpty() throws {
16+
let equal = Indentation.relativeIncrease(NSRange.zero)
17+
18+
#expect(equal.apply(to: "", indentationUnit: "\t", width: 4) == "\t")
19+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == " ")
20+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == " ")
21+
}
22+
23+
@Test func decreaseEmpty() throws {
24+
let equal = Indentation.relativeDecrease(NSRange.zero)
25+
26+
#expect(equal.apply(to: "", indentationUnit: "\t", width: 4) == "")
27+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == "")
28+
#expect(equal.apply(to: "", indentationUnit: " ", width: 4) == "")
29+
}
30+
31+
@Test func equalTab() throws {
32+
let equal = Indentation.equal(NSRange.zero)
33+
34+
#expect(equal.apply(to: "\t", indentationUnit: "\t", width: 4) == "\t")
35+
#expect(equal.apply(to: "\t", indentationUnit: " ", width: 4) == " ")
36+
#expect(equal.apply(to: "\t", indentationUnit: " ", width: 4) == " ")
37+
}
38+
39+
@Test func equalFourSpace() throws {
40+
let equal = Indentation.equal(NSRange.zero)
41+
42+
#expect(equal.apply(to: " ", indentationUnit: "\t", width: 4) == "\t")
43+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 4) == " ")
44+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 4) == " ")
45+
}
46+
47+
@Test func decreaseFourSpace() throws {
48+
let equal = Indentation.relativeDecrease(NSRange.zero)
49+
50+
#expect(equal.apply(to: " ", indentationUnit: "\t", width: 4) == "")
51+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 2) == " ")
52+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 4) == "")
53+
}
54+
55+
@Test func increaseFourSpace() throws {
56+
let equal = Indentation.relativeIncrease(NSRange.zero)
57+
58+
#expect(equal.apply(to: " ", indentationUnit: "\t", width: 4) == "\t\t")
59+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 2) == " ")
60+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 4) == " ")
61+
}
62+
63+
@Test func increasePreservingAlignment() throws {
64+
let equal = Indentation.relativeIncrease(NSRange.zero)
65+
66+
#expect(equal.apply(to: "\t ", indentationUnit: "\t", width: 4) == "\t\t ")
67+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 2) == " ")
68+
#expect(equal.apply(to: " ", indentationUnit: " ", width: 4) == " ")
69+
}
70+
}

0 commit comments

Comments
 (0)