commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Minhaj Mehmood (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-622) StringUtils.lastIndexOfAnyBut() function desired
Date Sat, 24 Jul 2010 03:45:50 GMT

    [ https://issues.apache.org/jira/browse/LANG-622?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12891920#action_12891920
] 

Minhaj Mehmood commented on LANG-622:
-------------------------------------

I've gone through the StringUtils source code and found that just minor changes required to
acquire such behavior, check code below.

Following methods could be add to StringUtils class.
public static int lastIndexOfAnyBut(CharSequence cs, char[] searchChars) {
	int ret = INDEX_NOT_FOUND;
	
    if (isEmpty(cs) || ArrayUtils.isEmpty(searchChars)) {
        return ret;
    }
    int csLen = cs.length();
    int csLast = csLen - 1;
    int searchLen = searchChars.length;
    int searchLast = searchLen - 1;
    outer:
    for (int i = 0; i < csLen; i++) {
        char ch = cs.charAt(i);
        for (int j = 0; j < searchLen; j++) {
            if (searchChars[j] == ch) {
                if (i < csLast && j < searchLast && Character.isHighSurrogate(ch))
{
                    if (searchChars[j + 1] == cs.charAt(i + 1)) {
                        continue outer;
                    }
                } else {
                    continue outer;
                }
            }
        }
        ret = i;
    }
    return ret;
}

public static int lastIndexOfAnyBut(String str, String searchChars) {
	int ret = INDEX_NOT_FOUND;
    if (isEmpty(str) || isEmpty(searchChars)) {
        return ret;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        char ch = str.charAt(i);
        boolean chFound = searchChars.indexOf(ch) >= 0;
        if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
            char ch2 = str.charAt(i + 1);
            if (chFound && searchChars.indexOf(ch2) < 0) {
                ret = i;
            }
        } else {
            if (!chFound) {
            	ret = i;
            }
        }
    }
    return ret;
}

> StringUtils.lastIndexOfAnyBut() function desired
> ------------------------------------------------
>
>                 Key: LANG-622
>                 URL: https://issues.apache.org/jira/browse/LANG-622
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>            Reporter: david cogen
>            Priority: Minor
>             Fix For: 3.1
>
>
> class StringUtils needs the function lastIndexOfAnyBut()
> This would be like indexOfAnyBut() except searches from the end rather than the beginning.
> It would be like lastIndexOfAny() except looks for a character other than those in the
string rather than for those in the string.
> This is the only one of the four combinations of "from beginning"/"from end" and "for
any"/"for any but" that is not provided.
> Interestingly, the class header doc. mentions "LastIndexOfAnyBut" but the function does
not exist - even as a private, as determined by class introspection.
> (This is not a frivolous request. I actually did need this function and ended up having
to code a search loop.)

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message