Skip to content

Commit c0f16f6

Browse files
committed
Multiline candidates possible now
1 parent 3ced429 commit c0f16f6

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

Common/Tracery.Text.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ import Foundation
2020
// candidate 2
2121
//
2222

23+
extension String {
24+
func trimTrailingSpaces() -> String {
25+
var t = self
26+
while t.hasSuffix(" ") {
27+
t = "" + t.dropLast()
28+
}
29+
return t
30+
}
31+
32+
func trimLeadingSpaces() -> String {
33+
var t = self
34+
while t.hasPrefix(" ") {
35+
t = "" + t.dropFirst()
36+
}
37+
return t
38+
}
39+
40+
func trimSpaces() -> String {
41+
return self.trimLeadingSpaces().trimTrailingSpaces()
42+
}
43+
44+
mutating func trimEnd() {
45+
self = self.trimTrailingSpaces()
46+
}
47+
mutating func trimStart() {
48+
self = self.trimLeadingSpaces()
49+
}
50+
mutating func trim() {
51+
self = self.trimLeadingSpaces().trimTrailingSpaces()
52+
}
53+
54+
}
55+
2356
extension Tracery {
2457

2558
convenience public init(path: String) {
@@ -50,7 +83,23 @@ struct TextParser {
5083
var ruleSet = [String: [String] ]()
5184
var rule = ""
5285

53-
for line in lines {
86+
var i = 0
87+
var concatlines: [String] = [String]()
88+
89+
// first pass is just concatinating lines that start with '\'
90+
// it also removes all whitespace at the start and the end of the line and then adds one at the start.
91+
for line in lines {
92+
if line.hasPrefix("\\") {
93+
if i>0 {
94+
concatlines[i-1] += " " + String(line[line.index(line.startIndex, offsetBy: 1)...]).trimSpaces()
95+
}
96+
} else {
97+
concatlines.append(line.trimSpaces())
98+
i += 1
99+
}
100+
}
101+
102+
for line in concatlines {
54103
if line.hasPrefix("["), line.hasSuffix("]") {
55104
let start = line.index(after: line.startIndex)
56105
let end = line.index(before: line.endIndex)

0 commit comments

Comments
 (0)