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

Refine StringUtils.abbreviate

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 3.12.0
    • None

    Description

      Avoid Multiple calls to calculate the length of the chain

       

      https://github.com/apache/commons-lang/pull/663

       public static String abbreviate(final String str, final String abbrevMarker, int offset, final int maxWidth) {
              if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) {
                  return substring(str, 0, maxWidth);
              } else if (isAnyEmpty(str, abbrevMarker)) {
                  return str;
              }
              final int abbrevMarkerLength = abbrevMarker.length();
              final int minAbbrevWidth = abbrevMarkerLength + 1;
              final int minAbbrevWidthOffset = abbrevMarkerLength + abbrevMarkerLength + 1;
      
              if (maxWidth < minAbbrevWidth) {
                  throw new IllegalArgumentException(String.format("Minimum abbreviation width is %d", minAbbrevWidth));
              }
              final int strLen = str.length();
              if (strLen <= maxWidth) {
                  return str;
              }
              if (offset > strLen) {
                  offset = strLen;
              }
              if (strLen - offset < maxWidth - abbrevMarkerLength) {
                  offset = strLen - (maxWidth - abbrevMarkerLength);
              }
              if (offset <= abbrevMarkerLength+1) {
                  return str.substring(0, maxWidth - abbrevMarkerLength) + abbrevMarker;
              }
              if (maxWidth < minAbbrevWidthOffset) {
                  throw new IllegalArgumentException(String.format("Minimum abbreviation width with offset is %d", minAbbrevWidthOffset));
              }
              if (offset + maxWidth - abbrevMarkerLength < strLen) {
                  return abbrevMarker + abbreviate(str.substring(offset), abbrevMarker, maxWidth - abbrevMarkerLength);
              }
              return abbrevMarker + str.substring(strLen - (maxWidth - abbrevMarkerLength));
          }
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            arturobernalg Arturo Bernal
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 50m
                50m