Skip to content

Fix the weight and sign of base 60 scalars - #832

Open
ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:fix-sexagesimal-weight-and-sign
Open

ihamzatarar wants to merge 1 commit into
ruby:masterfrom
ihamzatarar:fix-sexagesimal-weight-and-sign

Conversation

@ihamzatarar

Copy link
Copy Markdown

Fixes #814.

ScalarScanner#tokenize resolved a base 60 scalar with

i = 0
string.split(':').each_with_index do |n,e|
  i += (n.to_i * 60 ** (e - 2).abs)
end

which carries two separate faults.

The weight assumes three components. (e - 2).abs gives 3600, 60, 1 for a three component value, but 3600, 60 for a two component one, so 12:30 came out as 12*3600 + 30*60, sixty times too large. A consequence is that 12:30 and 12:30:00 loaded to the same value, although they are different documents.

The sign reaches only the first component. "-12:30:00".split(':') is ["-12", "30", "00"], so the result was -12*3600 + 30*60 + 0, that is -41400 rather than -45000. Any negative value with a non-zero trailing component had the wrong magnitude, independently of the first fault.

The weight now comes from how many components follow, and the sign is applied to the total.

Checked against another YAML 1.1 implementation

PyYAML 6.0.3 resolves the same scalars, and agrees with this patch on every case, including the ones beyond those reported:

scalar before after, and PyYAML
12:30 45000 750
1:30 5400 90
100:00 360000 6000
12:30.5 45030.0 750.5
-12:30 -41400 -750
-12:30:00 -41400 -45000
-12:30.5 -41370.0 -750.5
+12:30 45000 750
12:30:00.5 45000.5 45000.5
1_0:30 37800 630
12:30:00 45000 45000
1:00:00 3600 3600

The last two are the three component cases that were already right, and they are unchanged.

Tests

test/psych/test_scalar_scanner.rb already covered the three component forms, 190:20:30 and 190:20:30.15, which is why this went unnoticed. Three tests are added beside them: two component values, negative values, and one asserting that 12:30 and 12:30:00 no longer resolve alike.

All three fail on master. The file passes with the change:

33 tests, 86 assertions, 0 failures, 0 errors

ScalarScanner resolved a base 60 scalar with a positional weight of
60 ** (index - 2).abs, which is only correct for three components. A
two component value such as 12:30 was weighted 3600 and 60 rather than
60 and 1, so it came out sixty times too large, and 12:30 and 12:30:00
loaded to the same value.

The sign was also taken from the first component alone, because the
minus stays on the first element of the split, so -12:30:00 resolved to
-12*3600 + 30*60 + 0, that is -41400 rather than -45000.

Take the weight from the number of components that follow, and apply
the sign to the total. PyYAML resolves all of these the same way.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sexagesimal scalars are mis-scanned

1 participant