Skip to content

Commit d8bbc91

Browse files
committed
Improve indicator performance
1 parent d425bdc commit d8bbc91

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/BlockScalars.jl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,46 @@ module BlockScalars
22

33
export @blk_str
44

5-
const DEFAULT_BLOCK_SCALAR = "fs"
65

7-
function block(str::AbstractString, block_scalar::AbstractString=DEFAULT_BLOCK_SCALAR)
8-
style_indicators = intersect(block_scalar, "fl")
9-
chomp_indicators = intersect(block_scalar, "csk")
10-
11-
if length(style_indicators) == 0
12-
style_indicator = 'f'
13-
elseif length(style_indicators) == 1
14-
style_indicator = first(style_indicators)
15-
else
16-
throw(ArgumentError("Only one block style indicators can be provided"))
17-
end
18-
19-
if length(chomp_indicators) == 0
20-
chomp_indicator = 's'
21-
elseif length(chomp_indicators) == 1
22-
chomp_indicator = first(chomp_indicators)
6+
const DEFAULT_STYLE = 'f'
7+
const DEFAULT_CHOMP = 's'
8+
9+
function block(str::AbstractString, block_scalar::AbstractString="")
10+
block_scalar_len = length(block_scalar)
11+
block_scalar_len > 2 && throw(ArgumentError("Too many indicators provided"))
12+
13+
style, chomp = if block_scalar_len == 2
14+
block_scalar
15+
elseif block_scalar_len == 1
16+
ind = block_scalar[1]
17+
if ind in "fl"
18+
ind, DEFAULT_CHOMP
19+
else
20+
DEFAULT_STYLE, ind
21+
end
2322
else
24-
throw(ArgumentError("Only one block chomp indicators can be provided"))
23+
DEFAULT_STYLE, DEFAULT_CHOMP
2524
end
2625

27-
if style_indicator == 'f'
26+
if style == 'f'
2827
str = replace(str, r"(?<=\S)\n(?=\S)" => " ")
2928
str = replace(str, r"(?<=\S)\n(\n+)(?!$)" => s"\1")
29+
elseif style != 'l'
30+
throw(ArgumentError("Unknown block style indicator: $(repr(style))"))
3031
end
3132

32-
if chomp_indicator == 'c'
33+
if chomp == 'c'
3334
str = replace(str, r"\n+$" => "\n")
34-
elseif chomp_indicator == 's'
35+
elseif chomp == 's'
3536
str = replace(str, r"\n+$" => "")
37+
elseif chomp != 'k'
38+
throw(ArgumentError("Unknown block chomping indicator: $(repr(chomp))"))
3639
end
3740

3841
return str
3942
end
4043

41-
macro blk_str(str::AbstractString, suffix::AbstractString=DEFAULT_BLOCK_SCALAR)
44+
macro blk_str(str::AbstractString, suffix::AbstractString="")
4245
block(str, suffix)
4346
end
4447

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ const WHITESPACE = "a\nb\n c \nd\n"
5656
@test block(str) == yaml_block(str, ">-")
5757
end
5858
end
59+
60+
@testset "block invalid indicator" begin
61+
@test_throws ArgumentError block("", "fs_") # Too many indicators
62+
@test_throws ArgumentError block("", "sf") # Order matters
63+
@test_throws ArgumentError block("", "_s") # Invalid style
64+
@test_throws ArgumentError block("", "f_") # Invalid chomp
65+
@test_throws ArgumentError block("", "_") # Invalid style/chomp
66+
end
5967
end

0 commit comments

Comments
 (0)