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
60 changes: 26 additions & 34 deletions lib/rexml/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,34 +232,25 @@ def substring_after( string, test )
# vigorously, and pour into a tall, chilled glass. Serves 10,000.
def substring( string, start, length=nil )
ruby_string = string(string)
ruby_length = if length.nil?
ruby_string.length.to_f
else
number(length)
end
ruby_start = number(start)

# Handle the special cases
return '' if (
ruby_length.nan? or
ruby_start.nan? or
ruby_start.infinite?
)

infinite_length = ruby_length.infinite? == 1
ruby_length = ruby_string.length if infinite_length

# Now, get the bounds. The XPath bounds are 1..length; the ruby bounds
# are 0..length. Therefore, we have to offset the bounds by one.
ruby_start = round(ruby_start) - 1
ruby_length = round(ruby_length)

if ruby_start < 0
ruby_length += ruby_start unless infinite_length
ruby_start = 0
# The returned substring contains each character whose position p
# satisfies: round(start) <= p < round(start) + round(length)
first = round(start)
last = if length.nil?
Float::INFINITY
else
first + round(length)
end
# This is also false when first or last is NaN.
return '' unless first < last

ruby_start = first < 1 ? 0 : first - 1
if last > ruby_string.length
ruby_string[ruby_start..-1] || ''
elsif last - 1 > ruby_start
ruby_string[ruby_start...(last - 1)]
else
''
end
return '' if ruby_length <= 0
ruby_string[ruby_start,ruby_length]
end

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

def round( number )
number = number(number)
begin
neg = number.negative?
number = number.abs.round
neg ? -number : number
rescue FloatDomainError
number
end
return number if number.nan? or number.infinite?
# If there are two integers closest to the number, the one that is
# closest to positive infinity is returned. If the number is less than
# zero but greater than or equal to -0.5, negative zero is returned.
return -0.0 if number < 0 and number >= -0.5
rounded = number.floor
rounded += 1 if number - rounded >= 0.5
rounded
end

def call(name, args:, fallback: nil)
Expand Down
39 changes: 39 additions & 0 deletions test/functions/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ def test_substring2
assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size)
end

def test_substring_start_before_first_character
doc = Document.new("<a/>")
assert_equal([
"12345",
"12345",
"12345",
"12345",
"1",
"",
"",
],
[
XPath.first(doc, "substring('12345', 0)"),
XPath.first(doc, "substring('12345', -1)"),
XPath.first(doc, "substring('12345', -0.5)"),
XPath.first(doc, "substring('12345', -1 div 0)"),
XPath.first(doc, "substring('12345', -0.5, 1.5)"),
XPath.first(doc, "substring('12345', -1, 1)"),
XPath.first(doc, "substring('12345', 6)"),
])
end

# Submitted by Kouhei
def test_floor_ceiling_round
source = "<a><b id='1'/><b id='2'/><b id='3'/></a>"
Expand Down Expand Up @@ -207,6 +229,23 @@ def test_floor_ceiling_round
end
end

def test_round_half_toward_positive_infinity
doc = REXML::Document.new("<a/>")
assert_equal([
"3", "-2", "0", "-Infinity", "-Infinity",
"1", "0",
],
[
REXML::XPath.first(doc, "string(round(2.5))"),
REXML::XPath.first(doc, "string(round(-2.5))"),
REXML::XPath.first(doc, "string(round(-0.5))"),
REXML::XPath.first(doc, "string(1 div round(-0.5))"),
REXML::XPath.first(doc, "string(1 div round(-0.4))"),
REXML::XPath.first(doc, "string(round(0.5))"),
REXML::XPath.first(doc, "string(round(0.49999999999999994))"),
])
end

def test_floor_ceiling_not_finite
doc = REXML::Document.new("<a/>")
assert_equal([
Expand Down