Skip to content

Commit 6071847

Browse files
authored
Merge pull request #2 from invenia/cv/regex
Switch back to regex
2 parents 62e5d52 + 3142199 commit 6071847

File tree

2 files changed

+10
-69
lines changed

2 files changed

+10
-69
lines changed

src/MultilineStrings.jl

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -94,88 +94,28 @@ function multiline(str::AbstractString, indicators::AbstractString)
9494
end
9595

9696
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)
10697
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)" => "")
143100
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
159102
else
160103
throw(ArgumentError("Unknown style indicator: $style"))
161104
end
162105

163-
# Single newline at end (clip)
164106
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
168110
elseif chomp === :strip
169-
# no-op
170-
171-
# All newlines from end (keep)
111+
str = rstrip(str, '\n')
172112
elseif chomp === :keep
173-
write(out, "\n" ^ num_newlines)
113+
# no-op
174114
else
175115
throw(ArgumentError("Unknown chomping indicator: $chomp"))
176116
end
177117

178-
return String(take!(out))
118+
return str
179119
end
180120

181121
"""

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const TEST_STRINGS = [
2323
""",
2424
"whitespace" => "a\nb\n c \nd\n",
2525
"no ending newline" => "foo",
26+
"starting newline" => "\nbar",
2627
]
2728

2829
# Validate `yaml_block` function

0 commit comments

Comments
 (0)