Skip to content

Commit e704b3b

Browse files
committed
Add comments
1 parent e83c0ff commit e704b3b

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/BlockScalars.jl

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@ function block(str::AbstractString, block_scalar::AbstractString="")
2323
DEFAULT_STYLE, DEFAULT_CHOMP
2424
end
2525

26+
# Append an additional character to force one more iteration of the style loop
27+
str *= '\0'
28+
2629
out = IOBuffer()
27-
num_newlines = 0
30+
num_newlines = 0 # The number of newlines at the end of the string
31+
prev = curr = '\0'
2832

33+
# Replace newlines with spaces (folded)
2934
if style == 'f'
3035
# The code below is equivalent to these two regexes:
3136
# ```
3237
# str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
3338
# str = replace(str, r"(?<=\n)\n(?=\S)" => "")
3439
# ```
3540

36-
prev = curr = '\0'
3741
for next in str
38-
3942
if curr == '\n'
4043
if !isspace(next)
4144
if prev == '\n'
42-
# Skip
45+
# Skip last newline in a sequence of sequential blank lines
4346
elseif !isspace(prev)
47+
# Replace a single newline with a space
4448
write(out, ' ')
4549
else
4650
num_newlines += 1
@@ -49,6 +53,7 @@ function block(str::AbstractString, block_scalar::AbstractString="")
4953
num_newlines += 1
5054
end
5155
elseif curr != '\0'
56+
# Insert newlines which were determined to not be at the end of the string
5257
if num_newlines > 0
5358
write(out, "\n" ^ num_newlines)
5459
num_newlines = 0
@@ -61,22 +66,13 @@ function block(str::AbstractString, block_scalar::AbstractString="")
6166
curr = next
6267
end
6368

64-
if curr == '\n'
65-
num_newlines += 1
66-
elseif curr != '\0'
67-
if num_newlines > 0
68-
write(out, "\n" ^ num_newlines)
69-
num_newlines = 0
70-
end
71-
72-
write(out, curr)
73-
end
69+
# Keep newlines (literal)
7470
elseif style == 'l'
75-
curr = '\0'
7671
for next in str
7772
if curr == '\n'
7873
num_newlines += 1
7974
elseif curr != '\0'
75+
# Insert newlines which were determined to not be at the end of the string
8076
if num_newlines > 0
8177
write(out, "\n" ^ num_newlines)
8278
num_newlines = 0
@@ -87,26 +83,22 @@ function block(str::AbstractString, block_scalar::AbstractString="")
8783

8884
curr = next
8985
end
90-
91-
if curr == '\n'
92-
num_newlines += 1
93-
elseif curr != '\0'
94-
if num_newlines > 0
95-
write(out, "\n" ^ num_newlines)
96-
num_newlines = 0
97-
end
98-
99-
write(out, curr)
100-
end
10186
else
10287
throw(ArgumentError("Unknown block style indicator: $(repr(style))"))
10388
end
10489

90+
# Single newline at end (clip)
10591
if chomp == 'c'
10692
num_newlines > 0 && write(out, '\n')
93+
94+
# No newline at end (strip)
95+
elseif chomp == 's'
96+
# no-op
97+
98+
# All newlines from end (keep)
10799
elseif chomp == 'k'
108100
write(out, "\n" ^ num_newlines)
109-
elseif chomp != 's'
101+
else
110102
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp))"))
111103
end
112104

0 commit comments

Comments
 (0)