diff --git a/Changes.md b/Changes.md index daef9d5..c68bd7b 100644 --- a/Changes.md +++ b/Changes.md @@ -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`. diff --git a/strings/content.go b/strings/content.go index 5482fce..6ed00ae 100644 --- a/strings/content.go +++ b/strings/content.go @@ -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( diff --git a/strings/content_test.go b/strings/content_test.go index 94c2d28..9723e88 100644 --- a/strings/content_test.go +++ b/strings/content_test.go @@ -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", " ")) }