@@ -38,12 +38,20 @@ internal extension String {
3838
3939 case . escapeScalars( _, let octothorpLevel? , _, true ) :
4040 let octothorps = String ( repeating: " # " , count: Int ( octothorpLevel) )
41- return ( octothorpLevel, octothorps + #"""# , #"""# + octothorps)
41+ //#if swift(>=5.4)
42+ // return (octothorpLevel, octothorps + #"""#, #"""# + octothorps)
43+ //#else
44+ return ( octothorpLevel, octothorps + " \" " , " \" " + octothorps)
45+ //#endif
4246
4347 case . escapeScalars( _, nil , _, let showQuotes) :
4448 /* We must determine the octothorp level. */
4549 var level = UInt ( 0 )
46- var ( sepOpen, sepClose) = ( #"""# , #"""# )
50+ //#if swift(>=5.4)
51+ // var (sepOpen, sepClose) = (#"""#, #"""#)
52+ //#else
53+ var ( sepOpen, sepClose) = ( " \" " , " \" " )
54+ //#endif
4755 while str. contains ( sepClose) {
4856 level += 1
4957 sepOpen = " # " + sepOpen
@@ -65,7 +73,7 @@ internal extension String {
6573
6674 var hasProcessedNewLines = false
6775 var specialCharState : ( UnicodeScalar , Int ) ? = nil /* First element is the special char, the other is the number of octothorps found. */
68- let ascii = unicodeScalars . lazy . map { scalar in
76+ let ascii = unicodeScalars . lazy . map { scalar -> String in
6977 /* Let’s build the previous escape if needed. */
7078 let prefix : String
7179 if scalar == " # " {
@@ -75,7 +83,11 @@ internal extension String {
7583 if curSpecial. 1 == octothorpLevel - 1 {
7684 /* We have now reached the number of octothorp needed to build an actual “special char” (closing quote, backslash, etc.); we must escape it. */
7785 specialCharState = nil
78- return #"\"# + octothorps + String( curSpecial. 0 ) + octothorps
86+ //#if swift(>=5.4)
87+ // return #"\"# + octothorps + String(curSpecial.0) + octothorps
88+ //#else
89+ return " \\ " + octothorps + String( curSpecial. 0 ) + octothorps
90+ //#endif
7991 }
8092 specialCharState = ( curSpecial. 0 , curSpecial. 1 + 1 )
8193 return " "
@@ -87,7 +99,8 @@ internal extension String {
8799 prefix = " "
88100 }
89101
90- if Self . newLines. contains ( scalar) {
102+ let isNewLine = Self . newLines. contains ( scalar)
103+ if isNewLine {
91104 hasProcessedNewLines = true
92105 switch newLineProcessing {
93106 case . none: return prefix + String ( scalar)
@@ -111,14 +124,24 @@ internal extension String {
111124 specialCharState = ( scalar, 0 )
112125 return prefix + " "
113126 }
114- let escaped = scalar. escaped ( asASCII: asASCII)
115- return prefix + ( octothorpLevel == 0 ? escaped : escaped. replacingOccurrences ( of: #"\"# , with: #"\"# + octothorps, options: . literal) )
127+ let escaped = scalar. escaped ( asASCII: asASCII || isNewLine)
128+ //#if swift(>=5.4)
129+ // return prefix + (octothorpLevel == 0 ? escaped : escaped.replacingOccurrences(of: #"\"#, with: #"\"# + octothorps, options: .literal))
130+ //#else
131+ return prefix + ( octothorpLevel == 0 ? escaped : escaped. replacingOccurrences ( of: " \\ " , with: " \\ " + octothorps, options: . literal) )
132+ //#endif
116133 }
117134 }
118- return ( sepOpen + ascii. joined ( separator: " " ) + ( specialCharState. flatMap { String ( $0. 0 ) + String( repeating: " # " , count: $0. 1 ) } ?? " " ) + sepClose, hasProcessedNewLines)
135+ let asciiJoined = ascii. joined ( separator: " " )
136+ let specialCharStateMapped = ( specialCharState. flatMap { String ( $0. 0 ) + String( repeating: " # " , count: $0. 1 ) } ?? " " )
137+ return ( sepOpen + asciiJoined + specialCharStateMapped + sepClose, hasProcessedNewLines)
119138 }
120139
121140 private static let newLines = CharacterSet . newlines
122- private static let specialChars = CharacterSet ( charactersIn: #""\"# )
123-
141+ //#if swift(>=5.4)
142+ // private static let specialChars = CharacterSet(charactersIn: #""\"#)
143+ //#else
144+ private static let specialChars = CharacterSet ( charactersIn: " \" \\ " )
145+ //#endif
146+
124147}
0 commit comments