Repository navigation
Fix XPath substring() with a start before the first character and round() for negative halves - #382
Merged
Merged
Conversation
…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.
Contributor
Author
naitoh
reviewed
Nov 28, 2026
naitoh
left a comment
Contributor
There was a problem hiding this comment.
Thank you for fixing this PR!
The implementation in lib/rexml/functions.rb looks good to me.
Using individual assert_equal calls makes it much easier to see which expected value corresponds to which XPath expression.
Contributor
Author
Contributor
|
Thanks! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
substring()drops characters when the start is before the first character, andround()rounds negative halves the wrong way.substring('12345', 0)12341234512345substring('12345', -1)1231234512345substring('12345', -1 div 0)1234512345substring('12345', -0.5, 1.5)11round(-2.5)-3-2-2round(-0.5)-1-0-01 div round(-0.4)Infinity-Infinity-InfinityCause:
ruby_length += ruby_start), so every position before 1 cut one more character off the end. A-Infinitystart returned''right away.round()roundedabs(n)with Ruby's half-away-from-zero rule and put the sign back, so-2.5became-3.https://www.w3.org/TR/xpath-10/#function-substring
https://www.w3.org/TR/xpath-10/#function-round
substring()now follows that rule directly: keep positionspwithround(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 testpasses (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.