Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions lib/psych/scalar_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,19 @@ def tokenize string
@symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
end
elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
sign = string.start_with?('-') ? -1 : 1
i = 0
string.split(':').each_with_index do |n,e|
string.delete_prefix('-').delete_prefix('+').split(':').each_with_index do |n,e|
i += (n.to_i * 60 ** (e - 2).abs)
end
i
i * sign
elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
sign = string.start_with?('-') ? -1 : 1
i = 0
string.split(':').each_with_index do |n,e|
string.delete_prefix('-').delete_prefix('+').split(':').each_with_index do |n,e|
i += (n.to_f * 60 ** (e - 2).abs)
end
i
i * sign
elsif string.match?(FLOAT)
if string.match?(/\A[-+]?\.\Z/)
string
Expand Down
8 changes: 8 additions & 0 deletions test/psych/test_scalar_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_scan_sexagesimal_int
assert_equal 685230, ss.tokenize('190:20:30')
end

def test_scan_negative_sexagesimal_int
assert_equal(-685230, ss.tokenize('-190:20:30'))
end

def test_scan_negative_sexagesimal_float
assert_equal(-685230.15, ss.tokenize('-190:20:30.15'))
end

def test_scan_float
assert_equal 1.2, ss.tokenize('1.2')
end
Expand Down