Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1077

[PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) should return 2

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Invalid
    • 3.3.2
    • None
    • lang.*

    Description

      org.apache.commons.lang3.StringUtils.java
              int found = 0;
              int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
              do {
                  if (lastIndex) {
                      index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
                  } else {
                      index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
                  }
                  if (index < 0) {
                      return index;
                  }
                  found++;
              } while (found < ordinal);
      

      Should it be:

      org.apache.commons.lang3.StringUtils.java
              private static int ordinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal, final boolean lastIndex) {
              //        if (str == null || searchStr == null || ordinal <= 0) {
              //            return INDEX_NOT_FOUND;
              //        }
              //        if (searchStr.length() == 0) {
              //            return lastIndex ? str.length() : 0;
              //        }
              //        int found = 0;
              //        int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
              //        do {
              //            if (lastIndex) {
              //                index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
              //            } else {
              //                index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
              //            }
              //            if (index < 0) {
              //                return index;
              //            }
              //            found++;
              //        } while (found < ordinal);
              //        return index;
      
              if (str == null || searchStr == null || ordinal <= 0) {
                  return INDEX_NOT_FOUND;
              }
      
              if (searchStr.length() == 0) {
                  return lastIndex ? str.length() : 0;
              }
      
              final int searchStrLen = searchStr.length();
              int index = lastIndex ? str.length() : 0;
      
              for (int found = 0; index >= 0;) {
                  if (lastIndex) {
                      index = CharSequenceUtils.lastIndexOf(str, searchStr, index);
                  } else {
                      index = CharSequenceUtils.indexOf(str, searchStr, index);
                  }
      
                  if (index < 0) {
                      return INDEX_NOT_FOUND;
                  }
      
                  if (++found >= ordinal) {
                      break;
                  }
      
                  index = lastIndex ? index - searchStrLen : index + searchStrLen;
              }
      
              return index;
          }
      

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            lihy70 haiyang li
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment