Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strings.Index panic #24

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## WIP TBD

* :hammer: Fixed buf in `strings.Indent` that caused a panic if the input string was empty.

## v0.9.0 2024-09-03

* Adding `maps.Flip`, `maps.FlipSlice`, and `sets.FlipMap`.
Expand Down
2 changes: 1 addition & 1 deletion strings/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func Increment(input string) string {
// Indent returns the string with each line indented by the given string.
func Indent(s, indent string) string {
startIndent := indent
if s[0] == '\n' {
if len(s) > 0 && s[0] == '\n' {
startIndent = ""
}
return strings.TrimRight(
Expand Down
1 change: 1 addition & 0 deletions strings/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestIncrement(t *testing.T) {
func TestIndent(t *testing.T) {
t.Parallel()

assert.Equal(t, "", strings.Indent("", " "))
assert.Equal(t, "\n a\n b\n c\n", strings.Indent("\na\nb\nc\n", " "))
assert.Equal(t, " a\n b\n c", strings.Indent("a\nb\nc", " "))
}