Skip to content
Merged
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
30 changes: 18 additions & 12 deletions pkg/filter/ql/functions/substr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package functions
type Substr struct{}

func (f Substr) Call(args []interface{}) (interface{}, bool) {
if len(args) < 3 {
if len(args) < 2 {
return false, false
}

Expand All @@ -40,17 +40,23 @@ func (f Substr) Call(args []interface{}) (interface{}, bool) {
return false, false
}

switch v := args[2].(type) {
case int:
end = v
case int64:
end = int(v)
default:
return false, false
}
if len(args) > 2 {
switch v := args[2].(type) {
case int:
end = v
case int64:
end = int(v)
default:
return false, false
}

if start >= 0 && (end >= start && end < len(s)) {
return s[start:end], true
if start >= 0 && (end >= start && end < len(s)) {
return s[start:end], true
}
} else {
if start >= 0 && start < len(s) {
return s[start:], true
}
}

return s, true
Expand All @@ -62,7 +68,7 @@ func (f Substr) Desc() FunctionDesc {
Args: []FunctionArgDesc{
{Keyword: "string", Types: []ArgType{Func, Field, BoundField, BoundSegment, BareBoundVariable}, Required: true},
{Keyword: "start", Types: []ArgType{Func, Number}, Required: true},
{Keyword: "end", Types: []ArgType{Func, Number}, Required: true},
{Keyword: "end", Types: []ArgType{Func, Number}},
},
}
return desc
Expand Down
8 changes: 8 additions & 0 deletions pkg/filter/ql/functions/substr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func TestSubstr(t *testing.T) {
[]interface{}{"Hello World!", 6, 7},
"W",
},
{
[]interface{}{"Hello World!", 6},
"World!",
},
{
[]interface{}{"Hello World!", 20},
"Hello World!",
},
}

for i, tt := range tests {
Expand Down