Skip to content

fix: handle negative truncate lengths#744

Open
hiSandog wants to merge 1 commit intolarksuite:mainfrom
hiSandog:fix/truncate-negative-limit-20260504
Open

fix: handle negative truncate lengths#744
hiSandog wants to merge 1 commit intolarksuite:mainfrom
hiSandog:fix/truncate-negative-limit-20260504

Conversation

@hiSandog
Copy link
Copy Markdown

@hiSandog hiSandog commented May 4, 2026

Summary

  • return an empty string for non-positive truncate limits
  • prevent negative lengths from panicking in truncate helpers
  • add regression coverage for negative limits and ellipsis zero limits

Validation

  • env GOCACHE=/tmp/cli-gocache GOMODCACHE=/tmp/cli-gomodcache go test ./internal/util
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes
    • Improved string truncation behavior to correctly handle edge cases with non-positive parameters.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2d0c0079-cf04-4574-95f5-6ba6384485e1

📥 Commits

Reviewing files that changed from the base of the PR and between 7fb71c6 and 4c9e862.

📒 Files selected for processing (2)
  • internal/util/strings.go
  • internal/util/strings_test.go

📝 Walkthrough

Walkthrough

Two string truncation utility functions now handle non-positive limits by returning an empty string when n <= 0 before rune conversion. Existing truncation behavior for positive values remains unchanged. Tests are updated to validate the new edge-case handling.

Changes

String Truncation Edge-Case Handling

Layer / File(s) Summary
Core Implementation
internal/util/strings.go
TruncateStr and TruncateStrWithEllipsis add early-return of "" when n <= 0, before rune conversion.
Tests & Validation
internal/util/strings_test.go
Test cases added for n = 0 and n = -1 inputs, asserting empty string results in both functions.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A rabbit's gift, so small yet neat,
When bounds turn sour or minus sweet,
Return the void, no runes to dance—
Edge cases tamed by careful glance. 🐰✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description covers the main objectives but deviates from the repository's template structure, missing the standard 'Changes' and 'Test Plan' sections with checkboxes. Reformat the description to match the repository template with proper 'Changes' and 'Test Plan' sections, or confirm if the current format is acceptable for this repository.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: handling negative truncate lengths in the string truncation functions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the size/M Single-domain feat or fix with limited business impact label May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant