Skip to content

Fix XPath substring() with a start before the first character and round() for negative halves - #382

Open
bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-xpath-substring-round
Open

bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-xpath-substring-round

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

substring() drops characters when the start is before the first character, and round() rounds negative halves the wrong way.

query master this PR libxml2
substring('12345', 0) 1234 12345 12345
substring('12345', -1) 123 12345 12345
substring('12345', -1 div 0) (empty) 12345 12345
substring('12345', -0.5, 1.5) (empty) 1 1
round(-2.5) -3 -2 -2
round(-0.5) -1 -0 -0
1 div round(-0.4) Infinity -Infinity -Infinity

Cause:

  • The two-argument form set the length to the string length and then subtracted the start offset from it (ruby_length += ruby_start), so every position before 1 cut one more character off the end. A -Infinity start returned '' right away.
  • round() rounded abs(n) with Ruby's half-away-from-zero rule and put the sign back, so -2.5 became -3.

https://www.w3.org/TR/xpath-10/#function-substring

More precisely, each character in the string is considered to have a numeric position: the position of the first character is 1, the position of the second character is 2 and so on. [...] The returned substring contains those characters for which the position of the character is greater than or equal to the rounded value of the second argument and, if the third argument is specified, less than the sum of the rounded value of the second argument and the rounded value of the third argument

https://www.w3.org/TR/xpath-10/#function-round

The round function returns the number that is closest to the argument and that is an integer. If there are two such numbers, then the one that is closest to positive infinity is returned. [...] If the argument is less than zero, but greater than or equal to -0.5, then negative zero is returned.

substring() now follows that rule directly: keep positions p with round(start) <= p < round(start) + round(length) (no upper bound when there is no length). NaN bounds still give '', so the existing spec examples (substring("12345", 0 div 0, 3), substring("12345", -1 div 0, 1 div 0), ...) keep passing.

The two new tests fail on master and pass here. bundle exec rake test passes (843 tests). I also ran ~2,300 XPath expressions (substring with many start/length values, round/floor/ceiling, comparisons, axes) against libxml2 through lxml: all substring and round differences are gone, and no other result changed.

…nd() for negative halves

substring('12345', 0) returned '1234' and substring('12345', -1 div 0)
returned '' because the two-argument form subtracted the start offset
from the string length. round(-2.5) returned -3 and round(-0.5)
returned -1 because negative numbers were rounded half away from zero.
Copilot AI balanced review requested due to automatic review settings November 18, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants