@@ -20,6 +20,39 @@ import Foundation
20
20
// candidate 2
21
21
//
22
22
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
+
23
56
extension Tracery {
24
57
25
58
convenience public init ( path: String ) {
@@ -50,7 +83,23 @@ struct TextParser {
50
83
var ruleSet = [ String: [ String] ] ( )
51
84
var rule = " "
52
85
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 {
54
103
if line. hasPrefix ( " [ " ) , line. hasSuffix ( " ] " ) {
55
104
let start = line. index ( after: line. startIndex)
56
105
let end = line. index ( before: line. endIndex)
0 commit comments