-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// SExpPrinter.swift | ||
// OpenSwiftUICore | ||
// | ||
// Audited for RELEASE_2024 | ||
// Status: Complete | ||
|
||
package struct SExpPrinter { | ||
var output: String | ||
var depth: Int | ||
package internal(set) var indent: String | ||
|
||
package init(tag: String, singleLine: Bool = false) { | ||
output = "(\(tag)" | ||
depth = singleLine ? 0 : 1 | ||
indent = singleLine ? "" : " " | ||
} | ||
|
||
package mutating func end() -> String { | ||
if depth == 0 { | ||
output.append(")") | ||
return output | ||
} else { | ||
depth -= 1 | ||
indent.removeLast(2) | ||
output.append(")") | ||
return output | ||
} | ||
} | ||
|
||
package mutating func print(_ string: String, newline: Bool = true) { | ||
if newline, depth != 0 { | ||
output.append("\n\(indent)") | ||
} else { | ||
output.append(" ") | ||
} | ||
output.append(string) | ||
} | ||
|
||
package mutating func newline() { | ||
guard depth != 0 else { | ||
return | ||
} | ||
output.append("\n") | ||
output.append(indent) | ||
} | ||
|
||
package mutating func push(_ tag: String) { | ||
if depth == 0 { | ||
output.append("(\(tag)") | ||
} else { | ||
output.append("\n\(indent)(\(tag)") | ||
depth += 1 | ||
indent.append(" ") | ||
} | ||
} | ||
|
||
package mutating func pop() { | ||
if depth == 0 { | ||
output.append(")") | ||
} else { | ||
depth -= 1 | ||
indent.removeLast(2) | ||
output.append(")") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// | ||
// SExpPrinterTests.swift | ||
// OpenSwiftUICoreTests | ||
|
||
import OpenSwiftUICore | ||
import OpenGraphShims | ||
import Testing | ||
|
||
struct SExpPrinterTests { | ||
@Test | ||
func singleLine() { | ||
var printer = SExpPrinter(tag: "(test", singleLine: true) | ||
printer.push("A") | ||
printer.push("B") | ||
printer.pop() | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == "((test(A(B)))") | ||
} | ||
|
||
@Test | ||
func multiLine() { | ||
var printer = SExpPrinter(tag: "(test", singleLine: false) | ||
printer.push("A") | ||
printer.push("B") | ||
printer.pop() | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test | ||
(A | ||
(B))) | ||
""") | ||
} | ||
|
||
@Test | ||
func newline() { | ||
var printer = SExpPrinter(tag: "(test", singleLine: false) | ||
printer.push("A") | ||
printer.newline() | ||
printer.push("B") | ||
printer.newline() | ||
printer.pop() | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test | ||
(A | ||
(B | ||
))) | ||
""") | ||
printer.newline() | ||
let newEnd = printer.end() | ||
#expect(newEnd == """ | ||
((test | ||
(A | ||
(B | ||
)))) | ||
""") | ||
} | ||
|
||
@Test | ||
func printString() { | ||
do { | ||
var printer = SExpPrinter(tag: "(test", singleLine: false) | ||
printer.push("A") | ||
printer.print("B") | ||
printer.print("C") | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test | ||
(A | ||
B | ||
C)) | ||
""") | ||
} | ||
do { | ||
var printer = SExpPrinter(tag: "(test", singleLine: false) | ||
printer.push("A") | ||
printer.print("B") | ||
printer.pop() | ||
_ = printer.end() | ||
printer.print("C") | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test | ||
(A | ||
B)) C) | ||
""") | ||
} | ||
do { | ||
var printer = SExpPrinter(tag: "(test", singleLine: true) | ||
printer.push("A") | ||
printer.print("B") | ||
printer.print("C") | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test(A B C)) | ||
""") | ||
} | ||
do { | ||
var printer = SExpPrinter(tag: "(test", singleLine: true) | ||
printer.push("A") | ||
printer.print("B") | ||
printer.pop() | ||
printer.print("C") | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test(A B) C) | ||
""") | ||
} | ||
} | ||
|
||
@Test | ||
func pushPop() { | ||
var printer = SExpPrinter(tag: "(test", singleLine: false) | ||
printer.push("A") | ||
printer.push("B") | ||
printer.pop() | ||
printer.push("C") | ||
printer.pop() | ||
printer.pop() | ||
let end = printer.end() | ||
#expect(end == """ | ||
((test | ||
(A | ||
(B) | ||
(C))) | ||
""") | ||
} | ||
} |