jaxen

Sample substring calls from XPath spec fail

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor 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 :
    0

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

Activity

Hide
Brian Ewins added a comment -

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.

Show
Brian Ewins added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: