@@ -94,88 +94,28 @@ function multiline(str::AbstractString, indicators::AbstractString)
94
94
end
95
95
96
96
function multiline (str:: AbstractString , style:: Symbol , chomp:: Symbol )
97
- # Append an additional, non-space, character to force one more iteration of the style
98
- # loop
99
- str *= ETX
100
-
101
- out = IOBuffer ()
102
- num_newlines = 0 # The number of newlines at the end of the string
103
- prev = curr = ' \0 '
104
-
105
- # Replace newlines with spaces (folded)
106
97
if style === :folded
107
- # The code below is equivalent to these two regexes (the non-regex code has much
108
- # better performance):
109
- # ```
110
- # str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
111
- # str = replace(str, r"(?<=\n)\n(?=\S)" => "")
112
- # ```
113
-
114
- for next in str
115
- if curr == ' \n '
116
- if ! isspace (next) && next != ETX
117
- if prev == ' \n '
118
- # Skip last newline in a sequence of sequential blank lines
119
- elseif ! isspace (prev)
120
- # Replace a single newline with a space
121
- write (out, ' ' )
122
- else
123
- num_newlines += 1
124
- end
125
- else
126
- num_newlines += 1
127
- end
128
- elseif curr != ' \0 '
129
- # Insert newlines which were determined to not be at the end of the string
130
- if num_newlines > 0
131
- write (out, " \n " ^ num_newlines)
132
- num_newlines = 0
133
- end
134
-
135
- write (out, curr)
136
- end
137
-
138
- prev = curr
139
- curr = next
140
- end
141
-
142
- # Keep newlines (literal)
98
+ str = replace (str, r" (?<=\S )\n (?=\S )" => " " )
99
+ str = replace (str, r" (?<=\n )\n (?=\S )" => " " )
143
100
elseif style === :literal
144
- for next in str
145
- if curr == ' \n '
146
- num_newlines += 1
147
- elseif curr != ' \0 '
148
- # Insert newlines which were determined to not be at the end of the string
149
- if num_newlines > 0
150
- write (out, " \n " ^ num_newlines)
151
- num_newlines = 0
152
- end
153
-
154
- write (out, curr)
155
- end
156
-
157
- curr = next
158
- end
101
+ # no-op
159
102
else
160
103
throw (ArgumentError (" Unknown style indicator: $style " ))
161
104
end
162
105
163
- # Single newline at end (clip)
164
106
if chomp === :clip
165
- num_newlines > 0 && write (out , ' \n ' )
166
-
167
- # No newline at end (strip)
107
+ if endswith (str , ' \n ' )
108
+ str = rstrip (str, ' \n ' ) * ' \n '
109
+ end
168
110
elseif chomp === :strip
169
- # no-op
170
-
171
- # All newlines from end (keep)
111
+ str = rstrip (str, ' \n ' )
172
112
elseif chomp === :keep
173
- write (out, " \n " ^ num_newlines)
113
+ # no-op
174
114
else
175
115
throw (ArgumentError (" Unknown chomping indicator: $chomp " ))
176
116
end
177
117
178
- return String ( take! (out))
118
+ return str
179
119
end
180
120
181
121
"""
0 commit comments