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 slice #72

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 50 additions & 11 deletions filters/standard_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package filters
import (
"encoding/json"
"fmt"
"github.com/osteele/liquid/values"
"github.com/osteele/tuesday"
"html"
"math"
"net/url"
Expand All @@ -12,10 +14,6 @@ import (
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/osteele/liquid/values"
"github.com/osteele/tuesday"
)

// A FilterDictionary holds filters.
Expand Down Expand Up @@ -150,14 +148,55 @@ func AddStandardFilters(fd FilterDictionary) { // nolint: gocyclo
return strings.Replace(s, old, new, 1)
})
fd.AddFilter("sort_natural", sortNaturalFilter)
fd.AddFilter("slice", func(s string, start int, length func(int) int) string {
// runes aren't bytes; don't use slice
n := length(1)
if start < 0 {
start = utf8.RuneCountInString(s) + start
fd.AddFilter("slice", func(v interface{}, start int, length func(int) int) interface{} {
// Are we in the []byte case? Transform []byte to string
if b, ok := v.([]byte); ok {
v = string(b)
}
// Are we in the string case?
if s, ok := v.(string); ok {
// Work on runes, not chars
runes := []rune(s)
n := length(1)
if start < 0 {
start = len(runes) + start
if start < 0 {
start = 0
}
}
if start > len(runes) {
start = len(runes)
}
end := start + n
if end > len(runes) {
end = len(runes)
}
return string(runes[start:end])
}
// Are we in the slice case?
// A type test cannot suffice because []T and []U are different types, so we must use conversion.
var slice []interface{}
if sliceIface, err := values.Convert(v, reflect.TypeOf(slice)); err == nil {
var ok bool
if slice, ok = sliceIface.([]interface{}); ok {
n := length(1)
if start < 0 {
start = len(slice) + start
if start < 0 {
start = 0
}
}
if start > len(slice) {
start = len(slice)
}
end := start + n
if end > len(slice) {
end = len(slice)
}
return slice[start:end]
}
}
p := regexp.MustCompile(fmt.Sprintf(`^.{%d}(.{0,%d}).*$`, start, n))
return p.ReplaceAllString(s, "$1")
return nil
})
fd.AddFilter("split", splitFilter)
fd.AddFilter("strip_html", func(s string) string {
Expand Down
19 changes: 19 additions & 0 deletions filters/standard_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filters
import (
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -101,6 +102,24 @@ var filterTests = []struct {
{`"Liquid" | slice: 2`, "q"},
{`"Liquid" | slice: 2, 5`, "quid"},
{`"Liquid" | slice: -3, 2`, "ui"},
{`"Liquid" | slice: 2, 100`, "quid"},
{`"Liquid" | slice: 100`, ""},
{`"Liquid" | slice: 100, 200`, ""},
{`"Liquid" | slice: -100`, "L"},
{`"Liquid" | slice: -100, 200`, "Liquid"},
{`"白鵬翔" | slice: 0`, "白"},
{`"白鵬翔" | slice: 1`, "鵬"},
{`"白鵬翔" | slice: 2`, "翔"},
{`"白鵬翔" | slice: 0, 2`, "白鵬"},
{`"白鵬翔" | slice: 1, 2`, "鵬翔"},
{`"白鵬翔" | slice: 100, 200`, ""},
{`"白鵬翔" | slice: -100`, "白"},
{`"白鵬翔" | slice: -100, 200`, "白鵬翔"},
{`">` + strings.Repeat(".", 10000) + `<" | slice: 1, 10000`, strings.Repeat(".", 10000)},
{`"a,b,c" | split: "," | slice: -1 | join`, "c"},
{`"a,b,c" | split: "," | slice: 1, 1 | join`, "b"},
{`"a,b,c" | split: "," | slice: 0, 2 | join`, "a b"},
{`"a,b,c" | split: "," | slice: 1, 2 | join`, "b c"},

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