diff --git a/filters/standard_filters.go b/filters/standard_filters.go index e367f7d..87dac05 100644 --- a/filters/standard_filters.go +++ b/filters/standard_filters.go @@ -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) diff --git a/filters/standard_filters_test.go b/filters/standard_filters_test.go index 6a05fa1..2386db1 100644 --- a/filters/standard_filters_test.go +++ b/filters/standard_filters_test.go @@ -109,6 +109,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"},