Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.1
-
Fix Version/s: 1.1
-
Component/s: None
-
Labels:None
-
Environment:JDK 1.5, XP
-
Number of attachments :
Description
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
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)
return Math.round(value);
}
since there's still a spec compliance problem here I'll leave this open for now.