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

Clarify or improve behaviour of int-based methods in StringUtils

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.5
    • Fix Version/s: 3.6
    • Component/s: lang.*
    • Labels:
      None

      Description

      The following methods use an int to represent a search character:

      boolean contains(final CharSequence seq, final int searchChar)
      int indexOf(final CharSequence seq, final int searchChar)
      int indexOf(final CharSequence seq, final int searchChar, final int startPos)
      int lastIndexOf(final CharSequence seq, final int searchChar)
      int lastIndexOf(final CharSequence seq, final int searchChar, final int startPos)
      

      When I see an int representing a character, I tend to assume the method can handle supplementary characters. However, the current behaviour of these methods depends upon whether the CharSequence is a String or not.

      StringBuilder builder = new StringBuilder();
      builder.appendCodePoint(0x2070E);
      
      System.out.println(StringUtils.lastIndexOf(builder, 0x2070E)); // -1
      System.out.println(StringUtils.lastIndexOf(builder.toString(), 0x2070E)); // 0
      

      The Javadoc for these methods are ambiguous on this point, stating:

      This method uses String.lastIndexOf(int) if possible.

      I think we should consider updating the CharSequenceUtils methods used by this class to convert all CharSequence parameters to strings, enabling full code point support. The docs could be updated to make this crystal clear.

      There is a question of whether this breaks backwards compatibility.

        Attachments

          Activity

            People

            • Assignee:
              chtompki Rob Tompkins
              Reporter:
              dmjones500 Duncan Jones
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: