Skip to content

Add string utility functions - #33

Closed
CristianAmbrosini wants to merge 2 commits into
mainfrom
add-string-helpers
Closed

Add string utility functions#33
CristianAmbrosini wants to merge 2 commits into
mainfrom
add-string-helpers

Conversation

@CristianAmbrosini

Copy link
Copy Markdown
Owner

No description provided.

@sonar-review-dev18

sonar-review-dev18 Bot commented Apr 24, 2026

Copy link
Copy Markdown

Summary

This PR adds a new string_utils.py module with six utility functions for common string operations: normalize_whitespace, to_snake_case, count_words, pad_left, reverse_words, and truncate. These provide reusable helpers for text manipulation tasks.

What reviewers should know

Functions added:

  • normalize_whitespace() — collapses multiple spaces into single spaces
  • to_snake_case() — converts camelCase to snake_case
  • count_words() — word counter with empty-string handling
  • pad_left() — left-pads text with configurable character
  • reverse_words() — reverses word order
  • truncate() — shortens text with suffix (e.g., "...")

Things to check:

  • to_snake_case() uses a simple character-by-character approach; verify it handles your actual use cases (consecutive capitals like "HTTPSConnection" may not convert as expected)
  • pad_left() silently returns the text unchanged if it's already at or over the target width—confirm this is the desired behavior
  • No tests are visible in this diff; check if they exist elsewhere in the PR
  • Confirm these functions don't duplicate existing utilities in your codebase

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two real bugs found in the new module that need fixing before merge.

SonarQube Cloud quality gate is failing — coverage on new code is 0% against an 80% threshold. See the reviewer guide above for details.

🗣️ Give feedback

Comment thread string_utils.py
Comment on lines +5 to +11
def to_snake_case(text):
result = []
for i, char in enumerate(text):
if char.isupper() and i > 0:
result.append('_')
result.append(char.lower())
return ''.join(result)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: to_snake_case does not handle consecutive uppercase letters (acronyms). Each uppercase character unconditionally gets its own underscore prefix, so "XMLParser""x_m_l_parser" instead of "xml_parser".

If the function needs to handle real-world identifiers, consecutive uppercase runs need to be treated as a single word. A common approach: only insert _ when transitioning from a lowercase character to an uppercase one, or when a run of uppercase letters is followed by a lowercase letter.

  • Mark as noise

Comment thread string_utils.py

def pad_left(text, width, char=' '):
if len(text) >= width:
return text

@sonar-review-dev18 sonar-review-dev18 Bot Apr 24, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: char is not validated to be a single character. If the caller passes a multi-character string (e.g. pad_left("x", 5, "ab")), char * (width - len(text)) produces a string of the wrong length and the result won't respect width. Raise ValueError if len(char) != 1.

Suggested change
return text
if len(char) != 1:
raise ValueError("char must be a single character")
if len(text) >= width:
return text
  • Mark as noise

Comment thread string_utils.py
Comment on lines +20 to +21
def pad_left(text, width, char=' '):
if len(text) >= width:

@sonar-review-dev18 sonar-review-dev18 Bot Apr 24, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: No test covers the guard clause (len(text) >= width) or the multi-char char edge case. Unit tests with len(text) == width, len(text) > width, and char of length > 1 would catch the regression above and confirm the guard returns the original string unchanged.

  • Mark as noise

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sonarqube-cloud-dev18

Copy link
Copy Markdown

SonarQube reviewer guide

Review in SonarQube

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@sonar-review-dev18 sonar-review-dev18 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One bug in truncate needs fixing before merge (see inline). The to_snake_case consecutive-uppercase issue from the earlier review round is also still open.

SonarQube Cloud status: Coverage on new code is 0% against an 80% threshold — see the SonarQube reviewer guide above for details.

🗣️ Give feedback

Comment thread string_utils.py
Comment on lines +27 to +33
return ' '.join(text.split()[::-1])


def truncate(text, max_length, suffix='...'):
if len(text) <= max_length:
return text
return text[:max_length - len(suffix)] + suffix

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When max_length is smaller than len(suffix) (e.g. truncate("hello", 2)), max_length - len(suffix) is negative. text[:-1] slices from the end rather than the start, so the result is longer than max_length and contains wrong content.

Guard against this:

  • If max_length <= len(suffix), either raise ValueError or return suffix[:max_length] — depending on the intended contract.
Suggested change
return ' '.join(text.split()[::-1])
def truncate(text, max_length, suffix='...'):
if len(text) <= max_length:
return text
return text[:max_length - len(suffix)] + suffix
def truncate(text, max_length, suffix='...'):
if max_length < len(suffix):
raise ValueError("max_length must be >= len(suffix)")
if len(text) <= max_length:
return text
return text[:max_length - len(suffix)] + suffix
  • Mark as noise

@CristianAmbrosini
CristianAmbrosini deleted the add-string-helpers branch April 24, 2026 09:55
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