Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of _print(::IO, ::Pair, ::Int, ::Bool). #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,16 @@ _print(io::IO, arr::AbstractVector, level::Int=0, ignore_level::Bool=false) =

# print a single key-value pair
function _print(io::IO, pair::Pair, level::Int=0, ignore_level::Bool=false)
key = if pair[1] === nothing
"null" # this is what the YAML parser interprets as 'nothing'
else
string(pair[1]) # any useful case
end
p0 = first(pair)
p1 = last(pair)
Comment on lines +69 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are not so important but I would have gone for p1 and p2 rather than p0 and p1.

key = p0 ≡ nothing ? "null" : string(p0)
print(io, _indent(key * ":", level, ignore_level)) # print the key
if (pair[2] isa AbstractDict || pair[2] isa AbstractVector) && !isempty(pair[2])
print(io, "\n") # a line break is needed before a recursive structure
if (p1 isa AbstractDict || p1 isa AbstractVector) && !isempty(p1)
print(io, '\n') # a line break is needed before a recursive structure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure writing a character is better than writing a string? I'm finding the former to be slower and having an allocation (although I have no idea why that should be necessary).

else
print(io, " ") # a whitespace character is needed before a single value
print(io, ' ') # a whitespace character is needed before a single value
end
_print(io, pair[2], level + 1) # print the value
_print(io, p1, level + 1) # print the value
end

# _print a single string
Expand Down
Loading