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

Support indexof_n built-in function #4172

Merged
merged 5 commits into from Jan 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions ast/builtins.go
Expand Up @@ -112,6 +112,7 @@ var DefaultBuiltins = [...]*Builtin{
Concat,
FormatInt,
IndexOf,
IndexOfN,
Substring,
Lower,
Upper,
Expand Down Expand Up @@ -895,6 +896,18 @@ var IndexOf = &Builtin{
),
}

// IndexOfN returns a list of all the indexes of a substring contained inside a string
var IndexOfN = &Builtin{
Name: "indexof_n",
Decl: types.NewFunction(
types.Args(
types.S,
types.S,
),
types.NewArray(nil, types.N),
),
}

// Substring returns the portion of a string for a given start index and a length.
// If the length is less than zero, then substring returns the remainder of the string.
var Substring = &Builtin{
Expand Down
20 changes: 20 additions & 0 deletions capabilities.json
Expand Up @@ -1090,6 +1090,26 @@
"type": "function"
}
},
{
"name": "indexof_n",
"decl": {
"args": [
{
"type": "string"
},
{
"type": "string"
}
],
"result": {
"dynamic": {
"type": "number"
},
"type": "array"
},
"type": "function"
}
},
{
"name": "internal.member_2",
"decl": {
Expand Down
1 change: 1 addition & 0 deletions docs/content/policy-reference.md
Expand Up @@ -378,6 +378,7 @@ complex types.
| <span class="opa-keep-it-together">``endswith(string, search)``</span> | true if ``string`` ends with ``search`` | ✅ |
| <span class="opa-keep-it-together">``output := format_int(number, base)``</span> | ``output`` is string representation of ``number`` in the given ``base`` | ✅ |
| <span class="opa-keep-it-together">``output := indexof(string, search)``</span> | ``output`` is the index inside ``string`` where ``search`` first occurs, or -1 if ``search`` does not exist | ✅ |
| <span class="opa-keep-it-together">``output := indexof_n(string, search)``</span> | ``output`` is ``array[number]`` representing the indexes inside ``string`` where ``search`` occurs | ``SDK-dependent`` |
| <span class="opa-keep-it-together">``output := lower(string)``</span> | ``output`` is ``string`` after converting to lower case | ✅ |
| <span class="opa-keep-it-together">``output := replace(string, old, new)``</span> | ``output`` is a ``string`` representing ``string`` with all instances of ``old`` replaced by ``new`` | ✅ |
| <span class="opa-keep-it-together">``output := strings.reverse(string)``</span> | ``output`` is ``string`` reversed | ✅ |
Expand Down
41 changes: 41 additions & 0 deletions test/cases/testdata/strings/test-strings-0925.yaml
@@ -0,0 +1,41 @@
cases:
- note: 'strings/indexof_n_single_match'
query: data.test.p = x
modules:
- |
package test
p := indexof_n("dogcat", "cat")
want_result:
- x: [3]
- note: 'strings/indexof_n_multiple_matches'
query: data.test.p = x
modules:
- |
package test
p := indexof_n("dogcatdogcat", "cat")
want_result:
- x: [3, 9]
- note: 'strings/indexof_n_no_match'
query: data.test.p = x
modules:
- |
package test
p := indexof_n("dogcat", "rabbit")
want_result:
- x: []
- note: 'strings/indexof_n_unicode_matches'
query: data.test.p = x
modules:
- |
package test
p := indexof_n("😇😀😇😀😇😀", "😀")
want_result:
- x: [1, 3, 5]
- note: 'strings/indexof_n_unicode_no_match'
query: data.test.p = x
modules:
- |
package test
p := indexof_n("😇😀😇😀😇😀", "😂")
want_result:
- x: []
51 changes: 42 additions & 9 deletions topdown/strings.go
Expand Up @@ -87,19 +87,19 @@ func builtinConcat(a, b ast.Value) (ast.Value, error) {
return ast.String(strings.Join(strs, string(join))), nil
}

func builtinIndexOf(a, b ast.Value) (ast.Value, error) {
runesEqual := func(a, b []rune) bool {
if len(a) != len(b) {
func runesEqual(a, b []rune) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
return true
}

func builtinIndexOf(a, b ast.Value) (ast.Value, error) {
base, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,6 +130,38 @@ func builtinIndexOf(a, b ast.Value) (ast.Value, error) {
return ast.IntNumberTerm(-1).Value, nil
}

func builtinIndexOfN(a, b ast.Value) (ast.Value, error) {
base, err := builtins.StringOperand(a, 1)
if err != nil {
return nil, err
}

search, err := builtins.StringOperand(b, 2)
if err != nil {
return nil, err
}
if len(string(search)) == 0 {
return nil, fmt.Errorf("empty search character")
}

baseRunes := []rune(string(base))
searchRunes := []rune(string(search))
searchLen := len(searchRunes)

var arr []*ast.Term
for i, r := range baseRunes {
if len(baseRunes) >= i+searchLen {
if r == searchRunes[0] && runesEqual(baseRunes[i:i+searchLen], searchRunes) {
arr = append(arr, ast.IntNumberTerm(i))
}
} else {
break
}
}

return ast.NewArray(arr...), nil
}

func builtinSubstring(a, b, c ast.Value) (ast.Value, error) {

base, err := builtins.StringOperand(a, 1)
Expand Down Expand Up @@ -435,6 +467,7 @@ func init() {
RegisterFunctionalBuiltin2(ast.FormatInt.Name, builtinFormatInt)
RegisterFunctionalBuiltin2(ast.Concat.Name, builtinConcat)
RegisterFunctionalBuiltin2(ast.IndexOf.Name, builtinIndexOf)
RegisterFunctionalBuiltin2(ast.IndexOfN.Name, builtinIndexOfN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] RegisterFunctionalBuiltin2 is deprecated, let's use RegisterBuiltinFunc instead.

RegisterFunctionalBuiltin3(ast.Substring.Name, builtinSubstring)
RegisterFunctionalBuiltin2(ast.Contains.Name, builtinContains)
RegisterFunctionalBuiltin2(ast.StartsWith.Name, builtinStartsWith)
Expand Down