Skip to content

fix: ChunkString returns empty slice for empty input (fixes #788)#880

Open
nghiack7 wants to merge 1 commit into
samber:masterfrom
nghiack7:fix/chunk-string-empty
Open

fix: ChunkString returns empty slice for empty input (fixes #788)#880
nghiack7 wants to merge 1 commit into
samber:masterfrom
nghiack7:fix/chunk-string-empty

Conversation

@nghiack7
Copy link
Copy Markdown

@nghiack7 nghiack7 commented May 9, 2026

Summary

ChunkString("", n) was returning [""] (a one-element slice containing an empty string) instead of [] (an empty slice). This was inconsistent with Chunk([]rune{}, n) which correctly returns an empty slice.

Root Cause

The early-return guard if size >= len(str) { return []T{str} } always fires when str is empty because any positive size satisfies size >= 0. So it returned []T{""} instead of an empty slice.

Fix

Add an explicit early-return for the empty-string case before the size check:

if len(str) == 0 {
    return []T{}
}

Test

Updated the existing test case (which already carried a // @TODO: should be [] comment referencing #788) to assert the correct behavior.

// Before
ChunkString("", 2) // => [""]

// After  
ChunkString("", 2) // => []

Full test suite passes with no regressions.

Fixes #788

ChunkString("", n) was returning [""] (a slice containing one empty
string) because the early-return guard `size >= len(str)` always
fires when str is empty (any positive size satisfies 0 >= 0).

Add an explicit early-return for the empty-string case so that
ChunkString("", n) returns [] consistently with Chunk([]rune{}, n).

Update the existing test case that carried a TODO comment noting this
inconsistency (see samber#788).
@nghiack7
Copy link
Copy Markdown
Author

Friendly ping — this is a one-line fix for ChunkString("", n) returning [""] instead of []. All tests pass ✅. Fixes #788.

@samber
Copy link
Copy Markdown
Owner

samber commented May 23, 2026

Hello @nghiack7

Sure, this is a one-line fix, but it would break lots of code. The #788 is not fixed because the change impact is huge.

Adding a label #breaking-change.

@samber samber added the breaking change Introduces changes that break backward compatibility or alter the public API. label May 23, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.58%. Comparing base (6f2b2bc) to head (fe94cf7).
⚠️ Report is 14 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #880   +/-   ##
=======================================
  Coverage   92.57%   92.58%           
=======================================
  Files          32       32           
  Lines        5067     5069    +2     
=======================================
+ Hits         4691     4693    +2     
  Misses        273      273           
  Partials      103      103           
Flag Coverage Δ
unittests 92.58% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

breaking change Introduces changes that break backward compatibility or alter the public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2] ChunkString behavior inconsistency with Chunk for empty input

2 participants