File tree Expand file tree Collapse file tree 1 file changed +42
-17
lines changed Expand file tree Collapse file tree 1 file changed +42
-17
lines changed Original file line number Diff line number Diff line change @@ -23,48 +23,73 @@ function block(str::AbstractString, block_scalar::AbstractString="")
23
23
DEFAULT_STYLE, DEFAULT_CHOMP
24
24
end
25
25
26
+ out = IOBuffer ()
27
+ ending = IOBuffer ()
28
+
26
29
if style == ' f'
27
30
# The code below is equivalent to these two regexes:
28
31
# ```
29
32
# str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
30
33
# str = replace(str, r"(?<=\n)\n(?=\S)" => "")
31
34
# ```
32
35
33
- b = IOBuffer ()
34
36
prev = curr = ' \0 '
35
37
for next in str
36
- if curr == ' \n ' && ! isspace (next)
37
- if prev == ' \n '
38
- # Skip
39
- elseif ! isspace (prev)
40
- write (b, ' ' )
38
+
39
+ if curr == ' \n '
40
+ if ! isspace (next)
41
+ if prev == ' \n '
42
+ # Skip
43
+ elseif ! isspace (prev)
44
+ write (out, ' ' )
45
+ else
46
+ write (ending, ' \n ' )
47
+ end
41
48
else
42
- write (b, curr )
49
+ write (ending, ' \n ' )
43
50
end
44
51
elseif curr != ' \0 '
45
- write (b, curr)
52
+ write (out, take! (ending))
53
+ ending = IOBuffer ()
54
+
55
+ write (out, curr)
46
56
end
47
57
48
58
prev = curr
49
59
curr = next
50
60
end
51
61
52
- write (b, curr)
53
- str = String (take! (b))
54
- elseif style != ' l'
62
+ write (out, curr)
63
+ elseif style == ' l'
64
+ curr = ' \0 '
65
+ for next in str
66
+ if curr == ' \n '
67
+ write (ending, ' \n ' )
68
+ elseif curr != ' \0 '
69
+ write (out, take! (ending))
70
+ ending = IOBuffer ()
71
+
72
+ write (out, curr)
73
+ end
74
+
75
+ curr = next
76
+ end
77
+
78
+ write (out, curr)
79
+ else
55
80
throw (ArgumentError (" Unknown block style indicator: $(repr (style)) " ))
56
81
end
57
82
83
+ seekstart (ending)
58
84
if chomp == ' c'
59
- suffix = endswith (str, ' \n ' ) ? " \n " : " "
60
- str = rstrip (str, ' \n ' ) * suffix
61
- elseif chomp == ' s'
62
- str = rstrip (str, ' \n ' )
63
- elseif chomp != ' k'
85
+ ! eof (ending) && write (out, read (ending, Char))
86
+ elseif chomp == ' k'
87
+ write (out, ending)
88
+ elseif chomp != ' s'
64
89
throw (ArgumentError (" Unknown block chomping indicator: $(repr (chomp)) " ))
65
90
end
66
91
67
- return str
92
+ return String ( take! (out))
68
93
end
69
94
70
95
macro blk_str (str:: AbstractString , suffix:: AbstractString = " " )
You can’t perform that action at this time.
0 commit comments