Skip to content

Commit a68c74b

Browse files
authored
Fix XPath substring() with a start before the first character and round() for negative halves (#382)
`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.
1 parent 121633d commit a68c74b

2 files changed

Lines changed: 48 additions & 34 deletions

File tree

‎lib/rexml/functions.rb‎

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -232,34 +232,25 @@ def substring_after( string, test )
232232
# vigorously, and pour into a tall, chilled glass. Serves 10,000.
233233
def substring( string, start, length=nil )
234234
ruby_string = string(string)
235-
ruby_length = if length.nil?
236-
ruby_string.length.to_f
237-
else
238-
number(length)
239-
end
240-
ruby_start = number(start)
241-
242-
# Handle the special cases
243-
return '' if (
244-
ruby_length.nan? or
245-
ruby_start.nan? or
246-
ruby_start.infinite?
247-
)
248-
249-
infinite_length = ruby_length.infinite? == 1
250-
ruby_length = ruby_string.length if infinite_length
251-
252-
# Now, get the bounds. The XPath bounds are 1..length; the ruby bounds
253-
# are 0..length. Therefore, we have to offset the bounds by one.
254-
ruby_start = round(ruby_start) - 1
255-
ruby_length = round(ruby_length)
256-
257-
if ruby_start < 0
258-
ruby_length += ruby_start unless infinite_length
259-
ruby_start = 0
235+
# The returned substring contains each character whose position p
236+
# satisfies: round(start) <= p < round(start) + round(length)
237+
first = round(start)
238+
last = if length.nil?
239+
Float::INFINITY
240+
else
241+
first + round(length)
242+
end
243+
# This is also false when first or last is NaN.
244+
return '' unless first < last
245+
246+
ruby_start = first < 1 ? 0 : first - 1
247+
if last > ruby_string.length
248+
ruby_string[ruby_start..-1] || ''
249+
elsif last - 1 > ruby_start
250+
ruby_string[ruby_start...(last - 1)]
251+
else
252+
''
260253
end
261-
return '' if ruby_length <= 0
262-
ruby_string[ruby_start,ruby_length]
263254
end
264255

265256
def string_length( object=@context[:node] )
@@ -431,13 +422,14 @@ def ceiling( number )
431422

432423
def round( number )
433424
number = number(number)
434-
begin
435-
neg = number.negative?
436-
number = number.abs.round
437-
neg ? -number : number
438-
rescue FloatDomainError
439-
number
440-
end
425+
return number if number.nan? or number.infinite?
426+
# If there are two integers closest to the number, the one that is
427+
# closest to positive infinity is returned. If the number is less than
428+
# zero but greater than or equal to -0.5, negative zero is returned.
429+
return -0.0 if number < 0 and number >= -0.5
430+
rounded = number.floor
431+
rounded += 1 if number - rounded >= 0.5
432+
rounded
441433
end
442434

443435
def call(name, args:, fallback: nil)

‎test/functions/test_base.rb‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ def test_substring2
177177
assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size)
178178
end
179179

180+
def test_substring_start_before_first_character
181+
doc = Document.new("<a/>")
182+
assert_equal("12345", XPath.first(doc, "substring('12345', 0)"))
183+
assert_equal("12345", XPath.first(doc, "substring('12345', -1)"))
184+
assert_equal("12345", XPath.first(doc, "substring('12345', -0.5)"))
185+
assert_equal("12345", XPath.first(doc, "substring('12345', -1 div 0)"))
186+
assert_equal("1", XPath.first(doc, "substring('12345', -0.5, 1.5)"))
187+
assert_equal("", XPath.first(doc, "substring('12345', -1, 1)"))
188+
assert_equal("", XPath.first(doc, "substring('12345', 6)"))
189+
end
190+
180191
# Submitted by Kouhei
181192
def test_floor_ceiling_round
182193
source = "<a><b id='1'/><b id='2'/><b id='3'/></a>"
@@ -207,6 +218,17 @@ def test_floor_ceiling_round
207218
end
208219
end
209220

221+
def test_round_half_toward_positive_infinity
222+
doc = REXML::Document.new("<a/>")
223+
assert_equal("3", REXML::XPath.first(doc, "string(round(2.5))"))
224+
assert_equal("-2", REXML::XPath.first(doc, "string(round(-2.5))"))
225+
assert_equal("0", REXML::XPath.first(doc, "string(round(-0.5))"))
226+
assert_equal("-Infinity", REXML::XPath.first(doc, "string(1 div round(-0.5))"))
227+
assert_equal("-Infinity", REXML::XPath.first(doc, "string(1 div round(-0.4))"))
228+
assert_equal("1", REXML::XPath.first(doc, "string(round(0.5))"))
229+
assert_equal("0", REXML::XPath.first(doc, "string(round(0.49999999999999994))"))
230+
end
231+
210232
def test_floor_ceiling_not_finite
211233
doc = REXML::Document.new("<a/>")
212234
assert_equal([

0 commit comments

Comments
 (0)