Bug 32896

Summary: Invalid endsWith function
Product: Taglibs Reporter: Rastislav Rehak <rasto>
Component: Standard TaglibAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: jmiguelss
Priority: P2    
Version: nightly   
Target Milestone: ---   
Hardware: All   
OS: All   
URL: http://cvs.apache.org/viewcvs.cgi/jakarta-taglibs/standard/src/org/apache/taglibs/standard/functions/Functions.java?rev=1.9&view=markup

Description Rastislav Rehak 2004-12-30 21:19:43 UTC
The function endsWith in standard library returns false also when the searched
substring is contained twice in string. 
Example : fn:endsWith( 'abcdb' ,'b' ) returns false.

Original source code :

    public static boolean endsWith(String input, String substring) {
        if (input == null) input = "";
        if (substring == null) substring = "";
        int index = input.indexOf(substring);
        if (index == -1) return false;
        if (index == 0 && substring.length() == 0) return true;
        return (index == input.length() - substring.length());
    }  

should be :

    public static boolean endsWith(String input, String substring) {
        if (input == null) input = "";
        if (substring == null) substring = "";
        int index = input.lastIndexOf(substring);
        if (index == -1) return false;
        if (index == 0 && substring.length() == 0) return true;
        return (index == input.length() - substring.length());
    }
Comment 1 Dhiru Pandey 2005-02-17 02:33:19 UTC
Thanks for the fix Rastislav Rehak. The fix was committed by Pierre Delisle.

Marking this bug fixed.

-Dhiru
Comment 2 Henri Yandell 2010-03-22 01:29:43 UTC
*** Bug 48848 has been marked as a duplicate of this bug. ***