Skip to content

Commit

Permalink
Merge pull request #99 from jamslinger/slice-out-of-bound
Browse files Browse the repository at this point in the history
Don't panic in when slicing with index out of bound
  • Loading branch information
danog authored Oct 19, 2024
2 parents c1f5e56 + 76036f7 commit fe7cf84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions filters/standard_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,17 @@ func AddStandardFilters(fd FilterDictionary) { //nolint: gocyclo
})
fd.AddFilter("sort_natural", sortNaturalFilter)
fd.AddFilter("slice", func(s string, start int, length func(int) int) string {
if len(s) == 0 {
return ""
}
ss := []rune(s)
n := length(1)
if start < 0 {
start = len(ss) + start
}
if start < 0 {
return ""
}
end := start + n
if end > len(ss) {
end = len(ss)
Expand Down
2 changes: 2 additions & 0 deletions filters/standard_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Liquid" | slice: 0`, "L"},
{`"Liquid
Liquid" | slice: 2, 4`, "quid"},
{`"Liquid" | slice: -3, 2`, "ui"},
{`"" | slice: 1`, ""},
{`"Liquid" | slice: -7`, ""},

{`"a/b/c" | split: '/' | join: '-'`, "a-b-c"},
{`"a/b/" | split: '/' | join: '-'`, "a-b"},
Expand Down

0 comments on commit fe7cf84

Please sign in to comment.