Skip to content

Commit da4066c

Browse files
committed
Separate indicator logic from macro
1 parent 1e9bf6b commit da4066c

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed

src/BlockScalars.jl

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,49 @@ function block(str::AbstractString; style=DEFAULT_STYLE, chomp=DEFAULT_CHOMP)
1010
block(str, style, chomp)
1111
end
1212

13+
function block(str::AbstractString, indicators::AbstractString)
14+
indicators_len = length(indicators)
15+
indicators_len > 2 && throw(ArgumentError("Too many indicators provided"))
16+
17+
# Note: Using '\0` to indicate undefined
18+
style_char, chomp_char = if indicators_len == 2
19+
indicators
20+
elseif indicators_len == 1
21+
ind = indicators[1]
22+
if ind in "fl"
23+
ind, '\0'
24+
else
25+
'\0', ind
26+
end
27+
else
28+
'\0', '\0'
29+
end
30+
31+
style = if style_char == 'f'
32+
:folded
33+
elseif style_char == 'l'
34+
:literal
35+
elseif style_char == '\0'
36+
DEFAULT_STYLE
37+
else
38+
throw(ArgumentError("Unknown block style indicator: $(repr(style_char))"))
39+
end
40+
41+
chomp = if chomp_char == 'c'
42+
:clip
43+
elseif chomp_char == 's'
44+
:strip
45+
elseif chomp_char == 'k'
46+
:keep
47+
elseif chomp_char == '\0'
48+
DEFAULT_CHOMP
49+
else
50+
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp_char))"))
51+
end
52+
53+
return block(str, style, chomp)
54+
end
55+
1356
function block(str::AbstractString, style::Symbol, chomp::Symbol=DEFAULT_CHOMP)
1457
# Append an additional, non-space, character to force one more iteration of the style
1558
# loop
@@ -95,46 +138,7 @@ function block(str::AbstractString, style::Symbol, chomp::Symbol=DEFAULT_CHOMP)
95138
end
96139

97140
macro blk_str(str::AbstractString, indicators::AbstractString="")
98-
indicators_len = length(indicators)
99-
indicators_len > 2 && throw(ArgumentError("Too many indicators provided"))
100-
101-
# Note: Using '\0` to indicate undefined
102-
style_char, chomp_char = if indicators_len == 2
103-
indicators
104-
elseif indicators_len == 1
105-
ind = indicators[1]
106-
if ind in "fl"
107-
ind, '\0'
108-
else
109-
'\0', ind
110-
end
111-
else
112-
'\0', '\0'
113-
end
114-
115-
style = if style_char == 'f'
116-
:folded
117-
elseif style_char == 'l'
118-
:literal
119-
elseif style_char == '\0'
120-
DEFAULT_STYLE
121-
else
122-
throw(ArgumentError("Unknown block style indicator: $(repr(style_char))"))
123-
end
124-
125-
chomp = if chomp_char == 'c'
126-
:clip
127-
elseif chomp_char == 's'
128-
:strip
129-
elseif chomp_char == 'k'
130-
:keep
131-
elseif chomp_char == '\0'
132-
DEFAULT_CHOMP
133-
else
134-
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp_char))"))
135-
end
136-
137-
return block(str, style, chomp)
141+
return block(str, indicators)
138142
end
139143

140144
end

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ end
4848
@test block(str, style=:literal, chomp=:keep) == expected_lk
4949
@test block(str, style=:literal, chomp=:clip) == expected_lc
5050
@test block(str, style=:literal, chomp=:strip) == expected_ls
51+
52+
@test block(str, "lk") == expected_lk
53+
@test block(str, "lc") == expected_lc
54+
@test block(str, "ls") == expected_ls
5155
end
5256

5357
@testset "folding" begin
@@ -58,6 +62,10 @@ end
5862
@test block(str, style=:folded, chomp=:keep) == expected_fk
5963
@test block(str, style=:folded, chomp=:clip) == expected_fc
6064
@test block(str, style=:folded, chomp=:strip) == expected_fs
65+
66+
@test block(str, "fk") == expected_fk
67+
@test block(str, "fc") == expected_fc
68+
@test block(str, "fs") == expected_fs
6169
end
6270

6371
@testset "default chomp" begin
@@ -66,16 +74,24 @@ end
6674

6775
@test block(str, style=:literal) == expected_ls
6876
@test block(str, style=:folded) == expected_fs
77+
78+
@test block(str, "l") == expected_ls
79+
@test block(str, "f") == expected_fs
6980
end
7081

7182
@testset "default style" begin
7283
@test block(str, chomp=:keep) == expected_fk
7384
@test block(str, chomp=:clip) == expected_fc
7485
@test block(str, chomp=:strip) == expected_fs
86+
87+
@test block(str, "k") == expected_fk
88+
@test block(str, "c") == expected_fc
89+
@test block(str, "s") == expected_fs
7590
end
7691

7792
@testset "default style/chomp" begin
7893
@test block(str) == expected_fs
94+
@test block(str, "") == expected_fs
7995
end
8096
end
8197

0 commit comments

Comments
 (0)