commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

'abbreviate' contract violations

Open ThrawnCA opened this issue 1 month ago • 2 comments

I have added unit tests showing incorrect behaviour of StringUtils.abbreviate on short strings (between abbrevMarker.length + 1 and abbrevMarker.length * 2).

The documentation states that the offset character will always appear somewhere in the result, but when an offset is applied to a short string, it may not be.

ThrawnCA avatar Nov 14 '25 23:11 ThrawnCA

I'm unsure what's the best approach to fixing this. Should the documentation be updated to reflect the fact that for short strings, the offset marker might not be present in the result? Should the logic be changed so that the offset marker is present, and if so, what is the most efficient way?

ThrawnCA avatar Nov 17 '25 22:11 ThrawnCA

The failing test:

        // abbreviating a shorter string allows maxWidth < 7
        assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));

looks like a bug indeed.

garydgregory avatar Nov 28 '25 13:11 garydgregory