Issue Details (XML | Word | Printable)

Key: JAXEN-56
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Elliotte Rusty Harold
Reporter: Ryan Cox
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
jaxen

Sample substring calls from XPath spec fail

Created: 01/Nov/04 03:33 PM   Updated: 13/Jan/05 09:23 PM
Component/s: None
Affects Version/s: 1.1
Fix Version/s: 1.1

Time Tracking:
Not Specified

Environment: JDK 1.5, XP


 Description  « Hide
http://www.w3.org/TR/xpath#function-substring
  • substring("12345", 0, 3) returns "12"
  • substring("12345", -42, 1 div 0) returns "12345"

Executing these calls via Jaxen yields the following errors

substring('12345', -42, 1 div 0): String index out of range: -43
substring('12345', 0, 3): String index out of range: -1



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Brian Ewins added a comment - 16/Nov/04 12:04 PM
neither of these give errors in the nightlies, there's test cases:

<valueOf select="substring('12345', 0, 3)">123</valueOf>
<valueOf select="substring('12345', -42, 1 div 0)">12345</valueOf>

(http://cvs.jaxen.codehaus.org/jaxen/xml/test/tests.xml?rev=1.73&view=auto)

However as you can see above these aren't quite correct with respect to the spec. Below is my code for Functions.substring() (part of the zip attached to JAXEN-31) which I believe matches the spec:

public static String substring(String s1, double dpos, double dlen)
{
dpos = round(dpos); // see below
dlen = round(dlen);
if (Double.isNaN(dpos + dlen)) { return ""; }
int ipos = ((int) dpos) - 1;
int imax = ipos + (int) dlen;
if (ipos < 0)
{ ipos = 0; }
if (imax > s1.length())
{ imax = s1.length(); }
if (ipos >= imax)
{ return ""; } }
return s1.substring(ipos, imax);
}
public static double round(double value)
{
if (Double.isNaN(value)

Double.isInfinite(value)
value == 0.0
value == -0.0) { return value; }

return Math.round(value);
}

since there's still a spec compliance problem here I'll leave this open for now.