Skip to content

Fix StringUtilities.capitalise throwing on leading whitespace#7561

Open
dsavy4 wants to merge 1 commit into
finos:masterfrom
dsavy4:fix/capitalise-leading-whitespace
Open

Fix StringUtilities.capitalise throwing on leading whitespace#7561
dsavy4 wants to merge 1 commit into
finos:masterfrom
dsavy4:fix/capitalise-leading-whitespace

Conversation

@dsavy4

@dsavy4 dsavy4 commented Jul 24, 2026

Copy link
Copy Markdown

Problem

StringUtilities.capitalise throws StringIndexOutOfBoundsException when the input has leading whitespace.

words.split("\\s+") keeps an empty leading token when the string starts with whitespace (Java only trims trailing empty tokens). The per-word mapper then calls charAt(0) and substring(1) on that empty token and throws.

StringUtilities.capitalise(" hello"); // throws, expected "Hello"
StringUtilities.capitalise("   ");     // throws, expected ""

Fix

Filter out empty tokens before mapping:

.stream(words.split("\\s+"))
.filter(w -> !w.isEmpty())
.map(...)

Whitespace-only input now returns "", which matches the intent already noted in the existing test comments (" " => ""). All existing cases are unchanged, since none of them start with whitespace.

Tests

Added leadingWhitespace and onlyWhitespace cases to StringUtilities_capitaliseTest. Verified the fix independently with the JDK (old throws, fixed returns Hello / Hello World / "", existing cases unchanged).

split("\\s+") keeps an empty leading token when the input starts with
whitespace, and the per-word mapper then calls charAt(0) and substring(1)
on that empty token, throwing StringIndexOutOfBoundsException. For example
capitalise(" hello") threw instead of returning "Hello".

Filter out empty tokens before mapping. Whitespace-only input now returns
an empty string, matching the intent noted in the existing tests. Added
tests for leading whitespace and whitespace-only input.
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 24, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: dsavy4 / name: Dmitry Savy (50154c3)

@dsavy4

dsavy4 commented Jul 24, 2026

Copy link
Copy Markdown
Author

CLA Not Signed

Signed and accepted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant