Skip to content

Commit

Permalink
Merge pull request #24 from zostay/fix-indent-panic
Browse files Browse the repository at this point in the history
fix: strings.Index panic
  • Loading branch information
zostay authored Oct 15, 2024
2 parents f2cb16f + 6dfd53e commit e6d7b20
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
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", " "))
}

0 comments on commit e6d7b20

Please sign in to comment.