-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INI: Rewrite parser to avoid regular expressions (#5442)
`INI.parse` now parses an INI-formatted string into a `Hash` without using any regular expressions.
- 1.15.1
- 1.15.0
- 1.14.1
- 1.14.0
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.36.1
- 0.36.0
- 0.35.1
- 0.35.0
- 0.34.0
- 0.33.0
- 0.32.1
- 0.32.0
- 0.31.1
- 0.31.0
- 0.30.1
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.1
- 0.26.0
- 0.25.1
- 0.25.0
Showing
2 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,58 @@ | ||
class INI | ||
# Exception thrown on an INI parse error. | ||
class ParseException < Exception | ||
getter line_number : Int32 | ||
getter column_number : Int32 | ||
|
||
def initialize(message, @line_number, @column_number) | ||
super "#{message} at #{@line_number}:#{@column_number}" | ||
end | ||
|
||
def location | ||
{line_number, column_number} | ||
end | ||
end | ||
|
||
# Parses INI-style configuration from the given string. | ||
# Raises a `ParseException` on any errors. | ||
# | ||
# ``` | ||
# INI.parse("[foo]\na = 1") # => {"foo" => {"a" => "1"}} | ||
# ``` | ||
def self.parse(str) : Hash(String, Hash(String, String)) | ||
ini = {} of String => Hash(String, String) | ||
ini = Hash(String, Hash(String, String)).new | ||
current_section = ini[""] = Hash(String, String).new | ||
lineno = 0 | ||
|
||
section = "" | ||
str.each_line do |line| | ||
if line =~ /\s*(.*[^\s])\s*=\s*(.*[^\s])/ | ||
ini[section] ||= {} of String => String if section == "" | ||
ini[section][$1] = $2 | ||
elsif line =~ /\[(.*)\]/ | ||
section = $1 | ||
ini[section] = {} of String => String | ||
lineno += 1 | ||
next if line.empty? | ||
|
||
offset = 0 | ||
line.each_char do |char| | ||
break unless char.ascii_whitespace? | ||
offset += 1 | ||
end | ||
|
||
case line[offset] | ||
when '#', ';' | ||
next | ||
when '[' | ||
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 | ||
key, eq, value = line.partition('=') | ||
raise ParseException.new("expected declaration", lineno, key.size) if eq != "=" | ||
|
||
current_section[key.strip] = value.strip | ||
end | ||
end | ||
|
||
ini.delete_if { |_, v| v.empty? } | ||
ini | ||
end | ||
end |