Skip to content

Commit ae14d3f

Browse files
committed
Improve folding performance
1 parent 9f072a0 commit ae14d3f

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/BlockScalars.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,33 @@ function block(str::AbstractString, block_scalar::AbstractString="")
2424
end
2525

2626
if style == 'f'
27-
str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
28-
str = replace(str, r"(?<=\n)\n(?=\S)" => "")
27+
# The code below is equivalent to these two regexes:
28+
# ```
29+
# str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
30+
# str = replace(str, r"(?<=\n)\n(?=\S)" => "")
31+
# ```
32+
33+
b = IOBuffer()
34+
prev = curr = '\0'
35+
for next in str
36+
if curr == '\n' && !isspace(next)
37+
if prev == '\n'
38+
# Skip
39+
elseif !isspace(prev)
40+
write(b, ' ')
41+
else
42+
write(b, curr)
43+
end
44+
elseif curr != '\0'
45+
write(b, curr)
46+
end
47+
48+
prev = curr
49+
curr = next
50+
end
51+
52+
write(b, curr)
53+
str = String(take!(b))
2954
elseif style != 'l'
3055
throw(ArgumentError("Unknown block style indicator: $(repr(style))"))
3156
end

0 commit comments

Comments
 (0)