Skip to content

Conversation

@rm-hull
Copy link
Contributor

@rm-hull rm-hull commented Nov 8, 2025

At the moment, if I pass an empty string as per the added test case, then the stringwrap function panics:

$ go test ./...
--- FAIL: TestStringWrap (0.00s)
    --- FAIL: TestStringWrap/Wrapped_String_Test_15 (0.00s)
panic: runtime error: index out of range [-1] [recovered, repanicked]

goroutine 37 [running]:
testing.tRunner.func1.2({0x10249e600, 0x140000b8690})
        /opt/homebrew/Cellar/go/1.25.4/libexec/src/testing/testing.go:1872 +0x190
testing.tRunner.func1()
        /opt/homebrew/Cellar/go/1.25.4/libexec/src/testing/testing.go:1875 +0x31c
panic({0x10249e600?, 0x140000b8690?})
        /opt/homebrew/Cellar/go/1.25.4/libexec/src/runtime/panic.go:783 +0x120
github.com/galactixx/stringwrap.(*WrappedStringSeq).lastWrappedLine(...)
        /Users/rhu/dev/stringwrap/stringwrap.go:92
github.com/galactixx/stringwrap.stringWrap({0x102445868, 0x0}, 0x5, 0x4, 0x1, 0x1)
        /Users/rhu/dev/stringwrap/stringwrap.go:491 +0x548
github.com/galactixx/stringwrap.StringWrapSplit(...)
        /Users/rhu/dev/stringwrap/stringwrap.go:542
github.com/galactixx/stringwrap.wrapString({{0x102445868, 0x0}, {0x102445868, 0x0}, 0x5, 0x1, 0x1})
        /Users/rhu/dev/stringwrap/stringwrap_test.go:25 +0x48
github.com/galactixx/stringwrap.TestStringWrap.func1(0x14000139180)
        /Users/rhu/dev/stringwrap/stringwrap_test.go:143 +0x40
testing.tRunner(0x14000139180, 0x1400008eef0)
        /opt/homebrew/Cellar/go/1.25.4/libexec/src/testing/testing.go:1934 +0xc8
created by testing.(*T).Run in goroutine 22
        /opt/homebrew/Cellar/go/1.25.4/libexec/src/testing/testing.go:1997 +0x364
FAIL    github.com/galactixx/stringwrap 0.466s
FAIL

This PR adds a test case to TestStringWrap to reproduce the panic.

edit: LMK if you'd like me to provide a fix & i'll add further commits to this PR I've pushed a second commit fixing the issue.

Adds a test case to `TestStringWrap` to verify that an empty
string input correctly yields an empty string output.
The function `lastWrappedLine` is updated to safely return `nil`
if the internal sequence of wrapped lines is empty, avoiding
a runtime panic (index out of bounds).

The main wrapping logic now checks for a `nil` return before
accessing the line properties.

Testing logic for zero-length wrapped strings was also fixed
to correctly assert line counts for empty output.
@rm-hull rm-hull changed the title [WIP] bugfix: empty string causes panic fix: empty string causes panic Nov 8, 2025
@galactixx
Copy link
Owner

this is great. thanks

@galactixx galactixx merged commit 4c1b8d4 into galactixx:main Nov 9, 2025
12 checks passed
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.

2 participants