Skip to content

Commit

Permalink
Keep empty sections when parsing INI
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Feb 13, 2018
1 parent b30a9cc commit 2c63e43
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
6 changes: 3 additions & 3 deletions spec/std/ini_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe "INI" do

it "ignores whitespaces" do
INI.parse(" key = value ").should eq({"" => {"key" => "value"}})
INI.parse(" [foo]").should eq({} of String => Hash(String, String))
INI.parse(" [foo]").should eq({"foo" => Hash(String, String).new})
end

it "ignores comments" do
Expand All @@ -50,8 +50,8 @@ describe "INI" do
INI.parse("[foo]\na=1\n[foo]\nb=2").should eq({"foo" => {"a" => "1", "b" => "2"}})
end

it "ignores an empty section" do
INI.parse("[section]").should eq({} of String => Hash(String, String))
it "parses an empty section" do
INI.parse("[section]").should eq({"section" => Hash(String, String).new})
end

it "parses a file" do
Expand Down
3 changes: 1 addition & 2 deletions src/ini.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class INI
end_idx = line.index(']', offset)
raise ParseException.new("unterminated section", lineno, line.size) unless end_idx
raise ParseException.new("data after section", lineno, end_idx + 1) unless end_idx == line.size - 1

current_section_name = line[offset + 1...end_idx]
current_section = ini[current_section_name] ||= Hash(String, String).new
else
Expand All @@ -52,7 +51,7 @@ class INI
end
end

ini.delete_if { |_, v| v.empty? }
ini.delete_if { |k, v| k.empty? && v.empty? }
ini
end

Expand Down

0 comments on commit 2c63e43

Please sign in to comment.